Skip to content

Commit

Permalink
Add a test case for flag requiring an argument (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 5, 2024
1 parent 18b6d8d commit 2bff85d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ func (s *Set) parseLongFlag(long string, rest []string) (remaining []string, err
return rest[1:], nil
default:
// --flag (value was required)
// Do we ever hit this? Idk when a flag would be required
return nil, fmt.Errorf("flag --%s requires an argument", name)
}
}
Expand Down
27 changes: 27 additions & 0 deletions internal/flag/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,33 @@ func TestParse(t *testing.T) {
args: []string{"--count", "1"},
wantErr: false,
},
{
name: "valid long missing value",
newSet: func(t *testing.T) *flag.Set {
t.Helper()
set := flag.NewSet()
f, err := flag.New(new(int), "count", 'c', 0, "Count something")
test.Ok(t, err)

err = flag.AddToSet(set, f)
test.Ok(t, err)

return set
},
test: func(t *testing.T, set *flag.Set) {
t.Helper()
entry, exists := set.Get("count")
test.True(t, exists)

test.Equal(t, entry.Value.Type(), "int")
test.Equal(t, entry.Value.String(), "0")

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{"--count"}, // Count needs an argument
wantErr: true,
errMsg: "flag --count requires an argument",
},
{
name: "valid long value with args",
newSet: func(t *testing.T) *flag.Set {
Expand Down

0 comments on commit 2bff85d

Please sign in to comment.