command.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package flags
  2. // Command represents an application command. Commands can be added to the
  3. // parser (which itself is a command) and are selected/executed when its name
  4. // is specified on the command line. The Command type embeds a Group and
  5. // therefore also carries a set of command specific options.
  6. type Command struct {
  7. // Embedded, see Group for more information
  8. *Group
  9. // The name by which the command can be invoked
  10. Name string
  11. // The active sub command (set by parsing) or nil
  12. Active *Command
  13. commands []*Command
  14. hasBuiltinHelpGroup bool
  15. }
  16. // Commander is an interface which can be implemented by any command added in
  17. // the options. When implemented, the Execute method will be called for the last
  18. // specified (sub)command providing the remaining command line arguments.
  19. type Commander interface {
  20. // Execute will be called for the last active (sub)command. The
  21. // args argument contains the remaining command line arguments. The
  22. // error that Execute returns will be eventually passed out of the
  23. // Parse method of the Parser.
  24. Execute(args []string) error
  25. }
  26. // Usage is an interface which can be implemented to show a custom usage string
  27. // in the help message shown for a command.
  28. type Usage interface {
  29. // Usage is called for commands to allow customized printing of command
  30. // usage in the generated help message.
  31. Usage() string
  32. }
  33. // AddCommand adds a new command to the parser with the given name and data. The
  34. // data needs to be a pointer to a struct from which the fields indicate which
  35. // options are in the command. The provided data can implement the Command and
  36. // Usage interfaces.
  37. func (c *Command) AddCommand(command string, shortDescription string, longDescription string, data interface{}) (*Command, error) {
  38. cmd := newCommand(command, shortDescription, longDescription, data)
  39. if err := cmd.scan(); err != nil {
  40. return nil, err
  41. }
  42. c.commands = append(c.commands, cmd)
  43. return cmd, nil
  44. }
  45. // AddGroup adds a new group to the command with the given name and data. The
  46. // data needs to be a pointer to a struct from which the fields indicate which
  47. // options are in the group.
  48. func (c *Command) AddGroup(shortDescription string, longDescription string, data interface{}) (*Group, error) {
  49. group := newGroup(shortDescription, longDescription, data)
  50. if err := group.scanType(c.scanSubCommandHandler(group)); err != nil {
  51. return nil, err
  52. }
  53. c.groups = append(c.groups, group)
  54. return group, nil
  55. }
  56. // Commands returns a list of subcommands of this command.
  57. func (c *Command) Commands() []*Command {
  58. return c.commands
  59. }
  60. // Find locates the subcommand with the given name and returns it. If no such
  61. // command can be found Find will return nil.
  62. func (c *Command) Find(name string) *Command {
  63. for _, cc := range c.commands {
  64. if cc.Name == name {
  65. return cc
  66. }
  67. }
  68. return nil
  69. }