Visitar URL original
fix: use the configured completion command name in its help by nk-batman21 · Pull Request #2512 · urfave/cli · GitHub
Skip to content

fix: use the configured completion command name in its help - #2512

Open
nk-batman21 wants to merge 1 commit into
urfave:mainfrom
nk-batman21:fix/completion-help-command-name
Open

nk-batman21 wants to merge 1 commit into
urfave:mainfrom
nk-batman21:fix/completion-help-command-name

Conversation

@nk-batman21

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

An app that renames its completion command with ShellCompletionCommandName still prints setup instructions for completion. The description is built with that word written into it, and the command is renamed only afterwards:

cmd := &cli.Command{
	Name:                       "app",
	EnableShellCompletion:      true,
	ShellCompletionCommandName: "completions",
}

app completions --help shows source <(app completion bash), source <(app completion zsh) and app completion fish > ~/.config/fish/completions/app.fish. Running app completion bash prints "No help topic for 'completion'" to stderr and exits with status 3, so a user who copies the line into .bashrc sees that error in every new shell and completion never loads.

This adds a $COMPLETION placeholder next to $COMMAND in the description, and buildCompletionCommand now takes the command name and fills both. The help for the default completion name is unchanged.

  • completion.go: buildCompletionCommand(appName, name) sets Name and replaces $COMMAND and $COMPLETION in one pass.
  • command_setup.go: pick the name (default or ShellCompletionCommandName) before building the command.
  • completion_test.go: check the instructions in TestCompletionEnableDiffCommandName, and pass the name in TestCompletionSubcommandOrder.

Which issue(s) this PR fixes:

None filed.

Special notes for your reviewer:

#2463 adds a test that calls buildCompletionCommand("myapp"). If both are merged, that call needs the second argument.

A name changed inside ConfigureShellCompletionCommand is not covered: that callback runs after the description is built and can set Description too.

Testing

The new assertions fail on main (6898360) with does not contain "source <(foo junky bash)" and pass with this change.

Ran (Windows, Go 1.26.0): go test ./... and go test -tags urfave_cli_no_template ./..., then go run scripts/build.go with vet, check-binary-size, generate, diffcheck and v3diff.
Checked: goimports -l ., gofmt -l and golangci-lint run (v2.15.0) report nothing.

Release Notes

The help of the shell completion command now uses the name set in ShellCompletionCommandName in its setup instructions, instead of always "completion".

The completion command's description tells users to run "<app>
completion bash", "<app> completion zsh" and "<app> completion fish",
with "completion" written into the text. setupDefaults renames the
command to ShellCompletionCommandName only after the description is
built, so an app that renames it to "completions" prints instructions
for a command that does not exist: "app completion bash" fails with
"No help topic for 'completion'", and a .bashrc that sources it never
loads completion.

Pass the command name to buildCompletionCommand and fill it into the
description next to the app name. The default help text is unchanged.
@nk-batman21
nk-batman21 requested a review from a team as a code owner October 28, 2026 17:22

@yxshee yxshee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Read through this. It looks right to me. buildCompletionCommand only has the one caller in setupDefaults plus the order test, and the shell scripts themselves go through --generate-shell-completion rather than the command name, so the rename only needs to reach the description, which this does. The new assertions in TestCompletionEnableDiffCommandName cover both the bash and fish lines and check that "foo completion" is gone.

One non-blocking note: the description is built before ConfigureShellCompletionCommand runs, so if someone renames the command inside that callback (cmd.Name = "x") instead of using ShellCompletionCommandName, the help text will still show the earlier name. That seems fine, since the callback can set Description too, but it might be worth a line in the ConfigureShellCompletionCommand docs.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants