package subcommands
import "github.com/google/subcommands"
Package subcommands implements a simple way for a single command to have many subcommands, each of which takes arguments and so forth.
Index ¶
- func ImportantFlag(name string)
- func Register(cmd Command, group string)
- type Command
- func Alias(alias string, cmd Command) Command
- func CommandsCommand() Command
- func FlagsCommand() Command
- func HelpCommand() Command
- type Commander
- func NewCommander(topLevelFlags *flag.FlagSet, name string) *Commander
- func (cdr *Commander) CommandsCommand() Command
- func (cdr *Commander) Execute(ctx context.Context, args ...interface{}) ExitStatus
- func (cdr *Commander) FlagsCommand() Command
- func (cdr *Commander) HelpCommand() Command
- func (cdr *Commander) ImportantFlag(name string)
- func (cdr *Commander) Register(cmd Command, group string)
- type ExitStatus
Functions ¶
func ImportantFlag ¶
func ImportantFlag(name string)
ImportantFlag marks a top-level flag as important, which means it will be printed out as part of the output of an ordinary "help" subcommand. (All flags, important or not, are printed by the "flags" subcommand.) It is a wrapper around DefaultCommander.ImportantFlag.
func Register ¶
Register adds a subcommand to the supported subcommands in the specified group. (Help output is sorted and arranged by group name.) The empty string is an acceptable group name; such subcommands are explained first before named groups. It is a wrapper around DefaultCommander.Register.
Types ¶
type Command ¶
type Command interface { // Name returns the name of the command. Name() string // Synopsis returns a short string (less than one line) describing the command. Synopsis() string // Usage returns a long string explaining the command and giving usage // information. Usage() string // SetFlags adds the flags for this command to the specified set. SetFlags(*flag.FlagSet) // Execute executes the command and returns an ExitStatus. Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) ExitStatus }
A Command represents a single command.
func Alias ¶
Alias returns a Command alias which implements a "commands" subcommand.
func CommandsCommand ¶
func CommandsCommand() Command
CommandsCommand returns Command which implements a "commands" subcommand.
func FlagsCommand ¶
func FlagsCommand() Command
FlagsCommand returns a Command which implements "flags" for the DefaultCommander. Use Register(FlagsCommand(), <group>) for it to be recognized.
func HelpCommand ¶
func HelpCommand() Command
HelpCommand returns a Command which implements "help" for the DefaultCommander. Use Register(HelpCommand(), <group>) for it to be recognized.
type Commander ¶
type Commander struct { Output io.Writer // Output specifies where the commander should write its output (default: os.Stdout). Error io.Writer // Error specifies where the commander should write its error (default: os.Stderr). // contains filtered or unexported fields }
A Commander represents a set of commands.
var DefaultCommander *Commander
DefaultCommander is the default commander using flag.CommandLine for flags and os.Args[0] for the command name.
func NewCommander ¶
NewCommander returns a new commander with the specified top-level flags and command name. The Usage function for the topLevelFlags will be set as well.
func (*Commander) CommandsCommand ¶
CommandsCommand returns Command which implements a "commands" subcommand.
func (*Commander) Execute ¶
func (cdr *Commander) Execute(ctx context.Context, args ...interface{}) ExitStatus
Execute should be called once the top-level-flags on a Commander have been initialized. It finds the correct subcommand and executes it, and returns an ExitStatus with the result. On a usage error, an appropriate message is printed to os.Stderr, and ExitUsageError is returned. The additional args are provided as-is to the Execute method of the selected Command.
func (*Commander) FlagsCommand ¶
FlagsCommand returns a Command which implements a "flags" subcommand.
func (*Commander) HelpCommand ¶
HelpCommand returns a Command which implements a "help" subcommand.
func (*Commander) ImportantFlag ¶
ImportantFlag marks a top-level flag as important, which means it will be printed out as part of the output of an ordinary "help" subcommand. (All flags, important or not, are printed by the "flags" subcommand.)
func (*Commander) Register ¶
Register adds a subcommand to the supported subcommands in the specified group. (Help output is sorted and arranged by group name.) The empty string is an acceptable group name; such subcommands are explained first before named groups.
type ExitStatus ¶
type ExitStatus int
An ExitStatus represents a Posix exit status that a subcommand expects to be returned to the shell.
const ( ExitSuccess ExitStatus = iota ExitFailure ExitUsageError )
func Execute ¶
func Execute(ctx context.Context, args ...interface{}) ExitStatus
Execute should be called once the default flags have been initialized by flag.Parse. It finds the correct subcommand and executes it, and returns an ExitStatus with the result. On a usage error, an appropriate message is printed to os.Stderr, and ExitUsageError is returned. The additional args are provided as-is to the Execute method of the selected Command. It is a wrapper around DefaultCommander.Execute.
Source Files ¶
- Version
- v1.0.1
- Published
- Feb 11, 2019
- Platform
- windows/amd64
- Imports
- 8 packages
- Last checked
- 1 week ago –
Tools for package owners.