Repository navigation
fix: use the configured completion command name in its help - #2512
nk-batman21 wants to merge 1 commit into
Conversation
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.
yxshee
left a comment
There was a problem hiding this comment.
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.
What type of PR is this?
What this PR does / why we need it:
An app that renames its completion command with
ShellCompletionCommandNamestill prints setup instructions forcompletion. The description is built with that word written into it, and the command is renamed only afterwards:app completions --helpshowssource <(app completion bash),source <(app completion zsh)andapp completion fish > ~/.config/fish/completions/app.fish. Runningapp completion bashprints "No help topic for 'completion'" to stderr and exits with status 3, so a user who copies the line into.bashrcsees that error in every new shell and completion never loads.This adds a
$COMPLETIONplaceholder next to$COMMANDin the description, andbuildCompletionCommandnow takes the command name and fills both. The help for the defaultcompletionname is unchanged.completion.go:buildCompletionCommand(appName, name)setsNameand replaces$COMMANDand$COMPLETIONin one pass.command_setup.go: pick the name (default orShellCompletionCommandName) before building the command.completion_test.go: check the instructions inTestCompletionEnableDiffCommandName, and pass the name inTestCompletionSubcommandOrder.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
ConfigureShellCompletionCommandis not covered: that callback runs after the description is built and can setDescriptiontoo.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 ./...andgo test -tags urfave_cli_no_template ./..., thengo run scripts/build.gowithvet,check-binary-size,generate,diffcheckandv3diff.Checked:
goimports -l .,gofmt -landgolangci-lint run(v2.15.0) report nothing.Release Notes