Skip to content

Commit

Permalink
Fix some typos in README and comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed May 7, 2016
1 parent 1595299 commit 88bd5f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,36 +406,38 @@ A flag can also be assigned locally which will only apply to that specific comma
RootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read from")
```

### Specify if you command takes arguments
### Expected Arguments

Expected arguments can be specified using the `TakesArgs` field, which accepts
one of the following values:

There are multiple options for how a command can handle unknown arguments which can be set in `TakesArgs`
- `Legacy`
- `None`
- `Arbitrary`
- `ValidOnly`

`Legacy` (or default) the rules are as follows:
`Legacy` (the default):
- root commands with no subcommands can take arbitrary arguments
- root commands with subcommands will do subcommand validity checking
- subcommands will always accept arbitrary arguments and do no subsubcommand validity checking

`None` the command will be rejected if there are any left over arguments after parsing flags.
`None` - the command will be rejected if there are any left over arguments after parsing flags.

`Arbitrary` any additional values left after parsing flags will be passed in to your `Run` function.
`Arbitrary` - any additional values left after parsing flags will be passed to the `Run` function.

`ValidOnly` you must define all valid (non-subcommand) arguments to your command. These are defined in a slice name ValidArgs. For example a command which only takes the argument "one" or "two" would be defined as:
`ValidOnly` - all valid (non-subcommand) arguments must be defined in the `ValidArgs` field. For example a command which only takes the argument "one" or "two" would be defined as:

```go
var HugoCmd = &cobra.Command{
Use: "hugo",
Short: "Hugo is a very fast static site generator",
ValidArgs: []string{"one", "two", "three", "four"}
TakesArgs: cobra.ValidOnly
Run: func(cmd *cobra.Command, args []string) {
// args will only have the values one, two, three, four
// or the cmd.Execute() will fail.
},
}
Use: "hugo",
Short: "Hugo is a very fast static site generator",
ValidArgs: []string{"one", "two"}
TakesArgs: cobra.ValidOnly
Run: func(cmd *cobra.Command, args []string) {
// args will only have the values one, two
// or the cmd.Execute() will fail.
},
}
```

## Example
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Command struct {
// List of aliases for ValidArgs. These are not suggested to the user in the bash
// completion, but accepted if entered manually.
ArgAliases []string
// Does this command take arbitrary arguments
// Expected arguments
TakesArgs Args
// Custom functions used by the bash autocompletion generator
BashCompletionFunction string
Expand Down

0 comments on commit 88bd5f7

Please sign in to comment.