Skip to content

fix: parse child flags after shorthand command names - #2541

Open
yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/shorthand-command-flags
Open

yxshee wants to merge 1 commit into
urfave:mainfrom
yxshee:fix/shorthand-command-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 PrefixMatchCommands: true, app depl --region west rejects a flag declared on deploy, even though app deploy --region west works. If the root also declares --region, the abbreviated invocation succeeds but sets the root flag instead; the child action receives its default value.

parseFlags only recognizes exact command names and explicit aliases. It parses the remaining flags on the parent before run resolves the shorthand through SuggestCommandFunc.

Resolve the first positional argument while locating the child command, then let that child parse the remaining flags. Parsing and dispatch share the result, so a custom matcher is called once per invocation. An unmatched name still leaves subsequent flags for the parent, and the cached match is local to each run.

The regression tests cover full names, aliases, shorthand with long and short flags, a custom matcher with a shadowed parent flag, an unmatched name, repeated runs, and preserving the existing shell-completion path.

Which issue(s) this PR fixes:

No existing issue found. Searches for PrefixMatchCommands, SuggestCommandFunc, and shorthand command flags found no competing fix.

Special notes for your reviewer:

A custom matcher now runs when parsing reaches the first positional argument, before flags following it are parsed. This is necessary to choose which command owns those flags.

#2534 addresses command names following --; this change addresses flags following a shorthand command name. Both touch dispatch, so I will reconcile them if either merges first.

Testing

On macOS arm64 with Go 1.26.5:

  • The new regression tests fail on upstream d5c06cb, pass with the fix, and fail again with only the production fix stashed.
  • A standalone program with root and child --region flags prints child region=child-default; root region=west before the fix and child region=west; root region=root-default afterward.
  • 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.
  • make gfmrun FLAGS='--walk docs/v3/examples': 42 examples, 0 errors.

The local tests use the race detector. Linux, Windows, and the other Go version in the CI matrix have not been run locally.

Release Notes

Flags following a shorthand command name are now parsed by the selected child command, instead of being rejected or applied to a same-named parent flag.

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