{"record":{"id":"616f79159fe0e1ce","repo":"urfave/cli","slug":"can-t-duplicate-this-flag-616f79","errorCode":null,"errorMessage":"can't duplicate this flag","messagePattern":"can't duplicate this flag","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"flag_impl.go","lineNumber":205,"sourceCode":"// Set applies given value from string\nfunc (f *FlagBase[T, C, V]) Set(_ string, val string) error {\n\ttracef(\"apply (flag=%[1]q)\", f.Name)\n\n\t// TODO move this phase into a separate flag initialization function\n\t// if flag has been applied previously then it would have already been set\n\t// from env or file. So no need to apply the env set again. However\n\t// lots of units tests prior to persistent flags assumed that the\n\t// flag can be applied to different flag sets multiple times while still\n\t// keeping the env set.\n\tif !f.applied {\n\t\tif err := f.PreParse(); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tf.applied = true\n\t}\n\n\tif f.count == 1 && f.OnlyOnce {\n\t\treturn fmt.Errorf(\"can't duplicate this flag\")\n\t}\n\n\tf.count++\n\tif err := f.value.Set(val); err != nil {\n\t\treturn err\n\t}\n\tf.hasBeenSet = true\n\tif f.Validator != nil {\n\t\tif err := f.Validator(f.value.Get().(T)); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc (f *FlagBase[T, C, V]) Get() any {\n\tif f.value != nil {\n\t\treturn f.value.Get()","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/urfave/cli/blob/1a4deb4f5a35ee12706602698dc0527d44c14b19/flag_impl.go#L187-L223","documentation":"flag_impl.go's Set enforces the OnlyOnce option: when a flag has OnlyOnce set to true and it is being applied for the second time (count == 1), it returns \"can't duplicate this flag\". Normally repeated flags accumulate (count-based values), but OnlyOnce forbids specifying the same flag more than once.","triggerScenarios":"A flag created with OnlyOnce: true is supplied two or more times, e.g. `myapp --tag a --tag b`, or the flag is set once from an external source and again on the command line so Set runs a second time.","commonSituations":"Users used to repeatable flags (like docker -v) applying them to a flag marked OnlyOnce; scripts that append flags in a loop without deduplication; env var + CLI argument both supplying the same OnlyOnce flag.","solutions":["Remove the duplicate occurrence so the flag appears at most once on the command line.","If repetition is intended, set OnlyOnce: false (the default) on the flag definition so subsequent Set calls accumulate instead of erroring.","Deduplicate flags in generating scripts (e.g. use arrays + sort -u before joining into a command line).","Check Sources/env to ensure the flag is not being set from two places when OnlyOnce is enabled."],"exampleFix":"// flag definition\n&cli.StringFlag{Name: \"tag\", OnlyOnce: true}\n// before\nmyapp --tag a --tag b\n// after\nmyapp --tag a   # or set OnlyOnce: false to allow repeats","handlingStrategy":"validation","validationCode":"// dedupe repeated flags before invoking\nargs=(\"$@\")\nmapfile -t deduped < <(printf '%s\\n' \"${args[@]}\" | awk '!seen[$0]++')\n# pass \"${deduped[@]}\" to the command when OnlyOnce flags are in play","typeGuard":null,"tryCatchPattern":"if err := cmd.Run(ctx, os.Args); err != nil {\n    if strings.Contains(err.Error(), \"can't duplicate this flag\") {\n        fmt.Fprintln(os.Stderr, \"this flag may only be specified once\")\n        os.Exit(2)\n    }\n    return err\n}","preventionTips":["Pass OnlyOnce flags at most once per command line.","Set OnlyOnce: false (omit it) if the flag should be repeatable.","Deduplicate flags in scripts that build command lines dynamically.","Avoid supplying the same flag from both env Sources and CLI when OnlyOnce is enabled."],"tags":["cli","flags","duplicate-argument","go"],"backgroundTag":"duplicate-flag-specified","analyzedSha":"1a4deb4f5a35ee12706602698dc0527d44c14b19","analyzedAt":"2026-08-31T18:42:09.451Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}