Skip to content
Myr

Add usage example for required flags (#627)

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

* Add usage example for required flags * Explain new behavior in MarkFlagRequired godocs

authored by Jake Dodd · committed by Eric Paris · parent eb58983 · Browse files · 2 files+17 added−5 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+17 added−5 removed

  • other:+17 added−5 removed

2 files+17 added−5 removed

modified:bash_completions.go+6 added-3 removed
492 unmodified lines
493494 return c.GenBashCompletion(outFile)495}496497// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag, if it exists.497// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,498// and causes your command to report an error if invoked without the flag.499func (c *Command) MarkFlagRequired(name string) error {500 return MarkFlagRequired(c.Flags(), name)501}502502// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag, if it exists.503// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag if it exists,504// and causes your command to report an error if invoked without the flag.505func (c *Command) MarkPersistentFlagRequired(name string) error {506 return MarkFlagRequired(c.PersistentFlags(), name)507}508507// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.509// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,510// and causes your command to report an error if invoked without the flag.511func MarkFlagRequired(flags *pflag.FlagSet, name string) error {512 return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})513}514
29 unmodified lines
modified:README.md+11 added-2 removed
335 unmodified lines
336```337338### Local Flag on Parent Commands339340By default Cobra only parses local flags on the target command, any local flags on 341parent commands are ignored. By enabling `Command.TraverseChildren` Cobra will 340By default Cobra only parses local flags on the target command, any local flags on341parent commands are ignored. By enabling `Command.TraverseChildren` Cobra will342parse local flags on each command before executing the target command.343344```go345command := cobra.Command{
19 unmodified lines
365when the `--author` flag is not provided by user.366367More in [viper documentation](https://github.com/spf13/viper#working-with-flags).368369### Required flags370371Flags are optional by default. If instead you wish your command to report an error372when a flag has not been set, mark it as required:373```go374rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")375rootCmd.MarkFlagRequired("region")376```377378## Positional and Custom Arguments379380Validation of positional arguments can be specified using the `Args` field381of `Command`.
348 unmodified lines