Skip to content
Myr

Add documentation for repeated flags functionality (#2316)

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

Resolves #2298 - Added comprehensive "Repeated Flags" section to user guide - Documented CountVarP for SSH-style verbose flags (-v, -vv, -vvv) - Documented StringArrayVarP for multiple value collection - Provided complete code examples and usage patterns 🤖 Generated with [Claude Code](https://claude.com/claude-code)

authored by Ron Vergis · committed by GitHub · parent 0629892 · Browse files · 1 file+67 added−0 removed
This commit

State

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

Trailers

  • Co-authored-by: Vergis_Ron <Vergis_Ron@bah.com>
  • Co-authored-by: Claude <noreply@anthropic.com>

Diff stats

1 file+67 added−0 removed

  • other:+67 added−0 removed

1 file+67 added−0 removed

modified:site/content/user_guide.md+67 added
364 unmodified lines
365 - **NOTE:** the group is only enforced on commands where every flag is defined366 - a flag may appear in multiple groups367 - a group may contain any number of flags368369### Repeated Flags370371Cobra supports two types of repeated flags, useful for implementing SSH-like verbose flags (`-v`, `-vv`, `-vvv`) or collecting multiple values.372373#### Count Flags374375For implementing verbose-style flags where repeated usage increases a counter (like SSH's `-v`, `-vv`, `-vvv`):376377```go378var verbose int379380func init() {381 // CountVarP allows the flag to be repeated to increment the counter382 rootCmd.PersistentFlags().CountVarP(&verbose, "verbose", "v", "verbose output (can be repeated: -v, -vv, -vvv)")383}384```385386Usage examples:387- `myapp -v` → verbose = 1 (info level)388- `myapp -vv` or `myapp -v -v` → verbose = 2 (debug level)389- `myapp -vvv` or `myapp -v -v -v` → verbose = 3 (trace level)390391Then in your command logic:392```go393Run: func(cmd *cobra.Command, args []string) {394 switch verbose {395 case 0:396 // Default: no verbose output397 case 1:398 // Info level logging399 log.SetLevel(log.InfoLevel)400 case 2:401 // Debug level logging402 log.SetLevel(log.DebugLevel)403 case 3:404 // Trace level logging405 log.SetLevel(log.TraceLevel)406 default:407 // Maximum verbosity408 log.SetLevel(log.TraceLevel)409 }410},411```412413#### Array/Slice Flags414415For collecting multiple values of the same flag:416417```go418var inputFiles []string419420func init() {421 // StringArrayVarP allows multiple values: --input file1.txt --input file2.txt422 rootCmd.Flags().StringArrayVarP(&inputFiles, "input", "i", []string{}, "input files (can be repeated)")423424 // Alternative: StringSliceVarP for comma-separated values425 // rootCmd.Flags().StringSliceVarP(&inputFiles, "input", "i", []string{}, "input files (comma-separated or repeated)")426}427```428429Usage examples:430- `myapp --input file1.txt --input file2.txt`431- `myapp -i file1.txt -i file2.txt`432- With StringSlice: `myapp --input file1.txt,file2.txt,file3.txt`433434**Note**: Both `CountVar` and array flags leverage the underlying [pflag](https://github.com/spf13/pflag) library's support for repeated flags.435436## Positional and Custom Arguments437438Validation of positional arguments can be specified using the `Args` field of `Command`.439The following validators are built in:
447 unmodified lines