Repository navigation
fix: don't suggest hidden flags or commands - #2480
Conversation
With Suggest enabled, a mistyped flag gets a "Did you mean" hint built from every flag name, including flags declared Hidden. Hidden flags are already left out of help output and shell completion, so advertising them in the error output defeats the purpose of hiding them. Skip flags that report themselves as not visible when picking a suggestion. The help flag names are still considered as before.
With Suggest enabled, "app help <name>" with an unknown name appends the closest command name to the "No help topic" error, including commands declared Hidden. With a hidden "putz" command, "app help put" fails with: No help topic for 'put'. putz Hidden commands are already left out of help output and shell completion, so leave them out of this suggestion too. Filter them at this call site instead of in suggestCommand, because PrefixMatchCommands also resolves command names through suggestCommand. Fixes: 002bde2 ("Add suggestions support") Assisted-by: claude:claude-opus-5-5 Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Juneezee
left a comment
There was a problem hiding this comment.
Thanks! Confirmed on main: with Suggest: true, app --internal-debu gets Did you mean "--internal-debug"? for the hidden flag, and both new tests fail there and pass here.
help <name> had the same leak for hidden commands. With a hidden putz and a visible pull, app help put failed with No help topic for 'put'. putz. I pushed 24bab17 to filter hidden commands at that call site, so it now suggests pull:
Lines 341 to 352 in 24bab17
suggestCommand itself stays unchanged, as you noted, because PrefixMatchCommands resolves command names through it. Filtering there would make app putz run pull:
Lines 297 to 301 in 05d84c1
I also updated the title and the release note to cover both.
What type of PR is this?
What this PR does / why we need it:
With
Suggest: true, a mistyped flag gets a "Did you mean" hint fromsuggestFlag, which looks at every flag name, including flags declaredHidden: true. Hidden flags are already left out of help output and, since #2388, out of shell completion, so naming one in the usage error gives away the flag the application meant to hide:On
mainthis prints:The GLOBAL OPTIONS section printed right after it lists only
--verboseand--help.suggestions.go:suggestFlagskips the names of a flag that implementsVisibleFlagand reports!IsVisible(). This is the same checkprintFlagSuggestionsuses for shell completion. The help flag names are still appended as before, so help is suggested exactly as it is today. When no visible name is close, the existing best-score fallback applies. For the example above the hint becomesDid you mean "--verbose"?.suggestions_test.go: one unit test onsuggestFlagand one end-to-end test throughCommand.Run.suggestCommandis unchanged, because it also resolves commands forPrefixMatchCommands. The second commit filters hidden commands at thehelp <name>call site instead: with a hiddenputzcommand,app help putfailed withNo help topic for 'put'. putz.TestCommandHelpSuggestHiddenCommandcovers it.Which issue(s) this PR fixes:
None filed.
Special notes for your reviewer:
A custom flag type that doesn't implement
VisibleFlagis still suggested, which matches shell completion. All the built-in flag types implement it.Testing
On
main,TestSuggestFlagHiddenFlagfails withexpected: "--verbose", actual: "--internal-debug", andTestSuggestFlagHiddenFlagFromRunfails because the output containsDid you mean "--internal-debug"?.No exported API or doc comments change, so
godoc-current.txtis unaffected. (With Go 1.24 locally,make generaterewrites three unrelated doc-link lines ingodoc-current.txtonmaintoo. That comes from the local toolchain version, not this change.)Release Notes