Skip to content

Commit 0e32194

Browse files
Add documentation for repeated flags functionality
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) Co-Authored-By: Claude <[email protected]>
1 parent 7da941c commit 0e32194

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

site/content/user_guide.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,73 @@ In these cases:
366366
- a flag may appear in multiple groups
367367
- a group may contain any number of flags
368368

369+
### Repeated Flags
370+
371+
Cobra supports two types of repeated flags, useful for implementing SSH-like verbose flags (`-v`, `-vv`, `-vvv`) or collecting multiple values.
372+
373+
#### Count Flags
374+
375+
For implementing verbose-style flags where repeated usage increases a counter (like SSH's `-v`, `-vv`, `-vvv`):
376+
377+
```go
378+
var verbose int
379+
380+
func init() {
381+
// CountVarP allows the flag to be repeated to increment the counter
382+
rootCmd.PersistentFlags().CountVarP(&verbose, "verbose", "v", "verbose output (can be repeated: -v, -vv, -vvv)")
383+
}
384+
```
385+
386+
Usage 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)
390+
391+
Then in your command logic:
392+
```go
393+
Run: func(cmd *cobra.Command, args []string) {
394+
switch verbose {
395+
case 0:
396+
// Default: no verbose output
397+
case 1:
398+
// Info level logging
399+
log.SetLevel(log.InfoLevel)
400+
case 2:
401+
// Debug level logging
402+
log.SetLevel(log.DebugLevel)
403+
case 3:
404+
// Trace level logging
405+
log.SetLevel(log.TraceLevel)
406+
default:
407+
// Maximum verbosity
408+
log.SetLevel(log.TraceLevel)
409+
}
410+
},
411+
```
412+
413+
#### Array/Slice Flags
414+
415+
For collecting multiple values of the same flag:
416+
417+
```go
418+
var inputFiles []string
419+
420+
func init() {
421+
// StringArrayVarP allows multiple values: --input file1.txt --input file2.txt
422+
rootCmd.Flags().StringArrayVarP(&inputFiles, "input", "i", []string{}, "input files (can be repeated)")
423+
424+
// Alternative: StringSliceVarP for comma-separated values
425+
// rootCmd.Flags().StringSliceVarP(&inputFiles, "input", "i", []string{}, "input files (comma-separated or repeated)")
426+
}
427+
```
428+
429+
Usage 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`
433+
434+
**Note**: Both `CountVar` and array flags leverage the underlying [pflag](https://github.com/spf13/pflag) library's support for repeated flags.
435+
369436
## Positional and Custom Arguments
370437

371438
Validation of positional arguments can be specified using the `Args` field of `Command`.

0 commit comments

Comments
 (0)