JanDeDobbeleer/oh-my-posh · error
invalid argument %q for "-%s, --%s" flag: %v
Error message
invalid argument %q for "-%s, --%s" flag: %v
What it means
Raised when a shorthand's value fails flag.Value.Set during parseShort - the same failure mode as the long form's 'invalid argument' error, but the message shows both the shorthand and the long name. The underlying strconv error from bool/int/float parsing is wrapped as %v.
Source
Thrown at src/cmdflag/cmdflag.go:351
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 {
lines := make([]string, 0, len(f.order))
maxlen := 0
flags := make([]*Flag, len(f.order))View on GitHub (pinned to 0976794618)
Solutions
- Use a value valid for the flag type: true/false/0/1 for bools, digits for int/float.
- Inspect the trailing strconv error in the message to see the exact parse failure.
- Quote shell values so stray characters don't corrupt them.
- If you own the CLI, implement a custom Value whose Set accepts the looser syntax your users expect.
Example fix
// before
args := []string{"-p=8o80"} // invalid argument "8o80" for "-p, --port" flag: strconv.ParseInt: parsing "8o80": invalid syntax
// after
args := []string{"-p=8080"} Defensive patterns
Strategy: validation
Validate before calling
func validNumeric(s string, base int) bool {
_, err := strconv.ParseInt(s, base, 64)
return err == nil
}
// guard before invoking: if !validNumeric(port, 10) { return fmt.Errorf("port %q must be an integer", port) } Try / catch
if err := cmd.Execute(); err != nil {
if strings.Contains(err.Error(), "invalid argument") && strings.Contains(err.Error(), "flag:") {
// message includes value, -short, --long name and strconv cause
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(2)
}
return err
} Prevention
- Sanitize numeric inputs (strip spaces, units, thousands separators) before passing.
- Use only strconv-compatible bool literals: true/false/0/1/t/f/T/F/TRUE/FALSE.
- Quote values in shell to prevent hidden characters and glob expansion.
- Test script invocations with representative values, not just defaults.
When it happens
Trigger: Parse called with '-fVALUE', '-f=VALUE', or '-f VALUE' where VALUE cannot be parsed by the flag's Value type: e.g. '-p=abc' for an int port flag.
Common situations: Typos in numeric values; bool spellings like 'yes'/'on' that strconv.ParseBool rejects; values with whitespace or units from unquoted shell strings; copy-paste introducing invisible characters or smart quotes.
Related errors
- invalid argument %q for "--%s" flag: %v
- unknown shorthand flag: %q in -%s
- flag needs an argument: %q in -%s
- unknown flag: --%s
- flag needs an argument: --%s
AI-assisted analysis of JanDeDobbeleer/oh-my-posh@0976794618 (2026-08-31).
Data as JSON: /api/errors/4fbadf89cd9c25ab.
Report an issue: GitHub.