Skip to content

Commit

Permalink
Merge pull request #1640 from dearchap/issue_1338
Browse files Browse the repository at this point in the history
Fix:(issue_1338) Fix behaviour of skip flag parsing if there are subc…
  • Loading branch information
dearchap committed Jan 15, 2023
2 parents c24c9f3 + c65ee70 commit 6feec78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
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

0 comments on commit 6feec78

Please sign in to comment.