From f7deded2010a903c2d24b7173dbd99fcb90ccddd Mon Sep 17 00:00:00 2001 From: Josh French Date: Sun, 11 Feb 2024 10:40:37 -0500 Subject: [PATCH 1/3] Boundary check for optional arg --- args.go | 2 +- args_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/args.go b/args.go index f311a93cae..0bfb734d67 100644 --- a/args.go +++ b/args.go @@ -132,7 +132,7 @@ func (a *ArgumentBase[T, C, VC]) Parse(s []string) ([]string, error) { *a.Values = values } - if a.Max == 1 && a.Destination != nil { + if a.Max == 1 && a.Destination != nil && len(values) > 0 { *a.Destination = values[0] } return s[count:], nil diff --git a/args_test.go b/args_test.go index 14f8944cfa..6041d1522d 100644 --- a/args_test.go +++ b/args_test.go @@ -151,3 +151,23 @@ func TestArgsUsage(t *testing.T) { }) } } + +func TestSingleOptionalArg(t *testing.T) { + cmd := buildMinimalTestCommand() + var s1 string + arg := &StringArg{ + Min: 0, + Max: 1, + Destination: &s1, + } + cmd.Arguments = []Argument{ + arg, + } + + require.NoError(t, cmd.Run(context.Background(), []string{"foo"})) + require.Equal(t, "", s1) + + arg.Value = "bar" + require.NoError(t, cmd.Run(context.Background(), []string{"foo", "bar"})) + require.Equal(t, "bar", s1) +} From 6070e6167eac69852c3ce448ca8a8d15dfa5c61e Mon Sep 17 00:00:00 2001 From: Josh French Date: Sun, 11 Feb 2024 11:05:36 -0500 Subject: [PATCH 2/3] Omitted optional arg uses the given value --- args.go | 8 ++++++-- args_test.go | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/args.go b/args.go index 0bfb734d67..59b6e4e7e0 100644 --- a/args.go +++ b/args.go @@ -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 } diff --git a/args_test.go b/args_test.go index 6041d1522d..5551051776 100644 --- a/args_test.go +++ b/args_test.go @@ -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) } From d765601448fd154172286d9d85c9b729ccd3b070 Mon Sep 17 00:00:00 2001 From: Josh French Date: Sun, 11 Feb 2024 19:21:17 -0500 Subject: [PATCH 3/3] Assert that explicit arg overrides default value --- args_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/args_test.go b/args_test.go index 5551051776..e951c8bb27 100644 --- a/args_test.go +++ b/args_test.go @@ -170,4 +170,7 @@ func TestSingleOptionalArg(t *testing.T) { arg.Value = "bar" require.NoError(t, cmd.Run(context.Background(), []string{"foo"})) require.Equal(t, "bar", s1) + + require.NoError(t, cmd.Run(context.Background(), []string{"foo", "zbar"})) + require.Equal(t, "zbar", s1) }