Skip to content
Myr

Update documentation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 8bcacfe · Browse files · 2 files+8 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−40 removed

  • other:+8 added−40 removed

2 files+8 added−40 removed

modified:command.go+3 added-2 removed
52 unmodified lines
5354 // ValidArgs is list of all valid non-flag arguments that are accepted in bash completions55 ValidArgs []string5657 // Expected arguments58 Args PositionalArgs5960 // ArgAliases is List of aliases for ValidArgs.61 // These are not suggested to the user in the bash completion,62 // but accepted if entered manually.63 ArgAliases []string6462 // Expected arguments63 Args PositionalArgs65 // BashCompletionFunction is custom functions used by the bash autocompletion generator.66 BashCompletionFunction string6768 // Deprecated defines, if this command is deprecated and should print this string when used.
1238 unmodified lines
modified:README.md+5 added-38 removed
115 unmodified lines
116Flag functionality is provided by the [pflag117library](https://github.com/spf13/pflag), a fork of the flag standard library118which maintains the same interface while adding POSIX compliance.119120## Usage121122Cobra works by creating a set of commands and then organizing them into a tree.123The tree defines the structure of the application.124125Once each command is defined with its corresponding flags, then the126tree is assigned to the commander which is finally executed.127120# Installing121Using Cobra is easy. First, use `go get` to install the latest version122of the library. This command will install the `cobra` generator executable123along with the library and its dependencies:
286 unmodified lines
410```go411RootCmd.AddCommand(versionCmd)412```413422### Remove a command from its parent423424Removing a command is not a common action in simple programs, but it allows 3rd425parties to customize an existing command tree.426427In this example, we remove the existing `VersionCmd` command of an existing428root command, and we replace it with our own version:429430```go431mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)432mainlib.RootCmd.AddCommand(versionCmd)433```434414## Working with Flags415416Flags provide modifiers to control how the action command operates.417
247 unmodified lines
665You can provide your own command, function or template through the following methods:666667```go668command.SetHelpCommand(cmd *Command)690669command.SetHelpFunc(f func(*Command, []string))692670command.SetHelpTemplate(s string)671```672673The latter two will also apply to any children commands.
133 unmodified lines
807808 rootCmd.AddCommand(subCmd)809810 rootCmd.SetArgs([]string{""})834 _ = rootCmd.Execute()835 fmt.Print("\n")811 rootCmd.Execute()812 fmt.Println()813 rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})837 _ = rootCmd.Execute()814 rootCmd.Execute()815}816```817818
28 unmodified lines
847 var rootCmd = &cobra.Command{848 Use: "hugo",849 Short: "Hugo is a very fast static site generator",850 Long: `A Fast and Flexible Static Site Generator built with874 love by spf13 and friends in Go.875 Complete documentation is available at http://hugo.spf13.com`,851love by spf13 and friends in Go.852Complete documentation is available at http://hugo.spf13.com`,853 RunE: func(cmd *cobra.Command, args []string) error {854 // Do Stuff Here855 return errors.New("some random error")856 },
56 unmodified lines
913## Generating bash completions for your command914915Cobra can generate a bash-completion file. If you add more information to your command, these completions can be amazingly powerful and flexible. Read more about it in [Bash Completions](bash_completions.md).916940## Debugging917942Cobra provides a ‘DebugFlags’ method on a command which, when called, will print943out everything Cobra knows about the flags for each command.944945### Example946947```go948command.DebugFlags()949```950918## Extensions919920Libraries for extending Cobra:921
21 unmodified lines