Skip to content

fix: show flag completions when completing a double-dash prefix#2316

Merged
dearchap merged 1 commit intourfave:mainfrom
morozov:fix-completion-double-dash
Apr 18, 2026
Merged

fix: show flag completions when completing a double-dash prefix#2316
dearchap merged 1 commit intourfave:mainfrom
morozov:fix-completion-double-dash

Conversation

@morozov
Copy link
Copy Markdown
Contributor

@morozov morozov commented Apr 16, 2026

What type of PR is this?

  • bug

What this PR does / why we need it:

When the user types cmd subcommand --<TAB> or cmd subcommand --prefix<TAB>, the CLI should show available flags. Previously, this produced no completions or malformed output because of four bugs:

  1. checkShellCompleteFlag() disabled completion mode when -- appeared anywhere in arguments, including when -- was the token being completed.
  2. DefaultCompleteWithFlags() returned early when lastArg == "--".
  3. parseFlags() stripped -- from args during completion, causing lastArg to be empty and triggering command suggestions instead of flag suggestions.
  4. parseFlags() dropped unknown or value-less flags from posArgs and returned an error, so partial prefixes like --prefix were lost before DefaultCompleteWithFlags could use them to filter flag suggestions.

Changes:

  • help.go: checkShellCompleteFlag now only disables completion if -- appears before the token being completed, not when it IS the token being completed
  • help.go: Removed early return in DefaultCompleteWithFlags for lastArg == "--"
  • command_parse.go: Preserve -- in posArgs during shell completion mode
  • command_parse.go: Preserve unknown flags and flags missing a value in posArgs during shell completion mode, so partial prefixes reach DefaultCompleteWithFlags

Which issue(s) this PR fixes:

Fixes #2315

Testing

All changes are covered by new/updated tests that fail without the code changes and pass with them:

  • TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags: Updated to expect --undefined and --number flags preserved in args
  • TestCompletionSubcommand: Added tests for --<TAB> and --partial<TAB> scenarios
  • TestDefaultCompleteWithFlags: Updated flag-suggestion-double-dash-shows-all-flags to expect flag output
  • Test_checkShellCompleteFlag: Added double dash is the token being completed test case

Release Notes

Fixed shell completion to show flag suggestions when completing `--` or partial flag prefixes like `--prefix`

@morozov morozov requested a review from a team as a code owner April 16, 2026 15:13
@dearchap dearchap merged commit b79d768 into urfave:main Apr 18, 2026
9 checks passed
@morozov morozov deleted the fix-completion-double-dash branch April 18, 2026 18:49
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.

Shell completion fails for -- and --prefix flag patterns

2 participants