From b5def9fcd49515aef218cf450487e00f1c350d02 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sun, 28 Mar 2021 08:18:46 -0400 Subject: [PATCH] Fix: (issue#1254) short options handling needs to proceed from last terminated error --- flag_test.go | 31 +++++++++++++++++++++++++++++++ parse.go | 5 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/flag_test.go b/flag_test.go index e46270d034..5bfe14f584 100644 --- a/flag_test.go +++ b/flag_test.go @@ -2050,3 +2050,34 @@ func TestTimestampFlagApply_WithDestination(t *testing.T) { expect(t, err, nil) expect(t, *fl.Destination.timestamp, expectedResult) } + +// 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"}) +} diff --git a/parse.go b/parse.go index 7df17296a4..57df48bc2a 100644 --- a/parse.go +++ b/parse.go @@ -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 }