Skip to content

Commit

Permalink
Get non-flag args from flagset (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Aug 1, 2024
1 parent c384926 commit f5c18d1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/flag/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (s *Set) Get(name string) (Value, bool) {
return entry.value, true
}

// Args returns a slice of all the non-flag arguments.
func (s *Set) Args() []string {
// TODO: Handle nil safety
return s.args
}

// Parse parses flags and their values from the command line.
func (s *Set) Parse(args []string) (err error) {
if s == nil {
Expand Down
27 changes: 27 additions & 0 deletions internal/flag/set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flag_test

import (
"slices"
"testing"

"github.com/FollowTheProcess/cli/internal/flag"
Expand All @@ -27,6 +28,8 @@ func TestParse(t *testing.T) {
f, exists := set.Get("something")
test.False(t, exists)
test.Equal(t, f, nil)

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{},
wantErr: false,
Expand All @@ -42,6 +45,8 @@ func TestParse(t *testing.T) {
f, exists := set.Get("something")
test.False(t, exists)
test.Equal(t, f, nil)

test.EqualFunc(t, set.Args(), []string{"some", "args", "here", "no", "flags"}, slices.Equal)
},
args: []string{"some", "args", "here", "no", "flags"},
wantErr: false,
Expand All @@ -57,6 +62,8 @@ func TestParse(t *testing.T) {
f, exists := set.Get("flag")
test.False(t, exists)
test.Equal(t, f, nil)

test.EqualFunc(t, set.Args(), []string{"some", "args", "here"}, slices.Equal)
},
args: []string{"some", "args", "here", "--flag", "-s"},
wantErr: true,
Expand Down Expand Up @@ -232,6 +239,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "bool")
test.Equal(t, value.String(), "true")

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{"--delete"},
wantErr: false,
Expand All @@ -256,6 +265,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "bool")
test.Equal(t, value.String(), "true")

test.EqualFunc(t, set.Args(), []string{"some", "subcommand"}, slices.Equal)
},
args: []string{"some", "subcommand", "--delete"},
wantErr: false,
Expand All @@ -280,6 +291,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "bool")
test.Equal(t, value.String(), "true")

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{"-d"},
wantErr: false,
Expand All @@ -304,6 +317,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "bool")
test.Equal(t, value.String(), "true")

test.EqualFunc(t, set.Args(), []string{"some", "arg"}, slices.Equal)
},
args: []string{"some", "arg", "-d"},
wantErr: false,
Expand All @@ -328,6 +343,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{"--count", "1"},
wantErr: false,
Expand All @@ -352,6 +369,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), []string{"some", "arg", "more", "args"}, slices.Equal)
},
args: []string{"some", "arg", "--count", "1", "more", "args"},
wantErr: false,
Expand Down Expand Up @@ -401,6 +420,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), nil, slices.Equal)
},
args: []string{"-c", "1"},
wantErr: false,
Expand All @@ -425,6 +446,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), []string{"args", "more", "args"}, slices.Equal)
},
args: []string{"args", "-c", "1", "more", "args"},
wantErr: false,
Expand Down Expand Up @@ -498,6 +521,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), []string{"args", "more", "args"}, slices.Equal)
},
args: []string{"args", "--count=1", "more", "args"},
wantErr: false,
Expand Down Expand Up @@ -571,6 +596,8 @@ func TestParse(t *testing.T) {

test.Equal(t, value.Type(), "int")
test.Equal(t, value.String(), "1")

test.EqualFunc(t, set.Args(), []string{"args", "more", "args"}, slices.Equal)
},
args: []string{"args", "-c=1", "more", "args"},
wantErr: false,
Expand Down

0 comments on commit f5c18d1

Please sign in to comment.