Documentation ¶
Index ¶
- func Add(name string, cmd any, description string, opts ...CmdOpt)
- func AddAlias(aliasName, target string, opts ...CmdOpt)
- func AddCompletion()
- func AddCompletionWithName(name string)
- func AddGroup(name string, description string, opts ...CmdOpt)
- func AddHelp()
- func AddHidden(name string, cmd any, description string, opts ...CmdOpt)
- func AddRoot(cmd any, opts ...CmdOpt)
- func MustParse(args any, opts ...ParseOpt) *flag.FlagSet
- func Parse(args any, opts ...ParseOpt) (fs *flag.FlagSet, err error)
- func PrintHelp()
- func Run(args ...string)
- func SetGlobalFlags(v any)
- func SetOptions(options Options)
- type App
- func (p *App) Add(name string, cmd any, description string, opts ...CmdOpt)
- func (p *App) AddAlias(aliasName, target string, opts ...CmdOpt)
- func (p *App) AddCompletion()
- func (p *App) AddCompletionWithName(name string)
- func (p *App) AddGroup(name string, description string, opts ...CmdOpt)
- func (p *App) AddHelp()
- func (p *App) AddHidden(name string, cmd any, description string, opts ...CmdOpt)
- func (p *App) AddRoot(cmd any, opts ...CmdOpt)
- func (p *App) Run(args ...string)
- func (p *App) SetGlobalFlags(v any)
- type ArgCompletionContext
- type ArgCompletionFunc
- type CmdOpt
- type Command
- type CompletionItem
- type Context
- type Modifier
- type Options
- type ParseOpt
- func DisableGlobalFlags() ParseOpt
- func ReplaceUsage(f func() string) ParseOpt
- func WithArgCompFuncs(funcMap map[string]ArgCompletionFunc) ParseOpt
- func WithArgs(args []string) ParseOpt
- func WithErrorHandling(h flag.ErrorHandling) ParseOpt
- func WithExamples(examples string) ParseOpt
- func WithFooter(f func() string) ParseOpt
- func WithName(name string) ParseOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
Add adds a command.
Param cmd must be type of one of the following:
- `func()`, user should call `mcli.Parse` inside the function
- `func(ctx *mcli.Context)`, user should call `ctx.Parse` inside the function
- a Command created by NewCommand
func AddCompletion ¶ added in v0.8.0
func AddCompletion()
AddCompletion enables the "completion" command to generate auto-completion script. If you want a different name other than "completion", use AddCompletionWithName.
Note: by default this command only enables command completion, to enable flag completion, user should either set `App.Options.EnableFlagCompletionForAllCommands` to enable flag completion for the whole application, or provide command option `EnableFlagCompletion` when adding a command to enable for a specific command.
func AddCompletionWithName ¶ added in v0.8.0
func AddCompletionWithName(name string)
AddCompletionWithName enables the completion command with custom command name.
Note: by default this command only enables command completion, to enable flag completion, user should either set `App.Options.EnableFlagCompletionForAllCommands` to enable flag completion for the whole application, or provide command option `EnableFlagCompletion` when adding a command to enable for a specific command.
func AddGroup ¶
AddGroup adds a group explicitly. A group is a common prefix for some commands. It's not required to add group before adding sub commands, but user can use this function to add a description to a group, which will be showed in help.
func AddHidden ¶
AddHidden adds a hidden command. f must be a function of signature `func()` or `func(*Context)`, else it panics.
A hidden command won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
See Add for valid types of cmd.
func AddRoot ¶ added in v0.7.0
AddRoot adds a root command processor. When no sub command specified, a root command will be executed.
See Add for valid types of cmd.
func MustParse ¶ added in v0.9.4
MustParse is a helper that wraps Parse and panics if Parse returns an error.
func Parse ¶
Parse parses the command line for flags and arguments. `args` must be a pointer to a struct, else it panics.
By default, it prints help and exits the program if an error occurs when parsing, instead of returning the error, which is the same behavior with package "flag". Generally, user can safely ignore the return value of this function, except that an option `WithErrorHandling(flag.ContinueOnError)` is explicitly passed to it if you want to inspect the error.
See Context.Parse if you use an application created by NewApp instead of the default application.
func Run ¶
func Run(args ...string)
Run runs the program, it parses the command line and searches for a registered command, it runs the command if a command is found, else it will report an error and exit the program.
Optionally you may specify args to parse, by default it parses the command line arguments os.Args[1:].
func SetGlobalFlags ¶ added in v0.2.0
func SetGlobalFlags(v any)
SetGlobalFlags sets global flags, global flags are available to all commands. DisableGlobalFlags may be used to disable global flags for a specific command when calling Parse.
func SetOptions ¶ added in v0.4.0
func SetOptions(options Options)
SetOptions updates options of the default application.
Types ¶
type App ¶ added in v0.3.0
type App struct { // Description optionally provides a description of the program. Description string // Options specifies optional options to custom the behavior // of an App. Options // contains filtered or unexported fields }
App holds the state of a cli application.
func NewApp ¶ added in v0.3.0
func NewApp() *App
NewApp creates a new cli application instance.
Typically, there is no need to manually create an application, using the package-level functions with the default application is preferred.
func (*App) Add ¶ added in v0.3.0
Add adds a command.
Param cmd must be type of one of the following:
- `func()`, user should call `mcli.Parse` inside the function
- `func(ctx *mcli.Context)`, user should call `ctx.Parse` inside the function
- a Command created by NewCommand
func (*App) AddCompletion ¶ added in v0.8.0
func (p *App) AddCompletion()
AddCompletion enables the "completion" command to generate auto-completion script. If you want a different name other than "completion", use AddCompletionWithName.
Note: by default this command only enables command completion, to enable flag completion, user should either set `App.Options.EnableFlagCompletionForAllCommands` to enable flag completion for the whole application, or provide command option `EnableFlagCompletion` when adding a command to enable for a specific command.
func (*App) AddCompletionWithName ¶ added in v0.8.0
AddCompletionWithName enables the completion command with custom command name.
Note: by default this command only enables command completion, to enable flag completion, user should either set `App.Options.EnableFlagCompletionForAllCommands` to enable flag completion for the whole application, or provide command option `EnableFlagCompletion` when adding a command to enable for a specific command.
func (*App) AddGroup ¶ added in v0.3.0
AddGroup adds a group explicitly. A group is a common prefix for some commands. It's not required to add group before adding sub commands, but user can use this function to add a description to a group, which will be showed in help.
func (*App) AddHelp ¶ added in v0.3.0
func (p *App) AddHelp()
AddHelp enables the "help" command to print help about any command.
func (*App) AddHidden ¶ added in v0.3.0
AddHidden adds a hidden command.
A hidden command won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
See App.Add for valid types of cmd.
func (*App) AddRoot ¶ added in v0.7.0
AddRoot adds a root command. When no sub command specified, a root command will be executed.
See App.Add for valid types of cmd.
func (*App) Run ¶ added in v0.3.0
Run is the entry point to an application, it parses the command line and searches for a registered command, it runs the command if a command is found, else it will report an error and exit the program.
Optionally you may specify args to parse, by default it parses the command line arguments os.Args[1:].
func (*App) SetGlobalFlags ¶ added in v0.3.0
SetGlobalFlags sets global flags, global flags are available to all commands. DisableGlobalFlags may be used to disable global flags for a specific command when calling Parse.
type ArgCompletionContext ¶ added in v0.9.0
type ArgCompletionContext interface { context.Context GlobalFlags() any CommandArgs() any FlagSet() *flag.FlagSet ArgPrefix() string }
ArgCompletionContext provides essential information to do suggestion for flag value and positional argument completion.
type ArgCompletionFunc ¶ added in v0.9.0
type ArgCompletionFunc func(ctx ArgCompletionContext) []CompletionItem
ArgCompletionFunc is a function to do completion for flag value or positional argument.
type CmdOpt ¶ added in v0.8.1
type CmdOpt struct {
// contains filtered or unexported fields
}
CmdOpt specifies options to customize the behavior of a Command.
func EnableFlagCompletion ¶ added in v0.8.3
func EnableFlagCompletion() CmdOpt
EnableFlagCompletion enables flag completion for a command. By default, flag completion is disabled to avoid unexpectedly running the user command when doing flag completion, in case that the user does not call `Parse` in the command.
func WithCategory ¶ added in v0.9.0
WithCategory groups commands into different categories in help.
func WithLongDesc ¶ added in v0.8.1
WithLongDesc specifies a long description of a command, which will be showed in the command's help.
type Command ¶
type Command struct { Name string AliasOf string Description string Hidden bool // contains filtered or unexported fields }
Command holds the information of a command.
func NewCommand ¶ added in v0.8.3
NewCommand accepts a typed function and returns a Command. The type parameter T must be a struct, else it panics. When the command is matched, mcli will parse "args" and pass it to f, thus user must not call "Parse" again in f, else it panics. If option `WithErrorHandling(flag.ContinueOnError)` is used, user can use Context.ArgsError to check error that occurred during parsing flags and arguments. In case you want to get the parsed flag.FlagSet, check Context.FlagSet.
type CompletionItem ¶ added in v0.9.0
type Context ¶ added in v0.5.0
Context holds context-specific information, which is passed to a Command when executing it. Context embeds context.Context, it can be passed to functions which take context.Context as parameter.
func (*Context) ArgsError ¶ added in v0.8.3
ArgsError returns the error of parsing arguments. If no error occurs, it returns nil.
func (*Context) FlagSet ¶ added in v0.8.3
FlagSet returns the flag.FlagSet parsed from arguments. This is for compatibility to work with standard library, in most cases, using the strongly-typed parsing result is more convenient.
func (*Context) MustParse ¶ added in v0.9.4
MustParse is a helper that wraps Parse and panics if Parse returns an error.
func (*Context) Parse ¶ added in v0.5.0
Parse parses the command line for flags and arguments. `args` must be a pointer to a struct, else it panics.
By default, it prints help and exits the program if an error occurs when parsing, instead of returning the error, which is the same behavior with package "flag". Generally, user can safely ignore the return value of this function, except that an option `WithErrorHandling(flag.ContinueOnError)` is explicitly passed to it if you want to inspect the error.
Note:
- if you enable flag completion for a command, you must call this in the command function to make the completion work correctly
- if the command is created by NewCommand, mcli automatically calls this to parse flags and arguments, then pass args to user command, you must not call this again, else it panics
type Modifier ¶
type Modifier byte
Modifier represents an option to a flag, it sets the flag to be deprecated, hidden, or required. In a `cli` tag, modifiers appears as the first segment, starting with a `#` character.
Fow now the following modifiers are available:
D - marks a flag or argument as deprecated, "DEPRECATED" will be showed in help. R - marks a flag or argument as required, "REQUIRED" will be showed in help. H - marks a flag as hidden, see below for more about hidden flags. E - marks an argument read from environment variables, but not command line, environment variables will be showed in a separate section in help.
Hidden flags won't be showed in help, except that when a special flag "--mcli-show-hidden" is provided.
Modifier `H` shall not be used for an argument, else it panics. An argument must be showed in help to tell user how to use the program correctly.
Modifier `E` is useful when you want to read an environment variable, but don't want user to provide from command line (e.g. password or other secrets). Using together with `R` also ensures that the env variable must exist.
Some modifiers cannot be used together, else it panics, e.g.
H & R - a required flag must appear in help to tell user to set it D & R - a required flag must not be deprecated, it does not make sense, but makes user confused
type Options ¶ added in v0.4.0
type Options struct { // KeepCommandOrder makes Parse to print commands in the order of // adding the commands. // By default, it prints commands in lexicographic order. KeepCommandOrder bool // AllowPosixSTMO enables using the posix-style single token to specify // multiple boolean options. e.g. `-abc` is equivalent to `-a -b -c`. AllowPosixSTMO bool // EnableFlagCompletionForAllCommands enables flag completion for // all commands of an application. // By default, flag completion is disabled to avoid unexpectedly running // the user command when doing flag completion, in case that the // command does not call `Parse`. // // Note that when flag completion is enabled, the command functions // must call `Parse` to parse flags and arguments, either by creating // Command by NewCommand or manually call `Parse` in functions // with signature `func()` or `func(*mcli.Context)`. EnableFlagCompletionForAllCommands bool // If Parse is called with option `WithFooter`, the option function's // output overrides this setting. HelpFooter string }
Options specifies optional options for an App.
type ParseOpt ¶
type ParseOpt struct {
// contains filtered or unexported fields
}
ParseOpt specifies options to customize the behavior of Parse.
func DisableGlobalFlags ¶ added in v0.2.0
func DisableGlobalFlags() ParseOpt
DisableGlobalFlags tells Parse to don't parse and print global flags in help.
func ReplaceUsage ¶ added in v0.3.0
ReplaceUsage specifies a function to generate a usage help to replace the default help.
func WithArgCompFuncs ¶ added in v0.9.0
func WithArgCompFuncs(funcMap map[string]ArgCompletionFunc) ParseOpt
WithArgCompFuncs specifies completion functions to complete flag values or positional arguments. Key of funcMap should be a flag name in form "-flag" or a positional arg name "arg1".
func WithArgs ¶
WithArgs tells Parse to parse from the given args, instead of parsing from the command line arguments.
func WithErrorHandling ¶
func WithErrorHandling(h flag.ErrorHandling) ParseOpt
WithErrorHandling tells Parse to use the given ErrorHandling. By default, Parse exits the program when an error happens.
func WithExamples ¶ added in v0.8.1
WithExamples specifies examples for a command. Examples will be showed after flags in the command's help.
func WithFooter ¶ added in v0.3.0
WithFooter specifies a function to generate extra help text to print after the default help. If this option is provided, the option function's output overrides the App's optional help-footer setting.