{"record":{"id":"fa775905c56465a6","repo":"alibaba/open-code-review","slug":"w-s","errorCode":null,"errorMessage":"%w%s","messagePattern":"%w%s","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/opencodereview/flag_suggest.go","lineNumber":29,"sourceCode":"\t\"github.com/spf13/pflag\"\n)\n\n// flagErrorWithSuggestion is a SetFlagErrorFunc handler that appends a\n// \"Did you mean?\" suggestion when the user misspells a flag.\nfunc flagErrorWithSuggestion(cmd *cobra.Command, err error) error {\n\tmsg := err.Error()\n\n\tvar unknown string\n\tif strings.HasPrefix(msg, \"unknown flag: \") {\n\t\tunknown = strings.TrimPrefix(msg, \"unknown flag: \")\n\t\tunknown = strings.TrimLeft(unknown, \"-\")\n\t}\n\tif unknown == \"\" {\n\t\treturn err\n\t}\n\n\tif suggestion := suggestFlag(cmd, unknown); suggestion != \"\" {\n\t\treturn fmt.Errorf(\"%w%s\", err, suggestion)\n\t}\n\treturn err\n}\n\nfunc suggestFlag(cmd *cobra.Command, unknown string) string {\n\tunknown = strings.TrimLeft(unknown, \"-\")\n\tif unknown == \"\" {\n\t\treturn \"\"\n\t}\n\n\tvar best string\n\tbestDist := 3 // max edit distance to consider\n\tcmd.Flags().VisitAll(func(f *pflag.Flag) {\n\t\td := levenshtein(unknown, f.Name)\n\t\tif d < bestDist {\n\t\t\tbestDist = d\n\t\t\tbest = f.Name\n\t\t}","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/cmd/opencodereview/flag_suggest.go#L11-L47","documentation":"flagErrorWithSuggestion takes a cobra unknown-flag error, extracts the unknown flag name, and when suggestFlag finds a close match, appends the suggestion to the original error with fmt.Errorf(\"%w%s\", err, suggestion). The result is an error like `unknown flag: --outpu` plus `\\n\\nDid you mean this?\\n\\t--output`, and it still wraps the original err (errors.Is/As keep working).","triggerScenarios":"Typing a misspelled flag on any ocr command, e.g. `ocr review --audince agent` — cobra returns the unknown-flag error and this helper augments it with the nearest known flag.","commonSituations":"Simple typos; abbreviating flags when the exact short form does not exist; muscle memory from other CLIs (e.g. --message vs --msg).","solutions":["Read the 'Did you mean this?' line appended to the error and rerun with the suggested flag","Run `ocr <cmd> --help` to list valid flags","Use shell completion or tab-completion for flag names"],"exampleFix":"// before\nocr review --audince agent\n// after\nocr review --audience agent","handlingStrategy":"try-catch","validationCode":"func flagExists(cmd *cobra.Command, name string) bool {\n    return cmd.Flags().Lookup(strings.TrimLeft(name, \"-\")) != nil\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Execute(); err != nil {\n    if strings.Contains(err.Error(), \"unknown flag\") {\n        fmt.Fprintln(os.Stderr, err) // includes appended 'Did you mean this?' hint\n    }\n    os.Exit(1)\n}","preventionTips":["Use `--help` or shell completion instead of guessing flag names","Enable cobra SuggestionsMinimumDistance tuning for closer matches","Keep aliases for commonly mistyped flags (e.g. --msg)"],"tags":["cli","flags","cobra","typo"],"backgroundTag":"unknown-flag-typo","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}