Skip to content

Commit 156d47e

Browse files
committed
Fix: (issue#1254) short options handling needs to proceed from last terminated error
1 parent 850cf82 commit 156d47e

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

flag_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,3 +2059,34 @@ func TestTimestampFlagApply_WithDestination(t *testing.T) {
20592059
expect(t, err, nil)
20602060
expect(t, *fl.Destination.timestamp, expectedResult)
20612061
}
2062+
2063+
// Test issue #1254
2064+
// StringSlice() with UseShortOptionHandling causes duplicated entries, depending on the ordering of the flags
2065+
func TestSliceShortOptionHandle(t *testing.T) {
2066+
_ = (&App{
2067+
Commands: []*Command{
2068+
{
2069+
Name: "foobar",
2070+
UseShortOptionHandling: true,
2071+
Action: func(ctx *Context) error {
2072+
if ctx.Bool("i") != true {
2073+
t.Errorf("bool i not set")
2074+
}
2075+
if ctx.Bool("t") != true {
2076+
t.Errorf("bool i not set")
2077+
}
2078+
ss := ctx.StringSlice("net")
2079+
if !reflect.DeepEqual(ss, []string{"foo"}) {
2080+
t.Errorf("Got different slice(%v) than expected", ss)
2081+
}
2082+
return nil
2083+
},
2084+
Flags: []Flag{
2085+
&StringSliceFlag{Name: "net"},
2086+
&BoolFlag{Name: "i"},
2087+
&BoolFlag{Name: "t"},
2088+
},
2089+
},
2090+
},
2091+
}).Run([]string{"run", "foobar", "--net=foo", "-it"})
2092+
}

parse.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string, shellComple
4646
return err
4747
}
4848

49-
// swap current argument with the split version
50-
args = append(args[:i], append(shortOpts, args[i+1:]...)...)
49+
// Start processing only from failed argument and not
50+
// from beginning
51+
args = append(shortOpts, args[i+1:]...)
5152
argsWereSplit = true
5253
break
5354
}

0 commit comments

Comments
 (0)