Skip to content

Commit 78e356c

Browse files
authored
Merge pull request urfave#2296 from idelchi/issue_2292
Fix:(issue_2292) Empty positional args no longer break parse loop
2 parents 41c4f5f + 4d20bae commit 78e356c

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},
@@ -5624,6 +5624,47 @@ func TestCommand_ExclusiveFlagsPersistent(t *testing.T) {
56245624
}
56255625
}
56265626

5627+
func TestEmptyPositionalArgs(t *testing.T) {
5628+
testCases := []struct {
5629+
Name string
5630+
Args []string
5631+
Expected []string
5632+
}{
5633+
{
5634+
Name: "empty arg between values",
5635+
Args: []string{"app", "hello", "", "world"},
5636+
Expected: []string{"hello", "", "world"},
5637+
},
5638+
{
5639+
Name: "empty arg at start",
5640+
Args: []string{"app", "", "hello"},
5641+
Expected: []string{"", "hello"},
5642+
},
5643+
{
5644+
Name: "whitespace-only arg",
5645+
Args: []string{"app", "hello", " ", "world"},
5646+
Expected: []string{"hello", " ", "world"},
5647+
},
5648+
}
5649+
5650+
for _, tc := range testCases {
5651+
t.Run(tc.Name, func(t *testing.T) {
5652+
var args []string
5653+
5654+
cmd := &Command{
5655+
Action: func(_ context.Context, cmd *Command) error {
5656+
args = cmd.Args().Slice()
5657+
return nil
5658+
},
5659+
}
5660+
5661+
err := cmd.Run(buildTestContext(t), tc.Args)
5662+
assert.NoError(t, err)
5663+
assert.Equal(t, tc.Expected, args)
5664+
})
5665+
}
5666+
}
5667+
56275668
func TestFlagEqualsEmptyValue(t *testing.T) {
56285669
t.Run("--flag= sets empty string", func(t *testing.T) {
56295670
var val string

0 commit comments

Comments
 (0)