JanDeDobbeleer/oh-my-posh · error

flag needs an argument: %q in -%s

Error message

flag needs an argument: %q in -%s

What it means

parseShort's counterpart of the long-flag 'needs an argument' error: a registered non-bool shorthand appears as the last character of a group with no attached value ('-f'), nothing left in the group ('-fx' would feed 'x'), and no remaining token to consume. Bools are exempt since a bare shorthand means true.

Source

Thrown at src/cmdflag/cmdflag.go:347

		value := ""

		switch {
		case len(shorthands) > 2 && shorthands[1] == '=':
			value = shorthands[2:]
			shorthands = ""
		case flag.Value.Type() == boolType:
			value = trueStr
			shorthands = shorthands[1:]
		case len(shorthands) > 1:
			value = shorthands[1:]
			shorthands = ""
		case len(rest) > 0:
			value = rest[0]
			rest = rest[1:]
			shorthands = ""
		default:
			return rest, fmt.Errorf("flag needs an argument: %q in -%s", c, shorthands)
		}

		if err := flag.Value.Set(value); err != nil {
			return rest, fmt.Errorf("invalid argument %q for \"-%s, --%s\" flag: %v", value, flag.Shorthand, flag.Name, err)
		}

		flag.Changed = true
	}

	return rest, nil
}

// usage rendering

// FlagUsages renders the non-hidden flags sorted by name, shorthand column,
// type name after the flag, defaults in parentheses when they differ from
// the zero value, usage aligned in a single column.
func (f *FlagSet) FlagUsages() string {

View on GitHub (pinned to 0976794618)

Solutions

  1. Attach the value (-cVALUE or -c=VALUE) or provide it as the next token (-c VALUE).
  2. Verify shell variable expansion isn't emptying the value token.
  3. Omit the flag if the registered default is acceptable.
  4. Register the flag as bool if a bare toggle was the intent.

Example fix

// before
args := []string{"-c"} // flag needs an argument: 'c' in -c
// after
args := []string{"-c", "myconfig.toml"} // or "-cmyconfig.toml"
Defensive patterns

Strategy: validation

Validate before calling

func ensureShortHasValue(args []string) error {
    for i, a := range args {
        if len(a) == 2 && a[0] == '-' && a[1] != '-' {
            if isNonBoolShorthand(flagSet, a[1:]) && i == len(args)-1 {
                return fmt.Errorf("shorthand %s is missing its value", a)
            }
        }
    }
    return nil
}

Try / catch

if err := cmd.Execute(); err != nil {
    if strings.HasPrefix(err.Error(), "flag needs an argument:") {
        fmt.Fprintf(os.Stderr, "%v\nAttach the value: -cVALUE or -c VALUE.\n", err)
        os.Exit(2)
    }
    return err
}

Prevention

When it happens

Trigger: Parse called with a trailing shorthand of a string/int flag, e.g. Parse([]string{"-c"}) where 'c' is a registered string shorthand.

Common situations: Empty shell variable swallowing the value ('-c $CFG' with CFG empty); users forgetting the attached form -cvalue; scripts building argument arrays and dropping the last element; truncated command lines from xargs or line-continuation mistakes.

Related errors


AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31). Data as JSON: /api/errors/c0ad4d729a0c53b4. Report an issue: GitHub.