Documentation
¶
Overview ¶
gocli is yet another package to aid with parsing command line arguments.
Unlike many other libraries, it focuses mainly on support of subcommands. Define as many subcommands as you want, they are handled by using FlagSets recursively. Simple yet powerful enough for many scenarios.
The help output format is inspired among others by codegangsta's cli library.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var AppHelpTemplate = `` /* 321-byte string literal not displayed */
Default App help template.
var CommandHelpTemplate = `` /* 242-byte string literal not displayed */
Default Command help template.
Functions ¶
This section is empty.
Types ¶
type App ¶
The root object that starts the chain of subcommands.
Example ¶
// Create the root App object. app := NewApp("app") app.Short = "my bloody gocli app" app.Version = "1.2.3" app.Long = ` This is a long description of my super uber cool app.` // A verbose switch flag. var verbose bool // Create a subcommand. var subcmd = &Command{ UsageLine: "subcmd [-v]", Short: "some kind of subcommand, you name it", Long: "Brb, too tired to write long descriptions.", Action: func(cmd *Command, args []string) { fmt.Printf("verbose mode set to %t\n", verbose) }, } // Set up the verbose switch. This can be as well called in init() or so. subcmd.Flags.BoolVar(&verbose, "v", false, "print verbose output") // Register the command with the parent command. Also suitable for init(). app.MustRegisterSubcommand(subcmd) /* Run the whole thing. app.Run([]string{}) would lead into: APPLICATION: app - my bloody gocli app OPTIONS: -h=false: print help and exit VERSION: 1.2.3 DESCRIPTION: This is a long description of my super uber cool app. SUBCOMMANDS: subcmd some kind of subcommand, you name it , app.Run([]string{"subcmd", "-h"}) into something similar. */ app.Run([]string{"subcmd"}) app.Run([]string{"subcmd", "-v"})
Output: verbose mode set to false verbose mode set to true
type Command ¶
type Command struct { // Fitting to be set in the Command struct literal. UsageLine string Short string Long string Action func(cmd *Command, args []string) // Flag set for this subcommand. Flags flag.FlagSet // All subcommands registered. Although it can be accessed directly, // it is not supposed to be used like that. Use MustRegisterSubcmd(). // This is exported only for the templates to be able to access it. Subcmds []*Command // contains filtered or unexported fields }
Command represents a node in the chain of subcommands. Even App uses it, it is embedded there.
func (*Command) DefaultFlagsString ¶
Returns the same as flag.FlagSet.PrintDefaults, but as a string instead of printing it directly into the output stream.
func (*Command) MustRegisterSubcommand ¶
Register a new subcommand with the command. Panic if something is wrong.