func TestParseTCPAddr(t *testing.T) { p := parserMixin{} v := p.TCP() err := p.value.Set("127.0.0.1:1234") assert.NoError(t, err) expected, err := net.ResolveTCPAddr("tcp", "127.0.0.1:1234") assert.NoError(t, err) assert.Equal(t, *expected, **v) }
func TestParseTCPAddrList(t *testing.T) { p := parserMixin{} _ = p.TCPList() err := p.value.Set("127.0.0.1:1234") assert.NoError(t, err) err = p.value.Set("127.0.0.1:1235") assert.NoError(t, err) assert.Equal(t, "127.0.0.1:1234,127.0.0.1:1235", p.value.String()) }
func TestParseBase2Bytes(t *testing.T) { n, err := ParseBase2Bytes("0B") assert.NoError(t, err) assert.Equal(t, 0, n) n, err = ParseBase2Bytes("1KB") assert.NoError(t, err) assert.Equal(t, 1024, n) n, err = ParseBase2Bytes("1MB1KB25B") assert.NoError(t, err) assert.Equal(t, 1049625, n) n, err = ParseBase2Bytes("1.5MB") assert.NoError(t, err) assert.Equal(t, 1572864, n) }
func TestParseMetricBytes(t *testing.T) { n, err := ParseMetricBytes("0B") assert.NoError(t, err) assert.Equal(t, 0, n) n, err = ParseMetricBytes("1KB1B") assert.NoError(t, err) assert.Equal(t, 1001, n) n, err = ParseMetricBytes("1MB1KB25B") assert.NoError(t, err) assert.Equal(t, 1001025, n) n, err = ParseMetricBytes("1.5MB") assert.NoError(t, err) assert.Equal(t, 1500000, n) }
func TestNestedCommandsWithFlags(t *testing.T) { app := New("app", "") cmd := app.Command("a", "").Command("b", "") a := cmd.Flag("aaa", "").Short('a').String() b := cmd.Flag("bbb", "").Short('b').String() err := app.init() assert.NoError(t, err) context := tokenize(strings.Split("a b --aaa x -b x", " ")) selected, err := parseAndExecute(app, context) assert.NoError(t, err) assert.True(t, context.EOL()) assert.Equal(t, "a b", selected) assert.Equal(t, "x", *a) assert.Equal(t, "x", *b) }
func TestCommander(t *testing.T) { c := New("test", "test") ping := c.Command("ping", "Ping an IP address.") pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration() selected, err := c.Parse([]string{"ping"}) assert.NoError(t, err) assert.Equal(t, "ping", selected) assert.Equal(t, 5*time.Second, *pingTTL) selected, err = c.Parse([]string{"ping", "--ttl=10s"}) assert.NoError(t, err) assert.Equal(t, "ping", selected) assert.Equal(t, 10*time.Second, *pingTTL) }
func TestBool(t *testing.T) { app := New("test", "") b := app.Flag("b", "").Bool() _, err := app.Parse([]string{"--b"}) assert.NoError(t, err) assert.True(t, *b) }
func TestShortFlag(t *testing.T) { app := New("test", "") f := app.Flag("long", "").Short('s').Bool() _, err := app.Parse([]string{"-s"}) assert.NoError(t, err) assert.True(t, *f) }
func TestParserExpandFromFile(t *testing.T) { f, err := ioutil.TempFile("", "") assert.NoError(t, err) defer os.Remove(f.Name()) f.WriteString("hello\nworld\n") f.Close() app := New("test", "") arg0 := app.Arg("arg0", "").String() arg1 := app.Arg("arg1", "").String() _, err = app.Parse([]string{"@" + f.Name()}) assert.NoError(t, err) assert.Equal(t, "hello", *arg0) assert.Equal(t, "world", *arg1) }
func TestTopLevelArgWorks(t *testing.T) { c := New("test", "test") s := c.Arg("arg", "help").String() _, err := c.Parse([]string{"foo"}) assert.NoError(t, err) assert.Equal(t, "foo", *s) }
func TestCombinedShortFlagArg(t *testing.T) { a := New("test", "") n := a.Flag("short", "").Short('s').Int() _, err := a.Parse([]string{"-s10"}) assert.NoError(t, err) assert.Equal(t, 10, *n) }
func TestFloat32(t *testing.T) { p := parserMixin{} v := p.Float32() err := p.value.Set("123.45") assert.NoError(t, err) assert.InEpsilon(t, 123.45, *v, 0.001) }
func TestTooManyArgsAfterCommand(t *testing.T) { a := New("test", "test") a.Command("a", "") assert.NoError(t, a.init()) _, err := a.Parse([]string{"a", "b"}) assert.Error(t, err) }
func TestNestedCommandWithMergedFlags(t *testing.T) { app := New("app", "") cmd0 := app.Command("a", "") cmd0f0 := cmd0.Flag("aflag", "").Bool() // cmd1 := app.Command("b", "") // cmd1f0 := cmd0.Flag("bflag", "").Bool() cmd00 := cmd0.Command("aa", "") cmd00f0 := cmd00.Flag("aaflag", "").Bool() err := app.init() assert.NoError(t, err) context := tokenize(strings.Split("a aa --aflag --aaflag", " ")) selected, err := parseAndExecute(app, context) assert.NoError(t, err) assert.True(t, *cmd0f0) assert.True(t, *cmd00f0) assert.Equal(t, "a aa", selected) }
func TestSelectedCommand(t *testing.T) { app := New("test", "help") c0 := app.Command("c0", "") c0.Command("c1", "") s, err := app.Parse([]string{"c0", "c1"}) assert.NoError(t, err) assert.Equal(t, "c0 c1", s) }
func TestArgRemainder(t *testing.T) { app := New("test", "") v := app.Arg("test", "").Strings() args := []string{"hello", "world"} _, err := app.Parse(args) assert.NoError(t, err) assert.Equal(t, args, *v) }
func TestRequiredWithEnvar(t *testing.T) { os.Setenv("TEST_ENVAR", "123") app := New("test", "") flag := app.Flag("t", "").OverrideDefaultFromEnvar("TEST_ENVAR").Required().Int() _, err := app.Parse([]string{}) assert.NoError(t, err) assert.Equal(t, 123, *flag) }
func TestParseURL(t *testing.T) { p := parserMixin{} v := p.URL() p.value.Set("http://w3.org") u, err := url.Parse("http://w3.org") assert.NoError(t, err) assert.Equal(t, *u, **v) }
func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) { app := New("test", "") flag := app.Flag("flag", "").Default("default").String() app.Command("cmd", "") _, err := app.Parse([]string{"--flag=123", "cmd"}) assert.NoError(t, err) assert.Equal(t, "123", *flag) }
func TestCommandParseDoesNotFailRequired(t *testing.T) { app := New("test", "") flag := app.Flag("flag", "").Required().String() app.Command("cmd", "") _, err := app.Parse([]string{"cmd", "--flag=123"}) assert.NoError(t, err) assert.Equal(t, "123", *flag) }
func TestEnum(t *testing.T) { app := New("", "") a := app.Arg("a", "").Enum("one", "two", "three") _, err := app.Parse([]string{"moo"}) assert.Error(t, err) _, err = app.Parse([]string{"one"}) assert.NoError(t, err) assert.Equal(t, "one", *a) }
func TestRequiredFlags(t *testing.T) { c := New("test", "test") c.Flag("a", "a").String() c.Flag("b", "b").Required().String() _, err := c.Parse([]string{"--a=foo"}) assert.Error(t, err) _, err = c.Parse([]string{"--b=foo"}) assert.NoError(t, err) }
func TestDefaultSubcommandEOL(t *testing.T) { app := New("app", "").Terminate(nil) c0 := app.Command("c0", "").Default() c0.Command("c01", "").Default() c0.Command("c02", "") cmd, err := app.Parse([]string{"c0"}) assert.NoError(t, err) assert.Equal(t, "c0 c01", cmd) }
func TestNoBool(t *testing.T) { fg := newFlagGroup() f := fg.Flag("b", "").Default("true") b := f.Bool() fg.init() tokens := tokenize([]string{"--no-b"}) _, err := fg.parse(tokens) assert.NoError(t, err) assert.False(t, *b) }
func TestCombinedShortFlags(t *testing.T) { app := New("test", "") a := app.Flag("short0", "").Short('0').Bool() b := app.Flag("short1", "").Short('1').Bool() c := app.Flag("short2", "").Short('2').Bool() _, err := app.Parse([]string{"-01"}) assert.NoError(t, err) assert.True(t, *a) assert.True(t, *b) assert.False(t, *c) }
func TestInterspersedFalse(t *testing.T) { app := New("test", "help").Interspersed(false) a1 := app.Arg("a1", "").String() a2 := app.Arg("a2", "").String() f1 := app.Flag("flag", "").String() _, err := app.Parse([]string{"a1", "--flag=flag"}) assert.NoError(t, err) assert.Equal(t, "a1", *a1) assert.Equal(t, "--flag=flag", *a2) assert.Equal(t, "", *f1) }
func TestArgsMultipleRequiredThenNonRequired(t *testing.T) { c := New("test", "test").Terminate(nil).Writer(ioutil.Discard) cmd := c.Command("cmd", "") cmd.Arg("a", "a").Required().String() cmd.Arg("b", "b").Required().String() cmd.Arg("c", "c").String() cmd.Arg("d", "d").String() _, err := c.Parse([]string{"cmd", "a", "b"}) assert.NoError(t, err) _, err = c.Parse([]string{}) assert.Error(t, err) }
func TestDispatchCallbackIsCalled(t *testing.T) { dispatched := false c := New("test", "") c.Command("cmd", "").Action(func(*ParseContext) error { dispatched = true return nil }) _, err := c.Parse([]string{"cmd"}) assert.NoError(t, err) assert.True(t, dispatched) }
func TestRequiredFlag(t *testing.T) { app := New("test", "") app.Version("0.0.0").Writer(ioutil.Discard) exits := 0 app.Terminate(func(int) { exits++ }) app.Flag("a", "").Required().Bool() _, err := app.Parse([]string{"--a"}) assert.NoError(t, err) _, err = app.Parse([]string{}) assert.Error(t, err) _, err = app.Parse([]string{"--version"}) assert.Equal(t, 1, exits) }
func TestNestedCommandsWithArgs(t *testing.T) { app := New("app", "") cmd := app.Command("a", "").Command("b", "") a := cmd.Arg("a", "").String() b := cmd.Arg("b", "").String() context := tokenize([]string{"a", "b", "c", "d"}) selected, err := parseAndExecute(app, context) assert.NoError(t, err) assert.True(t, context.EOL()) assert.Equal(t, "a b", selected) assert.Equal(t, "c", *a) assert.Equal(t, "d", *b) }