spf13/cobra · error

duplicate argument %q for %q

Error message

duplicate argument %q for %q

What it means

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.

Source

Thrown at args.go:73

		validArgs := make([]string, 0, len(cmd.ValidArgs))
		for _, v := range cmd.ValidArgs {
			validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0])
		}
		for _, v := range args {
			if !stringInSlice(v, validArgs) {
				return fmt.Errorf("invalid argument %q for %q%s", v, cmd.CommandPath(), cmd.findSuggestions(args[0]))
			}
		}
	}
	return nil
}

// NoDuplicateArgs returns an error if there are any duplicate positional args.
func NoDuplicateArgs(cmd *Command, args []string) error {
	seen := make(map[string]struct{}, len(args))
	for _, arg := range args {
		if _, ok := seen[arg]; ok {
			return fmt.Errorf("duplicate argument %q for %q", arg, cmd.CommandPath())
		}
		seen[arg] = struct{}{}
	}

	return nil
}

// ArbitraryArgs never returns an error.
func ArbitraryArgs(cmd *Command, args []string) error {
	return nil
}

// MinimumNArgs returns an error if there is not at least N args.
func MinimumNArgs(n int) PositionalArgs {
	return func(cmd *Command, args []string) error {
		if len(args) < n {
			return fmt.Errorf("requires at least %d arg(s), only received %d", n, len(args))
		}

View on GitHub (pinned to adbc881390)

Solutions

  1. Deduplicate the positional args before invoking the command.
  2. If duplicates are legitimately meaningful for your command, do not use NoDuplicateArgs — pick or write a validator that fits.
  3. Combine via MatchAll(NoDuplicateArgs, ...) so it runs alongside rather than instead of other checks.

Example fix

// before
cmd.Args = cobra.NoDuplicateArgs
// `app add foo foo` -> duplicate argument "foo"

// after: dedupe upstream, or relax validator
args = unique(args) // caller-side
Defensive patterns

Strategy: validation

Validate before calling

// Dedupe upstream of the cobra call
func unique(args []string) []string {
    seen := make(map[string]struct{}, len(args))
    out := args[:0]
    for _, a := range args {
        if _, ok := seen[a]; ok { continue }
        seen[a] = struct{}{}
        out = append(out, a)
    }
    return out
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: 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.

Common situations: Shell glob/expansion producing duplicates (`app tag a b a`), user copy-paste errors, or downstream scripts concatenating arg lists without dedup.

Related errors


AI-assisted analysis of spf13/cobra@adbc881390 (2026-08-04). Data as JSON: /data/errors/00de3dc475329124.json. Report an issue: GitHub.