{"record":{"id":"8f091b009df9087d","repo":"uutils/coreutils","slug":"invalidvalue-clap-error-for-unmatched-argument-val","errorCode":null,"errorMessage":"InvalidValue clap error for unmatched argument value","messagePattern":"InvalidValue clap error for unmatched argument value","errorType":"validation","errorClass":"clap::Error","httpStatus":null,"severity":"error","filePath":"src/uucore/src/lib/features/parser/shortcut_value_parser.rs","lineNumber":37,"sourceCode":"/// `ShortcutValueParser` is similar to clap's `PossibleValuesParser`: it verifies that the value is\n/// from an enumerated set of `PossibleValue`.\n///\n/// Whereas `PossibleValuesParser` only accepts exact matches, `ShortcutValueParser` also accepts\n/// shortcuts as long as they are unambiguous.\nimpl ShortcutValueParser {\n    /// Create a new `ShortcutValueParser` from a list of `PossibleValue`.\n    pub fn new(values: impl Into<Self>) -> Self {\n        values.into()\n    }\n\n    fn generate_clap_error(\n        &self,\n        cmd: &clap::Command,\n        arg: Option<&clap::Arg>,\n        value: &str,\n        possible_values: &[&PossibleValue],\n    ) -> clap::Error {\n        let mut err = clap::Error::new(ErrorKind::InvalidValue).with_cmd(cmd);\n\n        if let Some(arg) = arg {\n            err.insert(\n                ContextKind::InvalidArg,\n                ContextValue::String(arg.to_string()),\n            );\n        }\n\n        err.insert(\n            ContextKind::InvalidValue,\n            ContextValue::String(value.to_string()),\n        );\n\n        err.insert(\n            ContextKind::ValidValue,\n            ContextValue::Strings(self.0.iter().map(|x| x.get_name().to_string()).collect()),\n        );\n","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/uutils/coreutils/blob/325183372aaf86d8ae6feaf4f9548f74fe815102/src/uucore/src/lib/features/parser/shortcut_value_parser.rs#L19-L55","documentation":"`generate_clap_error` in the shortcut (prefix-matching) value parser builds a clap `InvalidValue` error when the provided value does not match any possible value (or is ambiguous/unmatched) for the argument. It attaches the offending argument and the invalid value plus the list of valid possibilities.","triggerScenarios":"Passing a value that doesn't prefix-match any of the parser's `PossibleValue`s, or one that matches multiple values ambiguously, to an argument using ShortcutValueParser (e.g. abbreviated flag values).","commonSituations":"Typo in an abbreviated enum-like CLI value, abbreviations becoming ambiguous after new values are added in a version update, case-mismatched input.","solutions":["Use one of the exact valid values listed in the error","Check spelling/case of the value","If using an abbreviation, lengthen it so it matches exactly one possible value"],"exampleFix":"# before\nprog --color automn   # ambiguous/invalid\n# after\nprog --color auto     # or the full value: autumn","handlingStrategy":"validation","validationCode":"const VALID: &[&str] = &[\"auto\", \"always\", \"never\"];\nfn value_ok(v: &str) -> bool {\n    VALID.iter().filter(|x| x.starts_with(v)).count() == 1\n}","typeGuard":"fn is_exact_match(v: &str, opts: &[&str]) -> Option<&str> {\n    opts.iter().find(|o| **o == v).copied()\n}","tryCatchPattern":"match parse_arg(value) {\n    Err(e) if e.kind() == clap::error::ErrorKind::InvalidValue => {\n        eprintln!(\"bad value; use one of: auto, always, never\");\n        default_value()\n    }\n    other => other?,\n}","preventionTips":["Use full values instead of abbreviations in scripts","Pin utility versions when relying on abbreviation behavior","Print the possible values on failure"],"tags":["clap","argument-parsing","invalid-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"325183372aaf86d8ae6feaf4f9548f74fe815102","analyzedAt":"2026-08-31T11:11:36.175Z","contentChangedAt":"2026-08-31T11:11:36.175Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}