alibaba/open-code-review · error
%w%s
Error message
%w%s
What it means
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).
Source
Thrown at cmd/opencodereview/flag_suggest.go:29
"github.com/spf13/pflag"
)
// flagErrorWithSuggestion is a SetFlagErrorFunc handler that appends a
// "Did you mean?" suggestion when the user misspells a flag.
func flagErrorWithSuggestion(cmd *cobra.Command, err error) error {
msg := err.Error()
var unknown string
if strings.HasPrefix(msg, "unknown flag: ") {
unknown = strings.TrimPrefix(msg, "unknown flag: ")
unknown = strings.TrimLeft(unknown, "-")
}
if unknown == "" {
return err
}
if suggestion := suggestFlag(cmd, unknown); suggestion != "" {
return fmt.Errorf("%w%s", err, suggestion)
}
return err
}
func suggestFlag(cmd *cobra.Command, unknown string) string {
unknown = strings.TrimLeft(unknown, "-")
if unknown == "" {
return ""
}
var best string
bestDist := 3 // max edit distance to consider
cmd.Flags().VisitAll(func(f *pflag.Flag) {
d := levenshtein(unknown, f.Name)
if d < bestDist {
bestDist = d
best = f.Name
}View on GitHub (pinned to 5cf97d0d15)
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
Example fix
// before ocr review --audince agent // after ocr review --audience agent
Defensive patterns
Strategy: try-catch
Validate before calling
func flagExists(cmd *cobra.Command, name string) bool {
return cmd.Flags().Lookup(strings.TrimLeft(name, "-")) != nil
} Try / catch
if err := cmd.Execute(); err != nil {
if strings.Contains(err.Error(), "unknown flag") {
fmt.Fprintln(os.Stderr, err) // includes appended 'Did you mean this?' hint
}
os.Exit(1)
} Prevention
- 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)
When it happens
Trigger: 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.
Common situations: Simple typos; abbreviating flags when the exact short form does not exist; muscle memory from other CLIs (e.g. --message vs --msg).
Related errors
- unknown provider field %q: supported fields are api_key, api
- only one review mode allowed (--from/--to or --commit)
- --to is required when --from is specified
- --from is required when --to is specified
- invalid --audience value %q: must be 'human' or 'agent'
AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02).
Data as JSON: /api/errors/fa775905c56465a6.
Report an issue: GitHub.