Skip to content

Commit

Permalink
Fix: (issue#1254) short options handling needs to proceed from last t…
Browse files Browse the repository at this point in the history
…erminated error
  • Loading branch information
dearchap committed Mar 28, 2021
1 parent 13ded1e commit 1293f22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 31 additions & 0 deletions flag_test.go
Expand Up @@ -1973,3 +1973,34 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) {
err := set.Parse([]string{"--time", "2006-01-02T15:04:05Z"})
expect(t, err, fmt.Errorf("invalid value \"2006-01-02T15:04:05Z\" for flag -time: parsing time \"2006-01-02T15:04:05Z\" as \"Jan 2, 2006 at 3:04pm (MST)\": cannot parse \"2006-01-02T15:04:05Z\" as \"Jan\""))
}

// Test issue #1254
// StringSlice() with UseShortOptionHandling causes duplicated entries, depending on the ordering of the flags
func TestSliceShortOptionHandle(t *testing.T) {
_ = (&App{
Commands: []*Command{
{
Name: "foobar",
UseShortOptionHandling: true,
Action: func(ctx *Context) error {
if ctx.Bool("i") != true {
t.Errorf("bool i not set")
}
if ctx.Bool("t") != true {
t.Errorf("bool i not set")
}
ss := ctx.StringSlice("net")
if !reflect.DeepEqual(ss, []string{"foo"}) {
t.Errorf("Got different slice(%v) than expected", ss)
}
return nil
},
Flags: []Flag{
&StringSliceFlag{Name: "net"},
&BoolFlag{Name: "i"},
&BoolFlag{Name: "t"},
},
},
},
}).Run([]string{"run", "foobar", "--net=foo", "-it"})
}
5 changes: 3 additions & 2 deletions parse.go
Expand Up @@ -46,8 +46,9 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string, shellComple
return err
}

// swap current argument with the split version
args = append(args[:i], append(shortOpts, args[i+1:]...)...)
// Start processing only from failed argument and not
// from beginning
args = append(shortOpts, args[i+1:]...)
argsWereSplit = true
break
}
Expand Down

0 comments on commit 1293f22

Please sign in to comment.