Skip to content

Commit

Permalink
Omitted optional arg uses the given value
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfrench committed Feb 11, 2024
1 parent f7deded commit 6070e61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ func (a *ArgumentBase[T, C, VC]) Parse(s []string) ([]string, error) {
*a.Values = values
}

if a.Max == 1 && a.Destination != nil && len(values) > 0 {
*a.Destination = values[0]
if a.Max == 1 && a.Destination != nil {
if len(values) > 0 {
*a.Destination = values[0]
} else {
*a.Destination = t
}
}
return s[count:], nil
}
Expand Down
2 changes: 1 addition & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,6 @@ func TestSingleOptionalArg(t *testing.T) {
require.Equal(t, "", s1)

arg.Value = "bar"
require.NoError(t, cmd.Run(context.Background(), []string{"foo", "bar"}))
require.NoError(t, cmd.Run(context.Background(), []string{"foo"}))
require.Equal(t, "bar", s1)
}

0 comments on commit 6070e61

Please sign in to comment.