2015-07-20 19:18:25 +00:00
|
|
|
package cli
|
2013-09-09 18:51:46 +00:00
|
|
|
|
|
|
|
import (
|
2014-04-15 14:16:47 +00:00
|
|
|
"fmt"
|
2014-07-11 22:13:10 +00:00
|
|
|
"os"
|
2014-03-19 02:26:57 +00:00
|
|
|
"reflect"
|
2016-04-25 22:10:10 +00:00
|
|
|
"runtime"
|
2014-04-15 14:16:47 +00:00
|
|
|
"strings"
|
2013-09-09 18:51:46 +00:00
|
|
|
"testing"
|
2016-05-02 17:05:21 +00:00
|
|
|
"time"
|
2013-09-09 18:51:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var boolFlagTests = []struct {
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{"help", "--help\t"},
|
|
|
|
{"h", "-h\t"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBoolFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range boolFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := BoolFlag{Name: test.name}
|
2013-09-09 18:51:46 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
2016-05-02 17:05:21 +00:00
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
2013-09-09 18:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringFlagTests = []struct {
|
|
|
|
name string
|
2016-04-22 03:02:06 +00:00
|
|
|
usage string
|
2014-04-11 19:09:59 +00:00
|
|
|
value string
|
2013-09-09 18:51:46 +00:00
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"foo", "", "", "--foo value\t"},
|
|
|
|
{"f", "", "", "-f value\t"},
|
|
|
|
{"f", "The total `foo` desired", "all", "-f foo\tThe total foo desired (default: \"all\")"},
|
|
|
|
{"test", "", "Something", "--test value\t(default: \"Something\")"},
|
2016-05-02 17:05:21 +00:00
|
|
|
{"config,c", "Load configuration from `FILE`", "", "--config FILE, -c FILE\tLoad configuration from FILE"},
|
2016-05-02 23:42:08 +00:00
|
|
|
{"config,c", "Load configuration from `CONFIG`", "config.json", "--config CONFIG, -c CONFIG\tLoad configuration from CONFIG (default: \"config.json\")"},
|
2013-09-09 18:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range stringFlagTests {
|
2016-04-22 03:02:06 +00:00
|
|
|
flag := StringFlag{Name: test.name, Usage: test.usage, Value: test.value}
|
2013-09-09 18:51:46 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
2016-05-02 17:05:21 +00:00
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
2013-09-09 18:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_FOO", "derp")
|
|
|
|
for _, test := range stringFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := StringFlag{Name: test.name, Value: test.value, EnvVar: "APP_FOO"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_FOO]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_FOO%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%s does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringSliceFlagTests = []struct {
|
|
|
|
name string
|
2015-07-20 19:18:25 +00:00
|
|
|
value *StringSlice
|
2014-07-11 22:13:10 +00:00
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"foo", func() *StringSlice {
|
2015-07-20 19:18:25 +00:00
|
|
|
s := &StringSlice{}
|
2014-07-11 22:30:16 +00:00
|
|
|
s.Set("")
|
|
|
|
return s
|
2016-05-02 23:42:08 +00:00
|
|
|
}(), "--foo value\t"},
|
|
|
|
{"f", func() *StringSlice {
|
2015-07-20 19:18:25 +00:00
|
|
|
s := &StringSlice{}
|
2014-07-11 22:30:16 +00:00
|
|
|
s.Set("")
|
|
|
|
return s
|
2016-05-02 23:42:08 +00:00
|
|
|
}(), "-f value\t"},
|
|
|
|
{"f", func() *StringSlice {
|
2015-07-20 19:18:25 +00:00
|
|
|
s := &StringSlice{}
|
2016-05-02 23:42:08 +00:00
|
|
|
s.Set("Lipstick")
|
2014-07-11 22:30:16 +00:00
|
|
|
return s
|
2016-05-02 23:42:08 +00:00
|
|
|
}(), "-f value\t(default: \"Lipstick\")"},
|
2015-07-20 19:18:25 +00:00
|
|
|
{"test", func() *StringSlice {
|
|
|
|
s := &StringSlice{}
|
2014-07-11 22:30:16 +00:00
|
|
|
s.Set("Something")
|
|
|
|
return s
|
2016-05-02 23:42:08 +00:00
|
|
|
}(), "--test value\t(default: \"Something\")"},
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringSliceFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range stringSliceFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := StringSliceFlag{Name: test.name, Value: test.value}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_QWWX", "11,4")
|
|
|
|
for _, test := range stringSliceFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := StringSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_QWWX"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_QWWX]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_QWWX%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%q does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 18:51:46 +00:00
|
|
|
var intFlagTests = []struct {
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"hats", "--hats value\t(default: 9)"},
|
|
|
|
{"H", "-H value\t(default: 9)"},
|
2013-09-09 18:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range intFlagTests {
|
2016-05-02 23:42:08 +00:00
|
|
|
flag := IntFlag{Name: test.name, Value: 9}
|
2013-09-09 18:51:46 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("%s does not match %s", output, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestIntFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_BAR", "2")
|
|
|
|
for _, test := range intFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := IntFlag{Name: test.name, EnvVar: "APP_BAR"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_BAR]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_BAR%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%s does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-02 21:32:32 +00:00
|
|
|
var durationFlagTests = []struct {
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"hooting", "--hooting value\t(default: 1s)"},
|
|
|
|
{"H", "-H value\t(default: 1s)"},
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range durationFlagTests {
|
2016-05-02 17:05:21 +00:00
|
|
|
flag := DurationFlag{Name: test.name, Value: 1 * time.Second}
|
2014-08-02 21:32:32 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
2016-05-02 17:05:21 +00:00
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-08-02 21:32:32 +00:00
|
|
|
os.Setenv("APP_BAR", "2h3m6s")
|
|
|
|
for _, test := range durationFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := DurationFlag{Name: test.name, EnvVar: "APP_BAR"}
|
2014-08-02 21:32:32 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_BAR]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_BAR%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%s does not end with"+expectedSuffix, output)
|
2014-08-02 21:32:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
var intSliceFlagTests = []struct {
|
|
|
|
name string
|
2015-07-20 19:18:25 +00:00
|
|
|
value *IntSlice
|
2014-07-11 22:13:10 +00:00
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"heads", &IntSlice{}, "--heads value\t"},
|
|
|
|
{"H", &IntSlice{}, "-H value\t"},
|
|
|
|
{"H, heads", func() *IntSlice {
|
2015-07-20 19:18:25 +00:00
|
|
|
i := &IntSlice{}
|
2014-07-11 22:30:16 +00:00
|
|
|
i.Set("9")
|
2016-05-02 23:42:08 +00:00
|
|
|
i.Set("3")
|
2014-07-11 22:30:16 +00:00
|
|
|
return i
|
2016-05-02 23:42:08 +00:00
|
|
|
}(), "-H value, --heads value\t(default: 9, 3)"},
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntSliceFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range intSliceFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := IntSliceFlag{Name: test.name, Value: test.value}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_SMURF", "42,3")
|
|
|
|
for _, test := range intSliceFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := IntSliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_SMURF"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_SMURF]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_SMURF%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%q does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 13:42:09 +00:00
|
|
|
var float64FlagTests = []struct {
|
|
|
|
name string
|
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"hooting", "--hooting value\t(default: 0.1)"},
|
|
|
|
{"H", "-H value\t(default: 0.1)"},
|
2013-12-03 13:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFloat64FlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range float64FlagTests {
|
2016-05-02 17:05:21 +00:00
|
|
|
flag := Float64Flag{Name: test.name, Value: float64(0.1)}
|
2013-12-03 13:42:09 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2013-09-09 18:51:46 +00:00
|
|
|
if output != test.expected {
|
2016-05-02 17:05:21 +00:00
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
2013-09-09 18:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-01 14:31:37 +00:00
|
|
|
}
|
2013-11-18 23:35:23 +00:00
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_BAZ", "99.4")
|
|
|
|
for _, test := range float64FlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := Float64Flag{Name: test.name, EnvVar: "APP_BAZ"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_BAZ]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_BAZ%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%s does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var genericFlagTests = []struct {
|
|
|
|
name string
|
2015-07-20 19:18:25 +00:00
|
|
|
value Generic
|
2014-07-11 22:13:10 +00:00
|
|
|
expected string
|
|
|
|
}{
|
2016-05-02 23:42:08 +00:00
|
|
|
{"toads", &Parser{"abc", "def"}, "--toads value\ttest flag (default: abc,def)"},
|
|
|
|
{"t", &Parser{"abc", "def"}, "-t value\ttest flag (default: abc,def)"},
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenericFlagHelpOutput(t *testing.T) {
|
|
|
|
for _, test := range genericFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := GenericFlag{Name: test.name, Value: test.value, Usage: "test flag"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
|
|
|
if output != test.expected {
|
|
|
|
t.Errorf("%q does not match %q", output, test.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_ZAP", "3")
|
|
|
|
for _, test := range genericFlagTests {
|
2015-07-20 19:18:25 +00:00
|
|
|
flag := GenericFlag{Name: test.name, EnvVar: "APP_ZAP"}
|
2014-07-11 22:13:10 +00:00
|
|
|
output := flag.String()
|
|
|
|
|
2016-01-22 14:08:27 +00:00
|
|
|
expectedSuffix := " [$APP_ZAP]"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedSuffix = " [%APP_ZAP%]"
|
|
|
|
}
|
|
|
|
if !strings.HasSuffix(output, expectedSuffix) {
|
2016-04-25 22:10:10 +00:00
|
|
|
t.Errorf("%s does not end with"+expectedSuffix, output)
|
2014-07-11 22:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func TestParseMultiString(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "serve, s"},
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2013-11-18 23:35:23 +00:00
|
|
|
if ctx.String("serve") != "10" {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.String("s") != "10" {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run", "-s", "10"})
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:39:38 +00:00
|
|
|
func TestParseDestinationString(t *testing.T) {
|
|
|
|
var dest string
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Destination: &dest,
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2015-11-14 21:39:38 +00:00
|
|
|
if dest != "10" {
|
|
|
|
t.Errorf("expected destination String 10")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-11-14 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--dest", "10"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiStringFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_COUNT", "20")
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "count, c", EnvVar: "APP_COUNT"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.String("count") != "20" {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.String("c") != "20" {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiStringFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_COUNT", "20")
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringFlag{Name: "count, c", EnvVar: "COMPAT_COUNT,APP_COUNT"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if ctx.String("count") != "20" {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.String("c") != "20" {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:26:57 +00:00
|
|
|
func TestParseMultiStringSlice(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringSliceFlag{Name: "serve, s", Value: &StringSlice{}},
|
2014-03-19 02:26:57 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-03-19 02:26:57 +00:00
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-03-19 02:26:57 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run", "-s", "10", "-s", "20"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiStringSliceFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "APP_INTERVALS"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiStringSliceFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func TestParseMultiInt(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntFlag{Name: "serve, s"},
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2013-11-18 23:35:23 +00:00
|
|
|
if ctx.Int("serve") != 10 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Int("s") != 10 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
|
|
|
}
|
2013-11-20 08:05:18 +00:00
|
|
|
a.Run([]string{"run", "-s", "10"})
|
2013-11-18 23:35:23 +00:00
|
|
|
}
|
2013-11-18 23:37:59 +00:00
|
|
|
|
2015-11-14 21:39:38 +00:00
|
|
|
func TestParseDestinationInt(t *testing.T) {
|
|
|
|
var dest int
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Destination: &dest,
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2015-11-14 21:39:38 +00:00
|
|
|
if dest != 10 {
|
|
|
|
t.Errorf("expected destination Int 10")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-11-14 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--dest", "10"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiIntFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "10")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntFlag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.Int("timeout") != 10 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Int("t") != 10 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiIntFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "10")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if ctx.Int("timeout") != 10 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Int("t") != 10 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiIntSlice(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntSliceFlag{Name: "serve, s", Value: &IntSlice{}},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run", "-s", "10", "-s", "20"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseMultiIntSliceFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "APP_INTERVALS"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiIntSliceFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_INTERVALS", "20,30,40")
|
|
|
|
|
2015-07-20 19:18:25 +00:00
|
|
|
(&App{
|
|
|
|
Flags: []Flag{
|
|
|
|
IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}).Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiFloat64(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{Name: "serve, s"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.Float64("serve") != 10.2 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Float64("s") != 10.2 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "-s", "10.2"})
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:39:38 +00:00
|
|
|
func TestParseDestinationFloat64(t *testing.T) {
|
|
|
|
var dest float64
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{
|
|
|
|
Name: "dest",
|
|
|
|
Destination: &dest,
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2015-11-14 21:39:38 +00:00
|
|
|
if dest != 10.2 {
|
|
|
|
t.Errorf("expected destination Float64 10.2")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-11-14 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--dest", "10.2"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiFloat64FromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.Float64("timeout") != 15.5 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Float64("t") != 15.5 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiFloat64FromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
Float64Flag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if ctx.Float64("timeout") != 15.5 {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Float64("t") != 15.5 {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:35:23 +00:00
|
|
|
func TestParseMultiBool(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "serve, s"},
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2013-11-18 23:35:23 +00:00
|
|
|
if ctx.Bool("serve") != true {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.Bool("s") != true {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2013-11-18 23:35:23 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--serve"})
|
|
|
|
}
|
2014-04-15 14:16:47 +00:00
|
|
|
|
2015-11-14 21:39:38 +00:00
|
|
|
func TestParseDestinationBool(t *testing.T) {
|
|
|
|
var dest bool
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Destination: &dest,
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2015-11-14 21:39:38 +00:00
|
|
|
if dest != true {
|
|
|
|
t.Errorf("expected destination Bool true")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-11-14 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--dest"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiBoolFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_DEBUG", "1")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.Bool("debug") != true {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if ctx.Bool("d") != true {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiBoolFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_DEBUG", "1")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if ctx.Bool("debug") != true {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if ctx.Bool("d") != true {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiBoolT(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolTFlag{Name: "serve, s"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.BoolT("serve") != true {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if ctx.BoolT("s") != true {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--serve"})
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:39:38 +00:00
|
|
|
func TestParseDestinationBoolT(t *testing.T) {
|
|
|
|
var dest bool
|
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolTFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Destination: &dest,
|
|
|
|
},
|
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2015-11-14 21:39:38 +00:00
|
|
|
if dest != true {
|
|
|
|
t.Errorf("expected destination BoolT true")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2015-11-14 21:39:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "--dest"})
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:13:10 +00:00
|
|
|
func TestParseMultiBoolTFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_DEBUG", "0")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if ctx.BoolT("debug") != false {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if ctx.BoolT("d") != false {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-09-23 03:24:08 +00:00
|
|
|
func TestParseMultiBoolTFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_DEBUG", "0")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
BoolTFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if ctx.BoolT("debug") != false {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if ctx.BoolT("d") != false {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
|
|
|
|
2014-04-15 14:16:47 +00:00
|
|
|
type Parser [2]string
|
|
|
|
|
|
|
|
func (p *Parser) Set(value string) error {
|
|
|
|
parts := strings.Split(value, ",")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid format")
|
|
|
|
}
|
|
|
|
|
|
|
|
(*p)[0] = parts[0]
|
|
|
|
(*p)[1] = parts[1]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Parser) String() string {
|
|
|
|
return fmt.Sprintf("%s,%s", p[0], p[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseGeneric(t *testing.T) {
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
GenericFlag{Name: "serve, s", Value: &Parser{}},
|
2014-04-15 14:16:47 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-04-15 14:16:47 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) {
|
|
|
|
t.Errorf("main name not set")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) {
|
|
|
|
t.Errorf("short name not set")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-04-15 14:16:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run", "-s", "10,20"})
|
|
|
|
}
|
2014-07-11 22:13:10 +00:00
|
|
|
|
|
|
|
func TestParseGenericFromEnv(t *testing.T) {
|
2014-09-23 03:24:08 +00:00
|
|
|
os.Clearenv()
|
2014-07-11 22:13:10 +00:00
|
|
|
os.Setenv("APP_SERVE", "20,30")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"},
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-07-11 22:13:10 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) {
|
|
|
|
t.Errorf("main name not set from env")
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) {
|
|
|
|
t.Errorf("short name not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-07-11 22:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|
2014-09-23 03:24:08 +00:00
|
|
|
|
|
|
|
func TestParseGenericFromEnvCascade(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("APP_FOO", "99,2000")
|
2015-07-20 19:18:25 +00:00
|
|
|
a := App{
|
|
|
|
Flags: []Flag{
|
|
|
|
GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"},
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
2016-04-27 13:12:34 +00:00
|
|
|
Action: func(ctx *Context) error {
|
2014-09-23 03:24:08 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) {
|
|
|
|
t.Errorf("value not set from env")
|
|
|
|
}
|
2016-04-27 13:12:34 +00:00
|
|
|
return nil
|
2014-09-23 03:24:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
a.Run([]string{"run"})
|
|
|
|
}
|