]>
Commit | Line | Data |
---|---|---|
15c0b25d AP |
1 | package cli |
2 | ||
3 | import ( | |
4 | "github.com/posener/complete" | |
5 | ) | |
6 | ||
7 | const ( | |
8 | // RunResultHelp is a value that can be returned from Run to signal | |
9 | // to the CLI to render the help output. | |
10 | RunResultHelp = -18511 | |
11 | ) | |
12 | ||
13 | // A command is a runnable sub-command of a CLI. | |
14 | type Command interface { | |
15 | // Help should return long-form help text that includes the command-line | |
16 | // usage, a brief few sentences explaining the function of the command, | |
17 | // and the complete list of flags the command accepts. | |
18 | Help() string | |
19 | ||
20 | // Run should run the actual command with the given CLI instance and | |
21 | // command-line arguments. It should return the exit status when it is | |
22 | // finished. | |
23 | // | |
24 | // There are a handful of special exit codes this can return documented | |
25 | // above that change behavior. | |
26 | Run(args []string) int | |
27 | ||
28 | // Synopsis should return a one-line, short synopsis of the command. | |
29 | // This should be less than 50 characters ideally. | |
30 | Synopsis() string | |
31 | } | |
32 | ||
33 | // CommandAutocomplete is an extension of Command that enables fine-grained | |
34 | // autocompletion. Subcommand autocompletion will work even if this interface | |
35 | // is not implemented. By implementing this interface, more advanced | |
36 | // autocompletion is enabled. | |
37 | type CommandAutocomplete interface { | |
38 | // AutocompleteArgs returns the argument predictor for this command. | |
39 | // If argument completion is not supported, this should return | |
40 | // complete.PredictNothing. | |
41 | AutocompleteArgs() complete.Predictor | |
42 | ||
43 | // AutocompleteFlags returns a mapping of supported flags and autocomplete | |
44 | // options for this command. The map key for the Flags map should be the | |
45 | // complete flag such as "-foo" or "--foo". | |
46 | AutocompleteFlags() complete.Flags | |
47 | } | |
48 | ||
49 | // CommandHelpTemplate is an extension of Command that also has a function | |
50 | // for returning a template for the help rather than the help itself. In | |
51 | // this scenario, both Help and HelpTemplate should be implemented. | |
52 | // | |
53 | // If CommandHelpTemplate isn't implemented, the Help is output as-is. | |
54 | type CommandHelpTemplate interface { | |
55 | // HelpTemplate is the template in text/template format to use for | |
56 | // displaying the Help. The keys available are: | |
57 | // | |
58 | // * ".Help" - The help text itself | |
59 | // * ".Subcommands" | |
60 | // | |
61 | HelpTemplate() string | |
62 | } | |
63 | ||
64 | // CommandFactory is a type of function that is a factory for commands. | |
65 | // We need a factory because we may need to setup some state on the | |
66 | // struct that implements the command itself. | |
67 | type CommandFactory func() (Command, error) |