Skip to content

✨ feat(cli): Show hint message when file completion not applicable to subcommand#5587

Draft
vitorfloriano wants to merge 1 commit intokubernetes-sigs:masterfrom
vitorfloriano:show-active-help
Draft

✨ feat(cli): Show hint message when file completion not applicable to subcommand#5587
vitorfloriano wants to merge 1 commit intokubernetes-sigs:masterfrom
vitorfloriano:show-active-help

Conversation

@vitorfloriano
Copy link
Copy Markdown
Contributor

@vitorfloriano vitorfloriano commented Apr 7, 2026

This PR adds ActiveHelp to the init, edit, alpha generate, alpha update, create api and create webhook subcommands.

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:

$ kubebuilder create api <TAB>
AGENTS.md           designs/            go.mod              Makefile            RELEASE.md          test.sh
bin/                docs/               go.sum              netlify.toml        roadmap/            VERSIONING.md
build/              .git/               hack/               OWNERS              SECURITY_CONTACTS   .yamllint
code-of-conduct.md  .github/            internal/           OWNERS_ALIASES      test/               .yamllint-helm
CONTRIBUTING.md     .gitignore          LICENSE             pkg/                testdata/           
DESIGN.md           .golangci.yml       main.go             README.md           test_e2e.sh 

👍🏻 New behavior:

$ kubebuilder create api <TAB>
Type '--' and press TAB to list flags

Note

This is just a first iteration and can be expanded upon, as we see fit.

Relates to #5446

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: vitorfloriano
Once this PR has been reviewed and has the lgtm label, please assign camilamacedo86 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 7, 2026
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")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we could do something like:

Suggested change
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:

Suggested change
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.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, if possible we need to check how to solve in the CLI without need to implement each command.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@vitorfloriano
Copy link
Copy Markdown
Contributor Author

/retest

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ValidArgsFunction to append an ActiveHelp hint (Type '--' and press TAB to list flags) when appropriate.
  • Disable file completion via cobra.ShellCompDirectiveNoFileComp for the affected subcommands.
  • Apply the behavior across init, edit, create api, create webhook, alpha generate, alpha update (and also version).

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.

Comment thread pkg/cli/api.go
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') {

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/edit.go
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') {

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/init.go
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || !strings.HasPrefix(toComplete, "-")) {

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/version.go
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') {

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/webhook.go
Comment on lines +47 to +48
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
_ = toComplete
completions := []cobra.Completion{}
if len(args) == 0 {

Copilot uses AI. Check for mistakes.
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') {

Copilot uses AI. Check for mistakes.
toComplete string,
) ([]cobra.Completion, cobra.ShellCompDirective) {
completions := []cobra.Completion{}
if len(args) == 0 && toComplete == "" {
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if len(args) == 0 && toComplete == "" {
if len(args) == 0 && (toComplete == "" || toComplete[0] != '-') {

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/api.go
Comment on lines +40 to +52
// 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
}
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread pkg/cli/version.go
Comment on lines +37 to +49
// 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
}
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@vitorfloriano vitorfloriano marked this pull request as draft April 8, 2026 11:57
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Apr 8, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions 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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 20, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

@vitorfloriano: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubebuilder-e2e-k8s-1-36-0 bb4ddcb link true /test pull-kubebuilder-e2e-k8s-1-36-0

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.

Details

Instructions 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants