Skip to content

fix: preserve flag parsing when an argument matches a command - #2543

Open
yxshee wants to merge 2 commits into
urfave:mainfrom
yxshee:fix/positional-command-name-flags
Open

yxshee wants to merge 2 commits into
urfave:mainfrom
yxshee:fix/positional-command-name-flags

Conversation

@yxshee

@yxshee yxshee commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • bug

What this PR does / why we need it:

With a root --verbose flag and a deploy child, app file deploy --verbose runs the root action with verbose=false and arguments ["file", "deploy", "--verbose"]. Replacing deploy with an ordinary word correctly sets the flag. A command alias has the same effect. A padded first argument, " deploy ", also stops parsing even though dispatch preserves it as an argument.

parseFlags hands off the remaining arguments whenever a trimmed positional token matches a child, but dispatch only selects a child from the exact first positional argument. If a later argument happens to be a command name, parsing stops and no child takes over.

Only hand parsing to a child when the exact first positional argument selects it. The root then parses its remaining flags normally, while first-position command names and aliases still dispatch as before.

Which issue(s) this PR fixes:

No existing issue found. Found with a standalone program using the public API; searches for positional arguments, command names, and parseFlags found no competing fix for this case.

Special notes for your reviewer:

#2541 changes the same parser block to handle shorthand command names. I checked this guard together with that change and the other pending command fixes; the combined race suite passes. I will reconcile the small overlap if either parser change lands first.

Testing

On macOS arm64 with Go 1.26.5, based on upstream d5c06cb:

  • TestCommand_ChildNameAfterPositionalArgument fails for a later command name, a later alias, and a command name following an empty argument. The ordinary-argument and first-position dispatch controls pass. Those six cases pass with the initial fix; stashing only that production change reproduces the three failures. A seventh case for a padded first argument fails on the initial PR head 91df506, then passes when lookup uses the exact argument; stashing that follow-up reproduces its failure.
  • The standalone program changes from verbose=false args=["file" "deploy" "--verbose"] to verbose=true args=["file" "deploy"].
  • make ensure-goimports lint vet test
  • make test GFLAGS='--tags urfave_cli_no_template'
  • make check-binary-size generate v3diff diffcheck
  • golangci-lint run: 0 issues.

The test targets include the race detector. Linux/Windows and the other CI Go version were not run locally.

Release Notes

Command names used as later positional arguments, and padded arguments that resemble command names, no longer prevent later flags from being parsed by the current command.

@yxshee
yxshee requested a review from a team as a code owner November 15, 2026 00:58

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