Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1338) Fix behaviour of skip flag parsing if there are subc… #1640

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ type Commands []Command

// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) (err error) {
if len(c.Subcommands) > 0 {
return c.startApp(ctx)
if !c.SkipFlagParsing {
if len(c.Subcommands) > 0 {
return c.startApp(ctx)
}
}

if !c.HideHelp && (HelpFlag != BoolFlag{}) {
Expand Down Expand Up @@ -261,7 +263,7 @@ func reorderArgs(commandFlags []Flag, args []string) []string {

// argIsFlag checks if an arg is one of our command flags
func argIsFlag(commandFlags []Flag, arg string) bool {
if arg == "-" || arg == "--"{
if arg == "-" || arg == "--" {
// `-` is never a flag
// `--` is an option-value when following a flag, and a delimiter indicating the end of options in other cases.
return false
Expand Down
15 changes: 10 additions & 5 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestParseAndRunHyphenValues(t *testing.T) {
cases := []struct {
testArgs []string
expectedArgs []string
expectedOpt string
expectedOpt string
}{
{[]string{"foo", "test", "argz"}, []string{"argz"}, ""},
{[]string{"foo", "test", "argz", "arga"}, []string{"argz", "arga"}, ""},
Expand Down Expand Up @@ -155,10 +155,10 @@ func TestParseAndRunHyphenValues(t *testing.T) {

for _, tc := range cases {
tc := tc
t.Run(strings.Join(tc.testArgs, "_"), func(t *testing.T){
t.Run(strings.Join(tc.testArgs, "_"), func(t *testing.T) {
var (
args []string
opt string
opt string
)

cmd := Command{
Expand All @@ -171,8 +171,8 @@ func TestParseAndRunHyphenValues(t *testing.T) {
return nil
},
Flags: []Flag{
StringFlag{Name: "opt"},
StringFlag{Name: "opt2"},
StringFlag{Name: "opt"},
StringFlag{Name: "opt2"},
},
}

Expand Down Expand Up @@ -436,6 +436,11 @@ func TestCommandSkipFlagParsing(t *testing.T) {
Flags: []Flag{
StringFlag{Name: "flag"},
},
Subcommands: []Command{
{
Name: "some-arg",
},
},
Action: func(c *Context) {
fmt.Printf("%+v\n", c.String("flag"))
args = c.Args()
Expand Down