mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Update documentation
This commit is contained in:
parent
8bcacfe133
commit
1723331773
2 changed files with 8 additions and 40 deletions
43
README.md
43
README.md
|
@ -117,14 +117,6 @@ Flag functionality is provided by the [pflag
|
||||||
library](https://github.com/spf13/pflag), a fork of the flag standard library
|
library](https://github.com/spf13/pflag), a fork of the flag standard library
|
||||||
which maintains the same interface while adding POSIX compliance.
|
which maintains the same interface while adding POSIX compliance.
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Cobra works by creating a set of commands and then organizing them into a tree.
|
|
||||||
The tree defines the structure of the application.
|
|
||||||
|
|
||||||
Once each command is defined with its corresponding flags, then the
|
|
||||||
tree is assigned to the commander which is finally executed.
|
|
||||||
|
|
||||||
# Installing
|
# Installing
|
||||||
Using Cobra is easy. First, use `go get` to install the latest version
|
Using Cobra is easy. First, use `go get` to install the latest version
|
||||||
of the library. This command will install the `cobra` generator executable
|
of the library. This command will install the `cobra` generator executable
|
||||||
|
@ -419,19 +411,6 @@ root, but commands can be attached at any level.
|
||||||
RootCmd.AddCommand(versionCmd)
|
RootCmd.AddCommand(versionCmd)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Remove a command from its parent
|
|
||||||
|
|
||||||
Removing a command is not a common action in simple programs, but it allows 3rd
|
|
||||||
parties to customize an existing command tree.
|
|
||||||
|
|
||||||
In this example, we remove the existing `VersionCmd` command of an existing
|
|
||||||
root command, and we replace it with our own version:
|
|
||||||
|
|
||||||
```go
|
|
||||||
mainlib.RootCmd.RemoveCommand(mainlib.VersionCmd)
|
|
||||||
mainlib.RootCmd.AddCommand(versionCmd)
|
|
||||||
```
|
|
||||||
|
|
||||||
## Working with Flags
|
## Working with Flags
|
||||||
|
|
||||||
Flags provide modifiers to control how the action command operates.
|
Flags provide modifiers to control how the action command operates.
|
||||||
|
@ -687,9 +666,7 @@ You can provide your own command, function or template through the following met
|
||||||
|
|
||||||
```go
|
```go
|
||||||
command.SetHelpCommand(cmd *Command)
|
command.SetHelpCommand(cmd *Command)
|
||||||
|
|
||||||
command.SetHelpFunc(f func(*Command, []string))
|
command.SetHelpFunc(f func(*Command, []string))
|
||||||
|
|
||||||
command.SetHelpTemplate(s string)
|
command.SetHelpTemplate(s string)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -831,10 +808,10 @@ func main() {
|
||||||
rootCmd.AddCommand(subCmd)
|
rootCmd.AddCommand(subCmd)
|
||||||
|
|
||||||
rootCmd.SetArgs([]string{""})
|
rootCmd.SetArgs([]string{""})
|
||||||
_ = rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
fmt.Print("\n")
|
fmt.Println()
|
||||||
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
|
rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})
|
||||||
_ = rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -871,8 +848,8 @@ func main() {
|
||||||
Use: "hugo",
|
Use: "hugo",
|
||||||
Short: "Hugo is a very fast static site generator",
|
Short: "Hugo is a very fast static site generator",
|
||||||
Long: `A Fast and Flexible Static Site Generator built with
|
Long: `A Fast and Flexible Static Site Generator built with
|
||||||
love by spf13 and friends in Go.
|
love by spf13 and friends in Go.
|
||||||
Complete documentation is available at http://hugo.spf13.com`,
|
Complete documentation is available at http://hugo.spf13.com`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
// Do Stuff Here
|
// Do Stuff Here
|
||||||
return errors.New("some random error")
|
return errors.New("some random error")
|
||||||
|
@ -937,16 +914,6 @@ Cobra can generate a man page based on the subcommands, flags, etc. A simple exa
|
||||||
|
|
||||||
Cobra 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).
|
Cobra 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).
|
||||||
|
|
||||||
## Debugging
|
|
||||||
|
|
||||||
Cobra provides a ‘DebugFlags’ method on a command which, when called, will print
|
|
||||||
out everything Cobra knows about the flags for each command.
|
|
||||||
|
|
||||||
### Example
|
|
||||||
|
|
||||||
```go
|
|
||||||
command.DebugFlags()
|
|
||||||
```
|
|
||||||
|
|
||||||
## Extensions
|
## Extensions
|
||||||
|
|
||||||
|
|
|
@ -54,13 +54,14 @@ type Command struct {
|
||||||
// ValidArgs is list of all valid non-flag arguments that are accepted in bash completions
|
// ValidArgs is list of all valid non-flag arguments that are accepted in bash completions
|
||||||
ValidArgs []string
|
ValidArgs []string
|
||||||
|
|
||||||
|
// Expected arguments
|
||||||
|
Args PositionalArgs
|
||||||
|
|
||||||
// ArgAliases is List of aliases for ValidArgs.
|
// ArgAliases is List of aliases for ValidArgs.
|
||||||
// These are not suggested to the user in the bash completion,
|
// These are not suggested to the user in the bash completion,
|
||||||
// but accepted if entered manually.
|
// but accepted if entered manually.
|
||||||
ArgAliases []string
|
ArgAliases []string
|
||||||
|
|
||||||
// Expected arguments
|
|
||||||
Args PositionalArgs
|
|
||||||
// BashCompletionFunction is custom functions used by the bash autocompletion generator.
|
// BashCompletionFunction is custom functions used by the bash autocompletion generator.
|
||||||
BashCompletionFunction string
|
BashCompletionFunction string
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue