summaryrefslogtreecommitdiff
path: root/subcommand_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'subcommand_test.go')
-rw-r--r--subcommand_test.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/subcommand_test.go b/subcommand_test.go
index c34ab01..2c61dd3 100644
--- a/subcommand_test.go
+++ b/subcommand_test.go
@@ -1,6 +1,7 @@
package arg
import (
+ "reflect"
"testing"
"github.com/stretchr/testify/assert"
@@ -48,6 +49,17 @@ func TestMinimalSubcommand(t *testing.T) {
assert.Equal(t, []string{"list"}, p.SubcommandNames())
}
+func TestSubcommandNamesBeforeParsing(t *testing.T) {
+ type listCmd struct{}
+ var args struct {
+ List *listCmd `arg:"subcommand"`
+ }
+ p, err := NewParser(Config{}, &args)
+ require.NoError(t, err)
+ assert.Nil(t, p.Subcommand())
+ assert.Nil(t, p.SubcommandNames())
+}
+
func TestNoSuchSubcommand(t *testing.T) {
type listCmd struct {
}
@@ -179,6 +191,36 @@ func TestSubcommandsWithOptions(t *testing.T) {
}
}
+func TestSubcommandsWithEnvVars(t *testing.T) {
+ type getCmd struct {
+ Name string `arg:"env"`
+ }
+ type listCmd struct {
+ Limit int `arg:"env"`
+ }
+ type cmd struct {
+ Verbose bool
+ Get *getCmd `arg:"subcommand"`
+ List *listCmd `arg:"subcommand"`
+ }
+
+ {
+ var args cmd
+ setenv(t, "LIMIT", "123")
+ err := parse("list", &args)
+ require.NoError(t, err)
+ require.NotNil(t, args.List)
+ assert.Equal(t, 123, args.List.Limit)
+ }
+
+ {
+ var args cmd
+ setenv(t, "LIMIT", "not_an_integer")
+ err := parse("list", &args)
+ assert.Error(t, err)
+ }
+}
+
func TestNestedSubcommands(t *testing.T) {
type child struct{}
type parent struct {
@@ -353,3 +395,19 @@ func TestSubcommandsWithMultiplePositionals(t *testing.T) {
assert.Equal(t, 5, args.Limit)
}
}
+
+func TestValForNilStruct(t *testing.T) {
+ type subcmd struct{}
+ var cmd struct {
+ Sub *subcmd `arg:"subcommand"`
+ }
+
+ p, err := NewParser(Config{}, &cmd)
+ require.NoError(t, err)
+
+ typ := reflect.TypeOf(cmd)
+ subField, _ := typ.FieldByName("Sub")
+
+ v := p.val(path{fields: []reflect.StructField{subField, subField}})
+ assert.False(t, v.IsValid())
+}