Skip to content

Commit b4f42d3

Browse files
committed
Fix:(issue_2292) Empty positional args no longer break parse loop
1 parent a2d0cf1 commit b4f42d3

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

command_parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
8080

8181
firstArg := strings.TrimSpace(rargs[0])
8282
if len(firstArg) == 0 {
83-
break
83+
posArgs = append(posArgs, rargs[0])
84+
continue
8485
}
8586

8687
// stop parsing once we see a "--"

command_test.go

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ var defaultCommandTests = []struct {
823823
{"f", "", nil, true},
824824
{"", "foobar", nil, true},
825825
{"", "", nil, true},
826-
{" ", "", nil, true},
826+
{" ", "", nil, false},
827827
{"bat", "batbaz", nil, true},
828828
{"nothing", "batbaz", nil, true},
829829
{"nothing", "", nil, false},
@@ -911,10 +911,10 @@ var defaultCommandSubCommandTests = []struct {
911911
{"", "carly", "foobar", true},
912912
{"", "jimmers", "foobar", false},
913913
{"", "jimmers", "", false},
914-
{" ", "jimmers", "foobar", true},
914+
{" ", "jimmers", "foobar", false},
915915
{"", "", "", true},
916-
{" ", "", "", true},
917-
{" ", "j", "", true},
916+
{" ", "", "", false},
917+
{" ", "j", "", false},
918918
{"bat", "", "batbaz", false},
919919
{"nothing", "", "batbaz", false},
920920
{"nothing", "", "", false},
@@ -977,10 +977,10 @@ var defaultCommandFlagTests = []struct {
977977
{"", "--carly=derp", "foobar", true},
978978
{"", "-j", "foobar", true},
979979
{"", "-j", "", true},
980-
{" ", "-j", "foobar", true},
980+
{" ", "-j", "foobar", false},
981981
{"", "", "", true},
982-
{" ", "", "", true},
983-
{" ", "-j", "", true},
982+
{" ", "", "", false},
983+
{" ", "-j", "", false},
984984
{"bat", "", "batbaz", false},
985985
{"nothing", "", "batbaz", false},
986986
{"nothing", "", "", false},
@@ -5575,3 +5575,44 @@ func TestCommand_ExclusiveFlagsPersistent(t *testing.T) {
55755575
})
55765576
}
55775577
}
5578+
5579+
func TestEmptyPositionalArgs(t *testing.T) {
5580+
testCases := []struct {
5581+
Name string
5582+
Args []string
5583+
Expected []string
5584+
}{
5585+
{
5586+
Name: "empty arg between values",
5587+
Args: []string{"app", "hello", "", "world"},
5588+
Expected: []string{"hello", "", "world"},
5589+
},
5590+
{
5591+
Name: "empty arg at start",
5592+
Args: []string{"app", "", "hello"},
5593+
Expected: []string{"", "hello"},
5594+
},
5595+
{
5596+
Name: "whitespace-only arg",
5597+
Args: []string{"app", "hello", " ", "world"},
5598+
Expected: []string{"hello", " ", "world"},
5599+
},
5600+
}
5601+
5602+
for _, tc := range testCases {
5603+
t.Run(tc.Name, func(t *testing.T) {
5604+
var args []string
5605+
5606+
cmd := &Command{
5607+
Action: func(_ context.Context, cmd *Command) error {
5608+
args = cmd.Args().Slice()
5609+
return nil
5610+
},
5611+
}
5612+
5613+
err := cmd.Run(buildTestContext(t), tc.Args)
5614+
assert.NoError(t, err)
5615+
assert.Equal(t, tc.Expected, args)
5616+
})
5617+
}
5618+
}

0 commit comments

Comments
 (0)