✨ feat(cli): Show hint message when file completion not applicable to subcommand#5587
✨ feat(cli): Show hint message when file completion not applicable to subcommand#5587vitorfloriano wants to merge 1 commit intokubernetes-sigs:masterfrom
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: vitorfloriano The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| scaffoldCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { | ||
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags") |
There was a problem hiding this comment.
Or we could do something like:
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags") | |
| completions = cobra.AppendActiveHelp(completions, "Type '--help' to get help. Type '--' and press TAB to list flags.") |
Or:
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags") | |
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags. Type '--help' to show help.") |
There was a problem hiding this comment.
I think, if possible we need to check how to solve in the CLI without need to implement each command.
There was a problem hiding this comment.
I think, if possible we need to check how to solve in the CLI without need to implement each command.
ValidArgsFunction is a field in the cobra.Command struct. It's like Use, Run, PreRunE and alike. It needs to be implemented in each command.
There may be way to work around this and make it global, but I don't think that's very idiomatic and will possibly have side-effects.
When hitting TAB on subcommands that don't take files as arguments, the cli will show a hint message (ActiveHelp) on how to list flags instead of showing file completions.
fa5970b to
bb4ddcb
Compare
|
/retest |
There was a problem hiding this comment.
Pull request overview
Adds Cobra ActiveHelp-backed shell completion hints to several Kubebuilder subcommands so that pressing TAB on commands that don’t take file arguments shows a guidance message instead of suggesting filesystem entries.
Changes:
- Set
ValidArgsFunctionto append an ActiveHelp hint (Type '--' and press TAB to list flags) when appropriate. - Disable file completion via
cobra.ShellCompDirectiveNoFileCompfor the affected subcommands. - Apply the behavior across
init,edit,create api,create webhook,alpha generate,alpha update(and alsoversion).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/cli/api.go | Adds ActiveHelp hint + disables file completion for kubebuilder create api. |
| pkg/cli/webhook.go | Adds ActiveHelp hint + disables file completion for kubebuilder create webhook. |
| pkg/cli/init.go | Adds ActiveHelp hint + disables file completion for kubebuilder init. |
| pkg/cli/edit.go | Adds ActiveHelp hint + disables file completion for kubebuilder edit. |
| pkg/cli/version.go | Adds ActiveHelp hint + disables file completion for kubebuilder version. |
| internal/cli/alpha/generate.go | Adds ActiveHelp hint + disables file completion for kubebuilder alpha generate. |
| internal/cli/alpha/update.go | Adds ActiveHelp hint + disables file completion for kubebuilder alpha update. |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user types any partial token before TAB (e.g. kubebuilder create api x<TAB>), this returns no completions + ShellCompDirectiveNoFileComp, so nothing is shown and the hint is lost. Consider showing the hint whenever len(args) == 0 (or at least when len(args) == 0 and the token isn't a flag) so the UX is consistent.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') { |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user types any partial token before TAB, this returns no completions + ShellCompDirectiveNoFileComp, so nothing is shown. Consider showing the hint whenever len(args) == 0 (or when the token isn't a flag) so the UX remains helpful in more cases.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') { |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user has started typing an argument before pressing TAB, completion will return nothing and the hint won't appear. Consider appending ActiveHelp whenever len(args) == 0 (or when the token isn't a flag) so users still get guidance.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || !strings.HasPrefix(toComplete, "-")) { |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user starts typing a token and then presses TAB, this returns no completions + ShellCompDirectiveNoFileComp, so no hint is displayed. Consider showing the hint whenever len(args) == 0 (or when the token isn't a flag) to keep the behavior consistent.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') { |
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user starts typing an argument before TAB, completion will return nothing and the hint won't show. Consider appending ActiveHelp whenever len(args) == 0 (or when the token isn't a flag) for a more consistent UX.
| completions := []cobra.Completion{} | |
| if len(args) == 0 && toComplete == "" { | |
| _ = toComplete | |
| completions := []cobra.Completion{} | |
| if len(args) == 0 { |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user types a partial token before TAB, this will return no completions + ShellCompDirectiveNoFileComp and show nothing. Consider appending ActiveHelp whenever len(args) == 0 (or when the token isn't a flag) so guidance is still shown.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') { |
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { |
There was a problem hiding this comment.
The ActiveHelp hint is only appended when toComplete == "". If the user starts typing a token before pressing TAB, this returns no completions + ShellCompDirectiveNoFileComp, so no hint is displayed. Consider appending ActiveHelp whenever len(args) == 0 (or when the token isn't a flag) so users still get guidance.
| if len(args) == 0 && toComplete == "" { | |
| if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') { |
| // Show hint message on how to list flags instead of showing file completion for | ||
| // commands that don't take files as arguments | ||
| cmd.ValidArgsFunction = func( | ||
| _ *cobra.Command, | ||
| args []string, | ||
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { | ||
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags") | ||
| } | ||
| return completions, cobra.ShellCompDirectiveNoFileComp | ||
| } |
There was a problem hiding this comment.
This same ValidArgsFunction block (including the literal hint string) is duplicated across multiple commands/files in this PR. To keep behavior and wording consistent over time, consider extracting this into a small helper (e.g. a function that sets the ValidArgsFunction) and/or a shared const for the hint message.
| // Show hint message on how to list flags instead of showing file completion for | ||
| // commands that don't take files as arguments | ||
| cmd.ValidArgsFunction = func( | ||
| _ *cobra.Command, | ||
| args []string, | ||
| toComplete string, | ||
| ) ([]cobra.Completion, cobra.ShellCompDirective) { | ||
| completions := []cobra.Completion{} | ||
| if len(args) == 0 && toComplete == "" { | ||
| completions = cobra.AppendActiveHelp(completions, "Type '--' and press TAB to list flags") | ||
| } | ||
| return completions, cobra.ShellCompDirectiveNoFileComp | ||
| } |
There was a problem hiding this comment.
PR description lists the subcommands being updated, but version is also modified here. Either include version in the description (if intended) or drop this change to keep the scope aligned with the PR narrative.
|
PR needs rebase. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
@vitorfloriano: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
This PR adds ActiveHelp to the
init,edit,alpha generate,alpha update,create apiandcreate webhooksubcommands.Now, when hitting TAB on subcommands that don't take files as arguments, the cli will show a hint message (ActiveHelp) on how to list flags instead of showing file completions.
👎🏻 Current behavior:
👍🏻 New behavior:
Note
This is just a first iteration and can be expanded upon, as we see fit.
Relates to #5446