{"id":"00de3dc475329124","repo":"spf13/cobra","slug":"duplicate-argument-q-for-q","errorCode":null,"errorMessage":"duplicate argument %q for %q","messagePattern":"duplicate argument %q for %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"args.go","lineNumber":73,"sourceCode":"\t\tvalidArgs := make([]string, 0, len(cmd.ValidArgs))\n\t\tfor _, v := range cmd.ValidArgs {\n\t\t\tvalidArgs = append(validArgs, strings.SplitN(v, \"\\t\", 2)[0])\n\t\t}\n\t\tfor _, v := range args {\n\t\t\tif !stringInSlice(v, validArgs) {\n\t\t\t\treturn fmt.Errorf(\"invalid argument %q for %q%s\", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// NoDuplicateArgs returns an error if there are any duplicate positional args.\nfunc NoDuplicateArgs(cmd *Command, args []string) error {\n\tseen := make(map[string]struct{}, len(args))\n\tfor _, arg := range args {\n\t\tif _, ok := seen[arg]; ok {\n\t\t\treturn fmt.Errorf(\"duplicate argument %q for %q\", arg, cmd.CommandPath())\n\t\t}\n\t\tseen[arg] = struct{}{}\n\t}\n\n\treturn nil\n}\n\n// ArbitraryArgs never returns an error.\nfunc ArbitraryArgs(cmd *Command, args []string) error {\n\treturn nil\n}\n\n// MinimumNArgs returns an error if there is not at least N args.\nfunc MinimumNArgs(n int) PositionalArgs {\n\treturn func(cmd *Command, args []string) error {\n\t\tif len(args) < n {\n\t\t\treturn fmt.Errorf(\"requires at least %d arg(s), only received %d\", n, len(args))\n\t\t}","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/spf13/cobra/blob/adbc8813901bba65827259daa8e22ff94ec1f30e/args.go#L55-L91","documentation":"Returned by NoDuplicateArgs when the same positional argument string appears more than once in the args slice. NoDuplicateArgs is a standalone PositionalArgs validator the developer must opt into; it complements (not replaces) counting/valid-args validators.","triggerScenarios":"Setting `cmd.Args = cobra.NoDuplicateArgs` (or wrapping it via MatchAll) and invoking with repeated tokens, e.g. `app add foo foo bar`. Comparison is exact string equality, so 'foo' and 'Foo' are distinct.","commonSituations":"Shell glob/expansion producing duplicates (`app tag a b a`), user copy-paste errors, or downstream scripts concatenating arg lists without dedup.","solutions":["Deduplicate the positional args before invoking the command.","If duplicates are legitimately meaningful for your command, do not use NoDuplicateArgs — pick or write a validator that fits.","Combine via MatchAll(NoDuplicateArgs, ...) so it runs alongside rather than instead of other checks."],"exampleFix":"// before\ncmd.Args = cobra.NoDuplicateArgs\n// `app add foo foo` -> duplicate argument \"foo\"\n\n// after: dedupe upstream, or relax validator\nargs = unique(args) // caller-side","handlingStrategy":"validation","validationCode":"// Dedupe upstream of the cobra call\nfunc unique(args []string) []string {\n    seen := make(map[string]struct{}, len(args))\n    out := args[:0]\n    for _, a := range args {\n        if _, ok := seen[a]; ok { continue }\n        seen[a] = struct{}{}\n        out = append(out, a)\n    }\n    return out\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Deduplicate args in your shell wrapper or script before invoking.","Only apply NoDuplicateArgs when duplicates are genuinely invalid for the domain.","Combine with MatchAll so other arity/validity checks still run."],"tags":["cli","args","duplicate","cobra","validation"],"analyzedSha":"adbc8813901bba65827259daa8e22ff94ec1f30e","analyzedAt":"2026-08-04T21:41:27.617Z","schemaVersion":2}