2015-10-31 20:26:58 -05:00
|
|
|
package arg
|
2015-10-31 18:15:24 -05:00
|
|
|
|
|
|
|
import (
|
2016-01-23 22:55:40 -06:00
|
|
|
"net"
|
2016-01-23 22:58:43 -06:00
|
|
|
"net/mail"
|
2015-11-04 12:27:17 -06:00
|
|
|
"os"
|
2015-10-31 18:15:24 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-01-23 20:28:35 -06:00
|
|
|
"time"
|
2015-10-31 18:15:24 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2016-07-31 11:14:44 -05:00
|
|
|
func setenv(t *testing.T, name, val string) {
|
|
|
|
if err := os.Setenv(name, val); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
func parse(cmdline string, dest interface{}) error {
|
2016-01-18 12:31:01 -06:00
|
|
|
p, err := NewParser(Config{}, dest)
|
2015-11-01 01:57:26 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-23 21:40:15 -06:00
|
|
|
var parts []string
|
|
|
|
if len(cmdline) > 0 {
|
|
|
|
parts = strings.Split(cmdline, " ")
|
|
|
|
}
|
|
|
|
return p.Parse(parts)
|
2015-10-31 18:15:24 -05:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:28:35 -06:00
|
|
|
func TestString(t *testing.T) {
|
2015-10-31 18:15:24 -05:00
|
|
|
var args struct {
|
|
|
|
Foo string
|
2017-02-15 20:37:19 -06:00
|
|
|
Ptr *string
|
2015-10-31 18:15:24 -05:00
|
|
|
}
|
2017-02-15 20:37:19 -06:00
|
|
|
err := parse("--foo bar --ptr baz", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", args.Foo)
|
2017-02-15 20:37:19 -06:00
|
|
|
assert.Equal(t, "baz", *args.Ptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBool(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
A bool
|
|
|
|
B bool
|
|
|
|
C *bool
|
|
|
|
D *bool
|
|
|
|
}
|
|
|
|
err := parse("--a --c", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, args.A)
|
|
|
|
assert.False(t, args.B)
|
|
|
|
assert.True(t, *args.C)
|
|
|
|
assert.Nil(t, args.D)
|
2015-10-31 18:15:24 -05:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:28:35 -06:00
|
|
|
func TestInt(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo int
|
2017-02-15 20:37:19 -06:00
|
|
|
Ptr *int
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
2017-02-15 20:37:19 -06:00
|
|
|
err := parse("--foo 7 --ptr 8", &args)
|
2016-01-23 20:28:35 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 7, args.Foo)
|
2017-02-15 20:37:19 -06:00
|
|
|
assert.EqualValues(t, 8, *args.Ptr)
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUint(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo uint
|
2017-02-15 20:37:19 -06:00
|
|
|
Ptr *uint
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
2017-02-15 20:37:19 -06:00
|
|
|
err := parse("--foo 7 --ptr 8", &args)
|
2016-01-23 20:28:35 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 7, args.Foo)
|
2017-02-15 20:37:19 -06:00
|
|
|
assert.EqualValues(t, 8, *args.Ptr)
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFloat(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo float32
|
2017-02-15 20:37:19 -06:00
|
|
|
Ptr *float32
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
2017-02-15 20:37:19 -06:00
|
|
|
err := parse("--foo 3.4 --ptr 3.5", &args)
|
2016-01-23 20:28:35 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 3.4, args.Foo)
|
2017-02-15 20:37:19 -06:00
|
|
|
assert.EqualValues(t, 3.5, *args.Ptr)
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuration(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo time.Duration
|
2017-02-15 20:37:19 -06:00
|
|
|
Ptr *time.Duration
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
2017-02-15 20:37:19 -06:00
|
|
|
err := parse("--foo 3ms --ptr 4ms", &args)
|
2016-01-23 20:28:35 -06:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 3*time.Millisecond, args.Foo)
|
2017-02-15 20:37:19 -06:00
|
|
|
assert.Equal(t, 4*time.Millisecond, *args.Ptr)
|
2016-01-23 20:28:35 -06:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:35:08 -06:00
|
|
|
func TestInvalidDuration(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo time.Duration
|
|
|
|
}
|
|
|
|
err := parse("--foo xxx", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2016-01-23 21:40:15 -06:00
|
|
|
func TestIntPtr(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo *int
|
|
|
|
}
|
|
|
|
err := parse("--foo 123", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, args.Foo)
|
|
|
|
assert.Equal(t, 123, *args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntPtrNotPresent(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo *int
|
|
|
|
}
|
|
|
|
err := parse("", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, args.Foo)
|
|
|
|
}
|
|
|
|
|
2015-10-31 18:15:24 -05:00
|
|
|
func TestMixed(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"-f"`
|
|
|
|
Bar int
|
2017-10-02 08:18:41 -05:00
|
|
|
Baz uint `arg:"positional"`
|
2015-10-31 18:15:24 -05:00
|
|
|
Ham bool
|
|
|
|
Spam float32
|
|
|
|
}
|
|
|
|
args.Bar = 3
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("123 -spam=1.2 -ham -f xyz", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
assert.Equal(t, 3, args.Bar)
|
2015-10-31 19:05:14 -05:00
|
|
|
assert.Equal(t, uint(123), args.Baz)
|
2015-10-31 18:15:24 -05:00
|
|
|
assert.Equal(t, true, args.Ham)
|
2015-11-01 15:38:04 -06:00
|
|
|
assert.EqualValues(t, 1.2, args.Spam)
|
2015-10-31 18:15:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequired(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"required"`
|
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.Error(t, err, "--foo is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShortFlag(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"-f"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("-f xyz", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
err = parse("-foo xyz", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
err = parse("--foo xyz", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:27:17 -06:00
|
|
|
func TestInvalidShortFlag(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"-foo"`
|
|
|
|
}
|
|
|
|
err := parse("", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLongFlag(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"--abc"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse("-abc xyz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
|
|
|
|
err = parse("--abc xyz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
}
|
|
|
|
|
2015-10-31 18:15:24 -05:00
|
|
|
func TestCaseSensitive(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Lower bool `arg:"-v"`
|
|
|
|
Upper bool `arg:"-V"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("-v", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, args.Lower)
|
|
|
|
assert.False(t, args.Upper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaseSensitive2(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Lower bool `arg:"-v"`
|
|
|
|
Upper bool `arg:"-V"`
|
|
|
|
}
|
|
|
|
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("-V", &args)
|
2015-10-31 18:15:24 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.False(t, args.Lower)
|
|
|
|
assert.True(t, args.Upper)
|
|
|
|
}
|
2015-10-31 19:05:14 -05:00
|
|
|
|
|
|
|
func TestPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Output string `arg:"positional"`
|
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("foo", &args)
|
2015-10-31 19:05:14 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", args.Input)
|
|
|
|
assert.Equal(t, "", args.Output)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:27:17 -06:00
|
|
|
func TestPositionalPointer(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Output []*string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("foo bar baz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", args.Input)
|
|
|
|
bar := "bar"
|
|
|
|
baz := "baz"
|
|
|
|
assert.Equal(t, []*string{&bar, &baz}, args.Output)
|
|
|
|
}
|
|
|
|
|
2015-10-31 19:05:14 -05:00
|
|
|
func TestRequiredPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Output string `arg:"positional,required"`
|
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("foo", &args)
|
2015-10-31 19:05:14 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2017-03-30 13:47:59 -05:00
|
|
|
func TestRequiredPositionalMultiple(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Multiple []string `arg:"positional,required"`
|
|
|
|
}
|
|
|
|
err := parse("foo", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-10-31 19:05:14 -05:00
|
|
|
func TestTooManyPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Output string `arg:"positional"`
|
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("foo bar baz", &args)
|
2015-10-31 19:05:14 -05:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiple(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []int
|
|
|
|
Bar []string
|
|
|
|
}
|
2015-11-01 01:57:26 -05:00
|
|
|
err := parse("--foo 1 2 3 --bar x y z", &args)
|
2015-10-31 19:05:14 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []int{1, 2, 3}, args.Foo)
|
|
|
|
assert.Equal(t, []string{"x", "y", "z"}, args.Bar)
|
|
|
|
}
|
2015-11-04 12:27:17 -06:00
|
|
|
|
2017-03-30 13:47:59 -05:00
|
|
|
func TestMultiplePositionals(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Input string `arg:"positional"`
|
|
|
|
Multiple []string `arg:"positional,required"`
|
|
|
|
}
|
|
|
|
err := parse("foo a b c", &args)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "foo", args.Input)
|
|
|
|
assert.Equal(t, []string{"a", "b", "c"}, args.Multiple)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:27:17 -06:00
|
|
|
func TestMultipleWithEq(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []int
|
|
|
|
Bar []string
|
|
|
|
}
|
|
|
|
err := parse("--foo 1 2 3 --bar=x", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []int{1, 2, 3}, args.Foo)
|
|
|
|
assert.Equal(t, []string{"x"}, args.Bar)
|
|
|
|
}
|
|
|
|
|
2016-02-29 15:05:26 -06:00
|
|
|
func TestMultipleWithDefault(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []int
|
|
|
|
Bar []string
|
|
|
|
}
|
|
|
|
args.Foo = []int{42}
|
|
|
|
args.Bar = []string{"foo"}
|
|
|
|
err := parse("--foo 1 2 3 --bar x y z", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []int{1, 2, 3}, args.Foo)
|
|
|
|
assert.Equal(t, []string{"x", "y", "z"}, args.Bar)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:27:17 -06:00
|
|
|
func TestExemptField(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
Bar interface{} `arg:"-"`
|
|
|
|
}
|
|
|
|
err := parse("--foo xyz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "xyz", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownField(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
err := parse("--bar xyz", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMissingRequired(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"required"`
|
|
|
|
X []string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("x", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2017-09-16 06:05:53 -05:00
|
|
|
func TestNonsenseKey(t *testing.T) {
|
2017-03-30 13:47:59 -05:00
|
|
|
var args struct {
|
2017-09-16 06:05:53 -05:00
|
|
|
X []string `arg:"positional, nonsense"`
|
2017-03-30 13:47:59 -05:00
|
|
|
}
|
|
|
|
err := parse("x", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2015-11-04 12:27:17 -06:00
|
|
|
func TestMissingValue(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
err := parse("--foo", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidInt(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo int
|
|
|
|
}
|
|
|
|
err := parse("--foo=xyz", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidUint(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo uint
|
|
|
|
}
|
|
|
|
err := parse("--foo=xyz", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidFloat(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo float64
|
|
|
|
}
|
|
|
|
err := parse("--foo xyz", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidBool(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo bool
|
|
|
|
}
|
|
|
|
err := parse("--foo=xyz", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidIntSlice(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []int
|
|
|
|
}
|
|
|
|
err := parse("--foo 1 2 xyz", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo int `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("xyz", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidPositionalSlice(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []int `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("1 2 xyz", &args)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoMoreOptions(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
Bar []string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("abc -- --foo xyz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "", args.Foo)
|
|
|
|
assert.Equal(t, []string{"abc", "--foo", "xyz"}, args.Bar)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHelpFlag(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
Bar interface{} `arg:"-"`
|
|
|
|
}
|
|
|
|
err := parse("--help", &args)
|
|
|
|
assert.Equal(t, ErrHelp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPanicOnNonPointer(t *testing.T) {
|
|
|
|
var args struct{}
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
parse("", args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPanicOnNonStruct(t *testing.T) {
|
|
|
|
var args string
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
parse("", &args)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupportedType(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo interface{}
|
|
|
|
}
|
|
|
|
err := parse("--foo", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupportedSliceElement(t *testing.T) {
|
2016-01-23 21:40:15 -06:00
|
|
|
var args struct {
|
|
|
|
Foo []interface{}
|
|
|
|
}
|
|
|
|
err := parse("--foo 3", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsupportedSliceElementMissingValue(t *testing.T) {
|
2015-11-04 12:27:17 -06:00
|
|
|
var args struct {
|
|
|
|
Foo []interface{}
|
|
|
|
}
|
|
|
|
err := parse("--foo", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownTag(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"this_is_not_valid"`
|
|
|
|
}
|
|
|
|
err := parse("--foo xyz", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
os.Args = []string{"example", "--foo", "bar"}
|
|
|
|
err := Parse(&args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "bar", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseError(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"this_is_not_valid"`
|
|
|
|
}
|
|
|
|
os.Args = []string{"example", "--bar"}
|
|
|
|
err := Parse(&args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMustParse(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
os.Args = []string{"example", "--foo", "bar"}
|
2016-01-05 15:52:33 -06:00
|
|
|
parser := MustParse(&args)
|
2015-11-04 12:27:17 -06:00
|
|
|
assert.Equal(t, "bar", args.Foo)
|
2016-01-05 15:52:33 -06:00
|
|
|
assert.NotNil(t, parser)
|
2015-11-04 12:27:17 -06:00
|
|
|
}
|
2016-01-18 12:42:04 -06:00
|
|
|
|
|
|
|
func TestEnvironmentVariable(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"env"`
|
|
|
|
}
|
2016-07-31 11:14:44 -05:00
|
|
|
setenv(t, "FOO", "bar")
|
2016-01-18 12:42:04 -06:00
|
|
|
os.Args = []string{"example"}
|
|
|
|
MustParse(&args)
|
|
|
|
assert.Equal(t, "bar", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentVariableOverrideName(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"env:BAZ"`
|
|
|
|
}
|
2016-07-31 11:14:44 -05:00
|
|
|
setenv(t, "BAZ", "bar")
|
2016-01-18 12:42:04 -06:00
|
|
|
os.Args = []string{"example"}
|
|
|
|
MustParse(&args)
|
|
|
|
assert.Equal(t, "bar", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentVariableOverrideArgument(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"env"`
|
|
|
|
}
|
2016-07-31 11:14:44 -05:00
|
|
|
setenv(t, "FOO", "bar")
|
2016-01-18 12:42:04 -06:00
|
|
|
os.Args = []string{"example", "--foo", "baz"}
|
|
|
|
MustParse(&args)
|
|
|
|
assert.Equal(t, "baz", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentVariableError(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo int `arg:"env"`
|
|
|
|
}
|
2016-07-31 11:14:44 -05:00
|
|
|
setenv(t, "FOO", "bar")
|
2016-01-18 12:42:04 -06:00
|
|
|
os.Args = []string{"example"}
|
|
|
|
err := Parse(&args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvironmentVariableRequired(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"env,required"`
|
|
|
|
}
|
2016-07-31 11:14:44 -05:00
|
|
|
setenv(t, "FOO", "bar")
|
2016-01-18 12:42:04 -06:00
|
|
|
os.Args = []string{"example"}
|
|
|
|
MustParse(&args)
|
|
|
|
assert.Equal(t, "bar", args.Foo)
|
|
|
|
}
|
2016-01-23 21:40:15 -06:00
|
|
|
|
|
|
|
type textUnmarshaler struct {
|
|
|
|
val int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *textUnmarshaler) UnmarshalText(b []byte) error {
|
|
|
|
f.val = len(b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTextUnmarshaler(t *testing.T) {
|
|
|
|
// fields that implement TextUnmarshaler should be parsed using that interface
|
|
|
|
var args struct {
|
|
|
|
Foo *textUnmarshaler
|
|
|
|
}
|
|
|
|
err := parse("--foo abc", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, args.Foo.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
type boolUnmarshaler bool
|
|
|
|
|
|
|
|
func (p *boolUnmarshaler) UnmarshalText(b []byte) error {
|
|
|
|
*p = len(b)%2 == 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolUnmarhsaler(t *testing.T) {
|
|
|
|
// test that a bool type that implements TextUnmarshaler is
|
|
|
|
// handled as a TextUnmarshaler not as a bool
|
|
|
|
var args struct {
|
|
|
|
Foo *boolUnmarshaler
|
|
|
|
}
|
|
|
|
err := parse("--foo ab", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, true, *args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type sliceUnmarshaler []int
|
|
|
|
|
|
|
|
func (p *sliceUnmarshaler) UnmarshalText(b []byte) error {
|
|
|
|
*p = sliceUnmarshaler{len(b)}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSliceUnmarhsaler(t *testing.T) {
|
|
|
|
// test that a slice type that implements TextUnmarshaler is
|
|
|
|
// handled as a TextUnmarshaler not as a slice
|
|
|
|
var args struct {
|
|
|
|
Foo *sliceUnmarshaler
|
|
|
|
Bar string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("--foo abcde xyz", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, *args.Foo, 1)
|
|
|
|
assert.EqualValues(t, 5, (*args.Foo)[0])
|
|
|
|
assert.Equal(t, "xyz", args.Bar)
|
|
|
|
}
|
2016-01-23 22:55:40 -06:00
|
|
|
|
|
|
|
func TestIP(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host net.IP
|
|
|
|
}
|
|
|
|
err := parse("--host 192.168.0.1", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "192.168.0.1", args.Host.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPtrToIP(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host *net.IP
|
|
|
|
}
|
|
|
|
err := parse("--host 192.168.0.1", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "192.168.0.1", args.Host.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIPSlice(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host []net.IP
|
|
|
|
}
|
|
|
|
err := parse("--host 192.168.0.1 127.0.0.1", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, args.Host, 2)
|
|
|
|
assert.Equal(t, "192.168.0.1", args.Host[0].String())
|
|
|
|
assert.Equal(t, "127.0.0.1", args.Host[1].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidIPAddress(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host net.IP
|
|
|
|
}
|
|
|
|
err := parse("--host xxx", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2016-01-23 22:58:43 -06:00
|
|
|
|
|
|
|
func TestMAC(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host net.HardwareAddr
|
|
|
|
}
|
|
|
|
err := parse("--host 0123.4567.89ab", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "01:23:45:67:89:ab", args.Host.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidMac(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Host net.HardwareAddr
|
|
|
|
}
|
|
|
|
err := parse("--host xxx", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMailAddr(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Recipient mail.Address
|
|
|
|
}
|
|
|
|
err := parse("--recipient foo@example.com", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "<foo@example.com>", args.Recipient.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidMailAddr(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Recipient mail.Address
|
|
|
|
}
|
|
|
|
err := parse("--recipient xxx", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2016-10-09 19:18:28 -05:00
|
|
|
|
|
|
|
type A struct {
|
|
|
|
X string
|
|
|
|
}
|
|
|
|
|
|
|
|
type B struct {
|
|
|
|
Y int
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmbedded(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
A
|
|
|
|
B
|
|
|
|
Z bool
|
|
|
|
}
|
|
|
|
err := parse("--x=hello --y=321 --z", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "hello", args.X)
|
|
|
|
assert.Equal(t, 321, args.Y)
|
|
|
|
assert.Equal(t, true, args.Z)
|
|
|
|
}
|
2017-02-09 17:12:33 -06:00
|
|
|
|
|
|
|
func TestEmptyArgs(t *testing.T) {
|
|
|
|
origArgs := os.Args
|
|
|
|
|
|
|
|
// test what happens if somehow os.Args is empty
|
|
|
|
os.Args = nil
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
MustParse(&args)
|
|
|
|
|
|
|
|
// put the original arguments back
|
|
|
|
os.Args = origArgs
|
|
|
|
}
|
2017-02-21 11:08:08 -06:00
|
|
|
|
|
|
|
func TestTooManyHyphens(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
TooManyHyphens string `arg:"---x"`
|
|
|
|
}
|
|
|
|
err := parse("--foo -", &args)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHyphenAsOption(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
err := parse("--foo -", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "-", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHyphenAsPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("-", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "-", args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHyphenInMultiOption(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []string
|
|
|
|
Bar int
|
|
|
|
}
|
|
|
|
err := parse("--foo --- x - y --bar 3", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"---", "x", "-", "y"}, args.Foo)
|
|
|
|
assert.Equal(t, 3, args.Bar)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHyphenInMultiPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []string `arg:"positional"`
|
|
|
|
}
|
|
|
|
err := parse("--- x - y", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"---", "x", "-", "y"}, args.Foo)
|
|
|
|
}
|
2017-03-03 06:12:17 -06:00
|
|
|
|
|
|
|
func TestSeparate(t *testing.T) {
|
|
|
|
for _, val := range []string{"-f one", "-f=one", "--foo one", "--foo=one"} {
|
|
|
|
var args struct {
|
|
|
|
Foo []string `arg:"--foo,-f,separate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse(val, &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"one"}, args.Foo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeparateWithDefault(t *testing.T) {
|
|
|
|
args := struct {
|
|
|
|
Foo []string `arg:"--foo,-f,separate"`
|
|
|
|
}{
|
|
|
|
Foo: []string{"default"},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse("-f one -f=two", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"default", "one", "two"}, args.Foo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeparateWithPositional(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []string `arg:"--foo,-f,separate"`
|
|
|
|
Bar string `arg:"positional"`
|
|
|
|
Moo string `arg:"positional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse("zzz --foo one -f=two --foo=three -f four aaa", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"one", "two", "three", "four"}, args.Foo)
|
|
|
|
assert.Equal(t, "zzz", args.Bar)
|
|
|
|
assert.Equal(t, "aaa", args.Moo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeparatePositionalInterweaved(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []string `arg:"--foo,-f,separate"`
|
|
|
|
Bar []string `arg:"--bar,-b,separate"`
|
|
|
|
Pre string `arg:"positional"`
|
|
|
|
Post []string `arg:"positional"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse("zzz -f foo1 -b=bar1 --foo=foo2 -b bar2 post1 -b bar3 post2 post3", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"foo1", "foo2"}, args.Foo)
|
|
|
|
assert.Equal(t, []string{"bar1", "bar2", "bar3"}, args.Bar)
|
|
|
|
assert.Equal(t, "zzz", args.Pre)
|
|
|
|
assert.Equal(t, []string{"post1", "post2", "post3"}, args.Post)
|
|
|
|
}
|
2017-09-16 06:05:53 -05:00
|
|
|
|
|
|
|
func TestSpacesAllowedInTags(t *testing.T) {
|
|
|
|
var args struct {
|
|
|
|
Foo []string `arg:"--foo, -f, separate, required, help:quite nice really"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err := parse("--foo one -f=two --foo=three -f four", &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{"one", "two", "three", "four"}, args.Foo)
|
|
|
|
}
|