urfave/cli · error
can't duplicate this flag
Error message
can't duplicate this flag
What it means
BoolWithInverseFlag.Set enforces the OnlyOnce option: if the flag (or its inverse alias) has already been set (count > 0) and OnlyOnce is true, further Set calls return 'can't duplicate this flag'. PostParse invokes Set for sourced values, so a source plus a command-line occurrence can collide.
Source
Thrown at flag_bool_with_inverse.go:119
val = "false"
}
if err := bif.Set(bif.Name, val); err != nil {
return fmt.Errorf(
"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s",
val, bif.Value, source, bif.Name, err,
)
}
bif.hasBeenSet = true
}
}
return nil
}
func (bif *BoolWithInverseFlag) Set(name, val string) error {
if bif.count > 0 && bif.OnlyOnce {
return fmt.Errorf("can't duplicate this flag")
}
bif.hasBeenSet = true
if slices.Contains(append([]string{bif.Name}, bif.Aliases...), name) {
if bif.nset {
return fmt.Errorf("cannot set both flags `--%s` and `--%s`", bif.Name, bif.inversePrefix()+bif.Name)
}
if err := bif.value.Set(val); err != nil {
return err
}
bif.pset = true
} else {
if bif.pset {
return fmt.Errorf("cannot set both flags `--%s` and `--%s`", bif.Name, bif.inversePrefix()+bif.Name)
}
if err := bif.value.Set("false"); err != nil {
return errView on GitHub (pinned to 1a4deb4f5a)
Solutions
- Remove the duplicate occurrence, keeping one of the flag or its inverse
- Set OnlyOnce to false on the flag definition if repetition should be allowed
- Unset the mapped env/config source so only the explicit CLI value applies
Example fix
// before $ app --verbose --no-verbose // duplicate // after $ app --verbose // one occurrence only
Defensive patterns
Strategy: validation
Validate before calling
count := 0
for _, a := range os.Args[1:] {
if a == "--verbose" || a == "--no-verbose" || a == "-v" { count++ }
}
if count > 1 { return errors.New("flag given multiple times and OnlyOnce is set") } Try / catch
if err := cmd.Run(os.Args); err != nil {
if strings.Contains(err.Error(), "can't duplicate this flag") {
fmt.Fprintln(os.Stderr, err, "\nNote: env/config sources also count as one occurrence")
os.Exit(2)
}
return err
} Prevention
- Pass each OnlyOnce flag at most once, including via env/config Sources
- Set OnlyOnce:false if repeated use should be permitted
- Unset conflicting env vars in scripts that also pass the flag explicitly
When it happens
Trigger: Specifying the flag (or its inverse alias) twice on the command line when OnlyOnce is set; a value from Sources (env/config) combined with an explicit CLI occurrence also triggers it during PostParse.
Common situations: Shell scripts appending flags that the user also typed; alias and canonical name both present (e.g. --verbose and --no-verbose); env var mapped to the flag plus explicit CLI use after an upgrade to OnlyOnce semantics.
Related errors
- no such flag -%s
- flag needs an argument: %s
- flag provided but not defined: -%s
- could not parse %[1]q as %[2]T value from %[3]s for flag %[4
- cannot set both flags `--%s` and `--%s`
AI-assisted analysis of urfave/cli@1a4deb4f5a (2026-08-31).
Data as JSON: /api/errors/1c1a613d55382fe3.
Report an issue: GitHub.