Documentation ¶
Overview ¶
Package cli can be used to create modern command line interfaces.
To create a subcommand create and run an instance of the Command type.
Example ¶
package main import ( "flag" "fmt" "os" "mellium.im/cli" ) func commitCmd() *cli.Command { commitFlags := flag.NewFlagSet("commit", flag.ExitOnError) all := commitFlags.Bool("a", false, "Tell the command to automatically stage files…") return &cli.Command{ Usage: `commit [-a] …`, Description: `Records changes to the repository. Stores the current contents of the index in a new commit…`, Flags: commitFlags, Run: func(c *cli.Command, args ...string) error { commitFlags.Parse(args) fmt.Println("Ran commit!") if *all { fmt.Println("-a flag was used") } return nil }, } } func main() { commit := commitCmd() commit.Help(os.Stdout) }
Output: Usage: commit [-a] … Options: -a Tell the command to automatically stage files… Records changes to the repository. Stores the current contents of the index in a new commit…
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { // Usage always starts with the name of the command, followed by a description // of its usage. For more information, see the Name method. Usage string // Description starts with a short, one line description. It can optionally be // followed by a blank line and then a longer description or help info. Description string // Flags is a flag set that provides options that are specific to this // subcommand. Flags *flag.FlagSet // The action to take when this command is executed. The args will be the // remaining command line args after all flags have been parsed. Run func(c *Command, args ...string) error }
Command represents a new subcommand.
func (*Command) Help ¶
Help writes the usage line, flags, and description for the command to the provided io.Writer. If c.Flags is a valid flag set, calling Help sets the output of c.Flags.
func (*Command) Name ¶
Name returns the first word of c.Usage which will be the name of the command. For example with a usage line of:
commit [options]
Name returns "commit".
type CommandSet ¶ added in v0.0.2
CommandSet is a set of application subcommands and application level flags.
func (*CommandSet) Help ¶ added in v0.0.2
func (cs *CommandSet) Help(w io.Writer)
Help prints a usage line for the command set and a list of commands to the provided writer.
func (*CommandSet) Run ¶ added in v0.0.2
func (cs *CommandSet) Run(args ...string) error
Run attempts to run the command in the CommandSet that matches the first argument passed in. If no arguments are passed in, run prints help information to stdout. If the first argument does not match a command in the CommandSet, run prints the same help information to stderr.