Documentation
¶
Index ¶
- Variables
- type ArgValidator
- func ArgsAny() ArgValidator
- func ArgsBetween(lower, upper int) ArgValidator
- func ArgsEvery(validators ...ArgValidator) ArgValidator
- func ArgsExact(n int) ArgValidator
- func ArgsMax(bound int) ArgValidator
- func ArgsMin(bound int) ArgValidator
- func ArgsNone() ArgValidator
- func ArgsSome(validators ...ArgValidator) ArgValidator
- type Command
- type Context
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNoRunner = errors.New("gommand: command has no run function") ErrNoSubcommand = errors.New("gommand: must specify a subcommand") )
Functions ¶
This section is empty.
Types ¶
type ArgValidator ¶
func ArgsAny ¶
func ArgsAny() ArgValidator
func ArgsBetween ¶
func ArgsBetween(lower, upper int) ArgValidator
func ArgsEvery ¶
func ArgsEvery(validators ...ArgValidator) ArgValidator
func ArgsExact ¶
func ArgsExact(n int) ArgValidator
func ArgsMax ¶
func ArgsMax(bound int) ArgValidator
func ArgsMin ¶
func ArgsMin(bound int) ArgValidator
func ArgsNone ¶
func ArgsNone() ArgValidator
func ArgsSome ¶
func ArgsSome(validators ...ArgValidator) ArgValidator
type Command ¶
type Command struct { // Name is the name of the command. // This is ignored if the command is invoked using Execute or ExecuteContext, but if registered as a subcommand // of a function, the name defines how the subcommand is called. // ex: // c1 := &Command{Name: "entrypoint"} // c2 := &Command{Name: "my-sub-command", Run: func(*Context) error { fmt.Println("sub"); return nil }} // // c1.SubCommand(c2) // // func main() { // _ = c1.Execute() // } // // c2 would be called by running // entrypoint my-sub-command // // Anything included after a space is expected to be usage descriptions // General syntax guidance // ... indicates multiple of the preceding argument can be provided // [ ] indicates optional arguments // { } indicates a set of mutually exclusive required arguments // | indicates mutually exclusive arguments, where only one value in the set // should be provided at a time. As described above, if the set of arguments // are optional, the set should be enclosed in [ ] otherwise they should be // enclosed in { } // // Example: create {--from-file file | --from-gcs bucket} [-d destination] file_name... Name string // Usage is the short explanation of the command Usage string // Description is the longer description of the command printed out by the help text Description string // Aliases are aliases for the current command. // // Ex: // c1 := &Command{Name: "items"} // c2 := &Command{Name: "list", Aliases: []string{"ls", "l"}} // c1.SubCommand(c2) // // items list // items ls // items l // // All are valid ways of executing the `list` command Aliases []string // Version is the value that will be printed when `--version` is passed to the command. // When retrieving the command version, the call tree is traversed backwards until a Command // is reached that has a non-zero value for the version. This means that it is possible // to version individual branches of the call tree, though this is not recommended. It is // intended to be set at the root of the tree, ideally through a package level var that can // be set using ldflags at build time // ie: go build -ldflags="cmd.Version=1.1.0" Version string // ArgValidator is an ArgValidator to be called on the args of the function being executed. This is called before any of // the functions for this command are called. // If this is not defined ArgsNone is used. ArgValidator ArgValidator // Flags are a slice of flags.Flag that will be used to initialize the command's FlagSet FlagSet *flags.FlagSet // PersistentFlags are a slice of flags.Flag that will be used to initialize the command's PersistentFlagSet PersistentFlagSet *flags.FlagSet // Run is the core function the command should execute Run func(*Context) error // PreRun will run immediately before Run, if defined PreRun func(*Context) error // PostRun will run immediately after Run if defined and either Run exits with no error or DeferPost is true PostRun func(*Context) error // PersistentPreRun is a function that will run before PreRun and will be run for any subcommands of this command. // // PersistentPreRun commands are executed in FIFO order // // ex: // c1 := &Command{Name: "c1", PersistentPreRun: func(*Context) error { fmt.Println("c1"); return nil }} // c2 := &Command{Name: "c2", PersistentPreRun: func(*Context) error { fmt.Println("c2"); return nil }} // c3 := &Command{Name: "c3", Run: func(*Context) error { fmt.Println("c3"); return nil }} // // c1.SubCommand(c2) // c2.SubCommand(c3) // // When c3 is run, stdout will see // c1 // c2 // c3 // // If any of the nested commands return an error, all execution is stopped and that error is returned. PersistentPreRun func(*Context) error // PersistentPostRun is a function that will run after PostRun and will be run for any subcommands of this command. // // PersistentPostRun commands are executed in LIFO order // // ex: // c1 := &Command{Name: "c1", PersistentPostRun: func(*Context) error { fmt.Println("c1"); return nil }} // c2 := &Command{Name: "c2", PersistentPostRun: func(*Context) error { fmt.Println("c2"); return nil }} // c3 := &Command{Name: "c3", Run: func(*Context) error { fmt.Println("c3"); return nil }} // // c1.SubCommand(c2) // c2.SubCommand(c3) // // when c3 is run, stdout will see // c3 // c2 // c1 // // If any of the nested commands return an error, execution will stop and the error will be returned, unless DeferPost // is true, in which case the error will be recorded and returned at the end, but the remaining functions will still // execute PersistentPostRun func(*Context) error // DeferPost will defer PersistentPostRun and PostRun functions. // This will cause them to run even if the Run function exits with an error. // Setting this value is persistent, meaning any subcommands from where this is set will // also defer their post run functions DeferPost bool // SilenceHelp will not print the help message if the command exits with an error. // This field will propagate to subcommands and cannot be overwritten by the child, so if any // point of a command's upstream lineage has the value set, the help message will be silenced SilenceHelp bool // SilenceError is like SilenceHelp but does not print the "Error: xxx" message when the command // exits with an error SilenceError bool // contains filtered or unexported fields }
Command represents a command line command
The order of functions is:
- PersistentPreRun -- see note on field
- PreRun
- Run
- PostRun
- PersistentPostRun -- see note of field
if PersistentPreRun or PreRun return an error, execution is stopped and the error is returned if Run returns an error and DeferPost is false, execution is stopped and the error is returned if DeferPost is true, PostRun and PersistentPostRun will be executed even if Run returns an error
func (*Command) SubCommand ¶
Click to show internal directories.
Click to hide internal directories.