fix: allow DefaultCommand to handle its own flags#2322
Open
lihan3238 wants to merge 3 commits into
Open
Conversation
When DefaultCommand is set, flags defined on the default subcommand should work even without specifying the subcommand name. Previously, unknown flags were rejected during parent command parsing before the default command routing could occur. Pass unknown flags through as positional args when DefaultCommand is set, allowing the default command to handle them correctly. Fixes urfave#2249
Covers the shortOptionHandling path in parseFlags where unknown short flags are passed through when DefaultCommand is set, improving codecov patch coverage from 50% to 100%.
dearchap
reviewed
May 12, 2026
| for index, c := range flagName { | ||
| tracef("processing flag (fName=%[1]q)", string(c)) | ||
| if sf := cmd.lookupFlag(string(c)); sf == nil { | ||
| if cmd.DefaultCommand != "" { |
Contributor
There was a problem hiding this comment.
Suggested change
| if cmd.DefaultCommand != "" { | |
| if index == 0 && cmd.DefaultCommand != "" { |
you dont want to pass it unknown flags from middle of split
dearchap
reviewed
May 12, 2026
| Name: "run1", | ||
| Usage: "run the main application", | ||
| Action: func(ctx context.Context, cmd *Command) error { | ||
| if cmd.String("foo") != "bar" { |
Contributor
There was a problem hiding this comment.
you need to set a condition here that the action was executed and then check that outside the action block
dearchap
reviewed
May 12, 2026
| }, | ||
| Flags: []Flag{ | ||
| &StringFlag{ | ||
| Name: "bar", |
Contributor
There was a problem hiding this comment.
change flag name to something else. It causes confusion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2249
Summary
Test plan