Add usage example for required flags (#627)

* Add usage example for required flags

* Explain new behavior in MarkFlagRequired godocs
This commit is contained in:
Jake Dodd
2018-02-05 11:53:53 -08:00
committed by Eric Paris
parent eb58983359
commit c2bbfaa12d
2 changed files with 17 additions and 5 deletions

View File

@ -337,8 +337,8 @@ rootCmd.Flags().StringVarP(&Source, "source", "s", "", "Source directory to read
### Local Flag on Parent Commands
By default Cobra only parses local flags on the target command, any local flags on
parent commands are ignored. By enabling `Command.TraverseChildren` Cobra will
By default Cobra only parses local flags on the target command, any local flags on
parent commands are ignored. By enabling `Command.TraverseChildren` Cobra will
parse local flags on each command before executing the target command.
```go
@ -366,6 +366,15 @@ when the `--author` flag is not provided by user.
More in [viper documentation](https://github.com/spf13/viper#working-with-flags).
### Required flags
Flags are optional by default. If instead you wish your command to report an error
when a flag has not been set, mark it as required:
```go
rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
rootCmd.MarkFlagRequired("region")
```
## Positional and Custom Arguments
Validation of positional arguments can be specified using the `Args` field