src

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RegexArgumentMatchCommand        = regexp.MustCompile(`^[a-zA-Z][-_a-zA-Z0-9:.]*$`)
	RegexArgumentMatchOption         = regexp.MustCompile(`^-`)
	RegexArgumentMatchOptionWithFull = regexp.MustCompile(`^(-+)([a-zA-Z0-9][-_a-zA-Z0-9.]*)=(.+)`)
	RegexArgumentMatchOptionWithName = regexp.MustCompile(`^(-+)([a-zA-Z0-9][-_a-zA-Z0-9.]*)$`)
)

Functions

This section is empty.

Types

type Argument

type Argument struct {
	// contains filtered or unexported fields
}

Argument is a component used to parse command-line arguments.

func NewArgument

func NewArgument() *Argument

NewArgument returns a singleton instance.

func (*Argument) GetCommandName

func (o *Argument) GetCommandName() string

GetCommandName returns a command name that defined in argument.

Usage:

go run main.go example

Output:

example

func (*Argument) GetOption

func (o *Argument) GetOption(name string) (str string, has bool)

GetOption returns an option value with given name if exists.

Usage:

go run main.go example --key=value

Code:

key, has := console.GetOption("key")

Output:

"value", true

func (*Argument) GetOptions

func (o *Argument) GetOptions() map[string]string

GetOptions returns all options mapper.

Output:

{
    "key": "value"
}

func (*Argument) GetScriptExecutor

func (o *Argument) GetScriptExecutor() string

func (*Argument) GetScriptPath

func (o *Argument) GetScriptPath() string

GetScriptPath returns a real script path that called in argument.

Usage: (compiled)

/data/sketch/app example

Output:

/data/sketch/app

func (*Argument) Parse

func (o *Argument) Parse(args []string)

Parse parses specified argument list given args.

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command is a component used to schedule in container.

func NewCommand

func NewCommand(name string) *Command

NewCommand creates a new command instance.

func NewHelp

func NewHelp() *Command

NewHelp creates a built-in provider.

func (*Command) Add

func (o *Command) Add(options ...*Option)

Add adds an options list into command.

func (*Command) GenerateModule added in v1.0.5

func (o *Command) GenerateModule(path string) (name, folder string)

GenerateModule generate application module name and working path relatives with working directory.

func (*Command) GetDescription

func (o *Command) GetDescription() string

GetDescription returns a command description.

func (*Command) GetName

func (o *Command) GetName() string

GetName returns a command name.

Code:

println(cmd.GetName())

Output:

example

func (*Command) GetOption

func (o *Command) GetOption(key string) (opt *Option, has bool)

GetOption returns an option in command if exists.

Code:

opt, has := cmd.GetOption("key")

Output:

&Option{}, true

func (*Command) GetOptions

func (o *Command) GetOptions() map[string]*Option

GetOptions returns an option mapping.

Output:

{
    "key": &Option{...},
}

func (*Command) SetDescription

func (o *Command) SetDescription(description string) *Command

SetDescription set description for command.

func (*Command) SetProvider

func (o *Command) SetProvider(provider Provider) *Command

SetProvider set command provider. Method's in provider will be called when scheduler fired.

func (*Command) ToScript added in v1.0.3

func (o *Command) ToScript() (script string)

ToScript convert runtime command as runnable script.

gen:model --key="value" --null

type CommandConfig

type CommandConfig struct {
	Options map[string]string `yaml:"options" json:"options"`
}

CommandConfig is a component used to define options.

func (*CommandConfig) After

func (o *CommandConfig) After()

After is an event handler that called by parent.

func (*CommandConfig) GetOption

func (o *CommandConfig) GetOption(name string) any

GetOption returns an option definition if defined in config file.

func (*CommandConfig) GetOptions

func (o *CommandConfig) GetOptions() map[string]string

GetOptions returns an option mapper of a command.

type Configuration

type Configuration struct {
	Commands map[string]*CommandConfig `yaml:"commands" json:"commands"`
	// contains filtered or unexported fields
}

Configuration is a component used to configure console variables.

var Config *Configuration

Config is a singleton instance for console configuration.

func (*Configuration) After

func (o *Configuration) After()

After is an event handler used to be called in config-file manager.

func (*Configuration) GetCommand

func (o *Configuration) GetCommand(name string) *CommandConfig

GetCommand returns a command configuration if define in config file.

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container is a component used to manage all components for console.

func NewContainer

func NewContainer() *Container

NewContainer returns a singleton instance of package.

func (*Container) Add

func (o *Container) Add(commands ...*Command)

Add a commands list into container.

func (*Container) Context

func (o *Container) Context() context.Context

Context returns a root context.

func (*Container) Del

func (o *Container) Del(commands ...*Command)

Del deletes a commands list from container.

func (*Container) DelByName

func (o *Container) DelByName(name string)

DelByName deletes a command from container.

func (*Container) GetArgument

func (o *Container) GetArgument() *Argument

GetArgument returns the singleton instance of Argument.

func (*Container) GetCommand

func (o *Container) GetCommand(name string) (cmd *Command, has bool)

GetCommand returns the singleton instance of Command if exists.

func (*Container) GetCommands

func (o *Container) GetCommands() map[string]*Command

GetCommands returns a commands mapper in container.

func (*Container) GetOutput

func (o *Container) GetOutput() *Output

GetOutput returns the Output component.

func (*Container) Run

func (o *Container) Run()

Run the container

func (*Container) RunWith

func (o *Container) RunWith(args []string)

RunWith run the container with given args.

type Help

type Help struct {
	// contains filtered or unexported fields
}

Help is a built-in command provider used to print usages about console and command.

func (*Help) After

func (o *Help) After(_ context.Context, container *Container, _ *Command, err error)

After is a handler used to call in Command.

func (*Help) Before

func (o *Help) Before(_ context.Context, container *Container, command *Command) (err error)

Before is a handler used to call in Command.

func (*Help) Run

func (o *Help) Run(_ context.Context, container *Container, command *Command) (err error)

Run is a handler used to call in Command.

type Kind

type Kind int
const (
	KindString Kind = iota // Value of option accept any string.
	KindBool               // Value of option accept any boolean values
	KindFloat              // Value of option accept any float values
	KindInt                // Value of option accept any integer values
	KindNull               // Value of option do not accept any value.
)

func (Kind) Value

func (o Kind) Value() string

Value returns a type mapping string.

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option is an interface used to store command options.

func NewBoolOption

func NewBoolOption(name string) *Option

func NewFloatOption

func NewFloatOption(name string) *Option

func NewIntOption

func NewIntOption(name string) *Option

func NewNullOption

func NewNullOption(name string) *Option

func NewOption

func NewOption(name string) *Option

NewOption creates an option instance.

func (*Option) Generate

func (o *Option) Generate() string

func (*Option) GetDefaultValue

func (o *Option) GetDefaultValue() any

func (*Option) GetDescription

func (o *Option) GetDescription() string

func (*Option) GetKind

func (o *Option) GetKind() Kind

func (*Option) GetName

func (o *Option) GetName() string

func (*Option) GetRequired

func (o *Option) GetRequired() bool

func (*Option) GetShortName

func (o *Option) GetShortName() string

func (*Option) GetSpecified

func (o *Option) GetSpecified() bool

func (*Option) SetDefaultValue

func (o *Option) SetDefaultValue(dv any) *Option

func (*Option) SetDescription

func (o *Option) SetDescription(description string) *Option

func (*Option) SetKind

func (o *Option) SetKind(kind Kind) *Option

func (*Option) SetRequired

func (o *Option) SetRequired(required bool) *Option

func (*Option) SetShortName

func (o *Option) SetShortName(c byte) *Option

func (*Option) ToBool

func (o *Option) ToBool() bool

func (*Option) ToFloat

func (o *Option) ToFloat() float64

func (*Option) ToInt

func (o *Option) ToInt() int64

func (*Option) ToString

func (o *Option) ToString() string

type Output

type Output struct{}

Output is a component used to send message to terminal.

func NewOutput

func NewOutput() *Output

NewOutput creates a new Output instance.

func (*Output) Error

func (o *Output) Error(format string, args ...any)

Error send error message to terminal.

func (*Output) Fatal

func (o *Output) Fatal(format string, args ...any)

Fatal send fatal message to terminal.

func (*Output) Info

func (o *Output) Info(format string, args ...any)

Info send info message to terminal.

func (*Output) Render

func (o *Output) Render(list []string)

Render send list of message with info to terminal.

type Provider

type Provider interface {
	Run(ctx context.Context, container *Container, command *Command) error
}

Provider is a required handler. It's called when command scheduled and Before returned nil.

type ProviderAfter

type ProviderAfter interface {
	After(ctx context.Context, container *Container, command *Command, err error)
}

ProviderAfter is an optional handler. It's called when required handler done.

type ProviderBefore

type ProviderBefore interface {
	Before(ctx context.Context, container *Container, command *Command) error
}

ProviderBefore is an optional handler. It's called when command scheduled, Ignore other handlers if error returned

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL