-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Open
Description
Hello, team Cobra,
I got a request to make a CLI reject repeated flags (eg. ./cli --name test --name test2 or ./cli -f -f).
I found two ways of doing this.
- PersistentPreRunE with raw arguments validation
- Defining custom flag types that will error out on multiple Set calls.
I went with #2:
func (sb *singleBool) Set(value string) error {
err := sb.occurrenceCounter.Check()
if err != nil {
return err
}
sb.value, err = strconv.ParseBool(value)
return err
}
but it requires me to define full type for every type of flag used (6 files each ~60 lines). It's not terrible but it could be simple setting for the cobra itself instead of 360 lines of code.
Would it be possible to add feature flag allowing flags to be set only once? Or is there any simpler way I have misseed?
Thank you.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels