Skip to content

fix: complete only the first line of a multiline usage - #2475

Open
ybai08 wants to merge 1 commit into
urfave:mainfrom
ybai08:fix/completion-multiline-usage
Open

ybai08 wants to merge 1 commit into
urfave:mainfrom
ybai08:fix/completion-multiline-usage

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown

What type of PR is this?

  • bug

What this PR does / why we need it:

The completion scripts for bash, zsh, fish and PowerShell read --generate-shell-completion output one suggestion per line, in the form name:usage or name. A command or flag whose Usage has more than one line therefore writes extra lines, and the shell treats each of them as a suggestion of its own. Take this command:

Commands: []*cli.Command{
	{Name: "serve", Usage: "start the server\nand keep it running"},
	{Name: "stop", Usage: "stop the server"},
},

Here demo <TAB> in bash offers serve, and, keep, it, running, stop and help. The second line splits into words because bash's compgen -W splits on whitespace. The same happens with flag usage when completing --<TAB>.

  • help.go: add completionUsage, which returns the usage up to the first \r or \n, and use it in printCommandSuggestions and printFlagSuggestions. Cobra does the same for its completion descriptions and keeps only the first line. Help output still shows the full usage.
  • completion_test.go: add TestCompletionMultilineUsage. It checks the exact command and flag completion output for usages that contain \n, \n\n and \r\n.

Which issue(s) this PR fixes:

None filed. I found this on current main while looking at how the completion output is framed. I didn't find a matching issue or a competing PR.

Special notes for your reviewer:

Help output is unchanged. Only the shell completion descriptions are cut to their first line.

Testing

  • go test -run TestCompletionMultilineUsage . fails on main (192ed0b) in both cases and passes with this change.
  • End to end with macOS /bin/bash 3.2: built the example above against main and against this branch, sourced the output of demo completion bash (with a stub _get_comp_words_by_ref, since bash-completion isn't installed), then called the completion function with COMP_WORDS=(./demo ""). On main COMPREPLY is serve and keep it running stop help. With this change it's serve stop help.
  • macOS (arm64), Go 1.27.1: make generate, make vet, make test, make test GFLAGS='--tags urfave_cli_no_template', make check-binary-size, make diffcheck, make v3diff, gofmt -l ., goimports -l . and golangci-lint run (v2 latest) pass. Generated API documentation is unchanged.
  • Not run: zsh, fish and PowerShell end to end, make gfmrun (gfmrun not installed), and Linux/Windows CI.

Release Notes

Shell completion now shows only the first line of a multiline command or flag usage, instead of offering the other lines as extra suggestions.

The shell completion scripts read one suggestion per line, as
"name:usage" or "name". A command or flag whose usage spans several
lines therefore had every line after the first offered as a suggestion
of its own.

Write only the first line of the usage, as cobra does for its
completion descriptions.
@ybai08
ybai08 requested a review from a team as a code owner October 17, 2026 14:40

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.

1 participant