diff options
| author | xilabao <[email protected]> | 2017-01-13 16:50:03 +0800 |
|---|---|---|
| committer | xilabao <[email protected]> | 2017-01-16 09:06:09 +0800 |
| commit | 271ea0e85343ffc73d7871cb63cb3a615320cc47 (patch) | |
| tree | 37c1c8b11dff5dde17be2c52671468f8d14dddb0 /flag_test.go | |
| parent | 5ccb023bc27df288a957c5e994cd44fd19619465 (diff) | |
Make command line parsing available outside pflag
We basically want to allow callers to use our command line parser. There
is a user who wants to log the command line, but they don't want to log
"sensitive" flags. This allows that user to parse the command line and
get each flag. They can use information in the flag to write their own
printer.
Diffstat (limited to 'flag_test.go')
| -rw-r--r-- | flag_test.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go index b294fc7..ad050b3 100644 --- a/flag_test.go +++ b/flag_test.go @@ -333,6 +333,59 @@ func testParse(f *FlagSet, t *testing.T) { } } +func testParseAll(f *FlagSet, t *testing.T) { + if f.Parsed() { + fmt.Errorf("f.Parse() = true before Parse") + } + f.BoolP("boola", "a", false, "bool value") + f.BoolP("boolb", "b", false, "bool2 value") + f.BoolP("boolc", "c", false, "bool3 value") + f.BoolP("boold", "d", false, "bool4 value") + f.StringP("stringa", "s", "0", "string value") + f.StringP("stringz", "z", "0", "string value") + f.StringP("stringx", "x", "0", "string value") + f.StringP("stringy", "y", "0", "string value") + f.Lookup("stringx").NoOptDefVal = "1" + args := []string{ + "-ab", + "-cs=xx", + "--stringz=something", + "-d=true", + "-x", + "-y", + "ee", + } + want := []string{ + "boola", "true", + "boolb", "true", + "boolc", "true", + "stringa", "xx", + "stringz", "something", + "boold", "true", + "stringx", "1", + "stringy", "ee", + } + got := []string{} + store := func(flag *Flag, value string) error { + got = append(got, flag.Name) + if len(value) > 0 { + got = append(got, value) + } + return nil + } + if err := f.ParseAll(args, store); err != nil { + t.Errorf("expected no error, got ", err) + } + if !f.Parsed() { + t.Errorf("f.Parse() = false after Parse") + } + if !reflect.DeepEqual(got, want) { + t.Errorf("f.ParseAll() fail to restore the args") + t.Errorf("Got: %v", got) + t.Errorf("Want: %v", want) + } +} + func TestShorthand(t *testing.T) { f := NewFlagSet("shorthand", ContinueOnError) if f.Parsed() { @@ -398,6 +451,11 @@ func TestParse(t *testing.T) { testParse(GetCommandLine(), t) } +func TestParseAll(t *testing.T) { + ResetForTesting(func() { t.Error("bad parse") }) + testParseAll(GetCommandLine(), t) +} + func TestFlagSetParse(t *testing.T) { testParse(NewFlagSet("test", ContinueOnError), t) } |
