{"record":{"id":"c0ad4d729a0c53b4","repo":"JanDeDobbeleer/oh-my-posh","slug":"flag-needs-an-argument-q-in-s","errorCode":null,"errorMessage":"flag needs an argument: %q in -%s","messagePattern":"flag needs an argument: %q in -(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmdflag/cmdflag.go","lineNumber":347,"sourceCode":"\n\t\tvalue := \"\"\n\n\t\tswitch {\n\t\tcase len(shorthands) > 2 && shorthands[1] == '=':\n\t\t\tvalue = shorthands[2:]\n\t\t\tshorthands = \"\"\n\t\tcase flag.Value.Type() == boolType:\n\t\t\tvalue = trueStr\n\t\t\tshorthands = shorthands[1:]\n\t\tcase len(shorthands) > 1:\n\t\t\tvalue = shorthands[1:]\n\t\t\tshorthands = \"\"\n\t\tcase len(rest) > 0:\n\t\t\tvalue = rest[0]\n\t\t\trest = rest[1:]\n\t\t\tshorthands = \"\"\n\t\tdefault:\n\t\t\treturn rest, fmt.Errorf(\"flag needs an argument: %q in -%s\", c, shorthands)\n\t\t}\n\n\t\tif err := flag.Value.Set(value); err != nil {\n\t\t\treturn rest, fmt.Errorf(\"invalid argument %q for \\\"-%s, --%s\\\" flag: %v\", value, flag.Shorthand, flag.Name, err)\n\t\t}\n\n\t\tflag.Changed = true\n\t}\n\n\treturn rest, nil\n}\n\n// usage rendering\n\n// FlagUsages renders the non-hidden flags sorted by name, shorthand column,\n// type name after the flag, defaults in parentheses when they differ from\n// the zero value, usage aligned in a single column.\nfunc (f *FlagSet) FlagUsages() string {","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/cmdflag/cmdflag.go#L329-L365","documentation":"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.","triggerScenarios":"Parse called with a trailing shorthand of a string/int flag, e.g. Parse([]string{\"-c\"}) where 'c' is a registered string shorthand.","commonSituations":"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.","solutions":["Attach the value (-cVALUE or -c=VALUE) or provide it as the next token (-c VALUE).","Verify shell variable expansion isn't emptying the value token.","Omit the flag if the registered default is acceptable.","Register the flag as bool if a bare toggle was the intent."],"exampleFix":"// before\nargs := []string{\"-c\"} // flag needs an argument: 'c' in -c\n// after\nargs := []string{\"-c\", \"myconfig.toml\"} // or \"-cmyconfig.toml\"","handlingStrategy":"validation","validationCode":"func ensureShortHasValue(args []string) error {\n    for i, a := range args {\n        if len(a) == 2 && a[0] == '-' && a[1] != '-' {\n            if isNonBoolShorthand(flagSet, a[1:]) && i == len(args)-1 {\n                return fmt.Errorf(\"shorthand %s is missing its value\", a)\n            }\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Execute(); err != nil {\n    if strings.HasPrefix(err.Error(), \"flag needs an argument:\") {\n        fmt.Fprintf(os.Stderr, \"%v\\nAttach the value: -cVALUE or -c VALUE.\\n\", err)\n        os.Exit(2)\n    }\n    return err\n}","preventionTips":["Quote shell variables so an empty value never removes the token: \"-c ${CFG:-d.toml}\".","Prefer attached form -cVALUE; it cannot lose the value to token splitting.","Double-check generated argv arrays for dropped last elements.","Use long flags in scripts to sidestep shorthand grouping pitfalls."],"tags":["cli","flag-parsing","missing-argument","shorthand"],"backgroundTag":"flag-needs-argument","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}