Documentation ¶
Overview ¶
Package cli helps you create command line interfaces with the Go Programming Language
Index ¶
Constants ¶
const Version = "0.0.4"
Version is the current version number of the cli package
Variables ¶
var Output = os.Stdout // the output is the same for all Apps, atm.
Output the common output of the cli app, defaults to os.Stdout
Functions ¶
Types ¶
type Action ¶
Action the command's listener
func DefaultAction ¶
DefaultAction is the default action where no command's action is defined
type App ¶
App is the container of all commands, subcommands, flags and your application's name and description it builds the full help message and other things useful for your app example of initialize an cli.App in the init(): var app *cli.App init the cli app func init(){ app = cli.NewApp("iris", "Command line tool for Iris web framework", Version) // version command app.Command(cli.Command("version", "\t prints your iris version").
Action(func(cli.Flags) error { app.Printf("%s", iris.Version); return nil }))
// create command createCmd := cli.Command("create", "creates & runs a project based on the iris-contrib/examples, to a given directory").
Flag("dir", workingDir, "the directory to install the sample package"). Flag("type", "basic", "creates a project based on the -t package. Currently, available types are 'basic' & 'static'"). Action(create)
// run command runAndWatchCmd := cli.Command("run", "runs and reload on source code changes, example: iris run main.go").Action(runAndWatch)
// register the commands app.Command(createCmd) app.Command(runAndWatchCmd) }
func main() { // run the application app.Run(func(cli.Flags) error { return nil }) }
func NewApp ¶
NewApp creates and returns a new cli App instance
example: app := cli.NewApp("iris", "Command line tool for Iris web framework", Version)
func (*App) Flag ¶
Flag creates a new flag based on name, defaultValue (if empty then the flag is required) and a usage
func (App) HasCommands ¶
HasCommands returns true if the app has registered commands, otherwise false
func (*App) Printf ¶
Printf just like fmt.Printf but prints to the application's output, which is the global Output
func (App) Run ¶
Run executes the App, must be called last it receives an action too, like the commands but this action can be empty but if this action returns an error then the execution panics. Its common usage is when you need to 'monitor' the application's commands flags and check if a command should execute based its name or no (the app's command is a flag to the app's action function)
type Cmd ¶
type Cmd struct { Name string Description string // Flags are not the arguments was given by the user, but the flags that developer sets to this command Flags Flags Subcommands Commands // contains filtered or unexported fields }
Cmd is the command struct which contains the command's Name, description, any flags, any subcommands and mostly its action listener
func Command ¶
Command builds and returns a new *Cmd, no app-relative. Note that the same command can be used by multiple applications. example: createCmd := cli.Command("create", "creates a project to a given directory").
Flag("offline", false, "set to true to disable the packages download on each create command"). Flag("dir", workingDir, "$GOPATH/src/$dir the directory to install the sample package"). Flag("type", "basic", "creates a project based on the -t package. Currently, available types are 'basic' & 'static'"). Action(create) app = cli.NewApp("iris", "Command line tool for Iris web framework", Version) app.Command(createCmd) // registers the command to this app
returns itself
func (*Cmd) Action ¶
Action is the most important function declares a command's action/listener example: createCmd := cli.Command("create", "creates a project to a given directory").
Flag("dir", workingDir, "the directory to install the sample package"). Action(func(flags cli.Flags){ /* here the action */ })
returns itself
func (*Cmd) Execute ¶
Execute calls the Action of the command and the subcommands returns true if this command has been executed successfully
func (*Cmd) Flag ¶
Flag sets a flag to the command example: createCmd := cli.Command("create", "creates a project to a given directory").
Flag("dir", "the default value/path is setted here", "$GOPATH/src/$dir the directory to install the sample package").Flag(...).Action(...)
returns the Cmd itself
type Flag ¶
type Flag struct { Name string Default interface{} Usage string Value interface{} Raw *goflags.FlagSet }
Flag is the command's action's flag, use it by flags.Get("myflag").Alias()/.Name/.Usage/.Raw/.Value
type Flags ¶
type Flags []*Flag
Flags the flags passed to the command's Action
func (Flags) Bool ¶
Bool returns the flag's value as bool by it's name, if not found returns false panics on !bool
func (Flags) Int ¶
Int returns the flag's value as int by it's name, if can't parse int then returns -1 panics on !int
func (Flags) String ¶
String returns the flag's value as string by it's name, if not found returns empty string "" panics on !string