{"record":{"id":"1c1a613d55382fe3","repo":"urfave/cli","slug":"can-t-duplicate-this-flag","errorCode":null,"errorMessage":"can't duplicate this flag","messagePattern":"can't duplicate this flag","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_bool_with_inverse.go","lineNumber":119,"sourceCode":"\t\t\t\tval = \"false\"\n\t\t\t}\n\t\t\tif err := bif.Set(bif.Name, val); err != nil {\n\t\t\t\treturn fmt.Errorf(\n\t\t\t\t\t\"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s\",\n\t\t\t\t\tval, bif.Value, source, bif.Name, err,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tbif.hasBeenSet = true\n\t\t}\n\t}\n\n\treturn nil\n}\n\nfunc (bif *BoolWithInverseFlag) Set(name, val string) error {\n\tif bif.count > 0 && bif.OnlyOnce {\n\t\treturn fmt.Errorf(\"can't duplicate this flag\")\n\t}\n\n\tbif.hasBeenSet = true\n\n\tif slices.Contains(append([]string{bif.Name}, bif.Aliases...), name) {\n\t\tif bif.nset {\n\t\t\treturn fmt.Errorf(\"cannot set both flags `--%s` and `--%s`\", bif.Name, bif.inversePrefix()+bif.Name)\n\t\t}\n\t\tif err := bif.value.Set(val); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tbif.pset = true\n\t} else {\n\t\tif bif.pset {\n\t\t\treturn fmt.Errorf(\"cannot set both flags `--%s` and `--%s`\", bif.Name, bif.inversePrefix()+bif.Name)\n\t\t}\n\t\tif err := bif.value.Set(\"false\"); err != nil {\n\t\t\treturn err","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/urfave/cli/blob/1a4deb4f5a35ee12706602698dc0527d44c14b19/flag_bool_with_inverse.go#L101-L137","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$ app --verbose --no-verbose  // duplicate\n// after\n$ app --verbose  // one occurrence only","handlingStrategy":"validation","validationCode":"count := 0\nfor _, a := range os.Args[1:] {\n    if a == \"--verbose\" || a == \"--no-verbose\" || a == \"-v\" { count++ }\n}\nif count > 1 { return errors.New(\"flag given multiple times and OnlyOnce is set\") }","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(os.Args); err != nil {\n    if strings.Contains(err.Error(), \"can't duplicate this flag\") {\n        fmt.Fprintln(os.Stderr, err, \"\\nNote: env/config sources also count as one occurrence\")\n        os.Exit(2)\n    }\n    return err\n}","preventionTips":["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"],"tags":["go","cli","flags","duplicate"],"backgroundTag":"flag-specified-multiple-times","analyzedSha":"1a4deb4f5a35ee12706602698dc0527d44c14b19","analyzedAt":"2026-08-31T18:42:09.451Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}