Skip to content

Commit

Permalink
Update README.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
  • Loading branch information
dnephin committed Sep 16, 2016
1 parent 6d5851f commit 631cea0
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,43 +412,36 @@ 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")
```

### Positional Arguments

Validation of positional arguments can be specified using the `Args` field, which accepts
one of the following values:

- `NoArgs` - the command will report an error if there are any positional args.
- `ArbitraryArgs` - the command will accept any args.
- `OnlyValidArgs` - the command will report an error if there are any positiona
args that are not in the `ValidArgs` list.
- `MinimumNArgs(int)` - the command will report an error if there are not at
least N positional args.
- `MaximumNArgs(int)` - the command will report an error if there are more than
N positional args.
- `ExactArgs(int)` - the command will report an error if there are not
exactly N positional args.
- `RangeArgs(min, max)` - the command will report an error if the number of args
is not between the minimum and maximum number of expected args.

By default, `Args` uses the following legacy behaviour:
- 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
### Positional and Custom Arguments

Validation of positional arguments can be specified using the `Args` field. A
custom validator can be provided like this:

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

Args: func validColorArgs(cmd *cobra.Command, args []string) error {
if err := cli.RequiresMinArgs(1)(cmd, args); err != nil {
return err
}
if myapp.IsValidColor(args[0]) {
return nil
}
return fmt.Errorf("Invalid color specified: %s", args[0])
}

```

The follow validators are built in:

- NoArgs - the command will report an error if there are any positional args.
- ArbitraryArgs - the command will accept any args.
- OnlyValidArgs - the command will report an error if there are any positiona args that are not in the ValidArgs list.
- MinimumNArgs(int) - the command will report an error if there are not at least N positional args.
- MaximumNArgs(int) - the command will report an error if there are more than N positional args.
- ExactArgs(int) - the command will report an error if there are not exactly N positional args.
- RangeArgs(min, max) - the command will report an error if the number of args is not between the minimum and maximum number of expected args.


## Example

In the example below, we have defined three commands. Two are at the top level
Expand Down

0 comments on commit 631cea0

Please sign in to comment.