forked from spf13/pflag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BoolSlice and UintSlice flag types. (spf13#111)
* Add Gogland/IntelliJ/Jetbrains config directory to .gitignore. * Added uint slice flag type. Added godoc for uint slice type. Added unit tests for new uint slice type. * Added new boolSliceValue type to handle []bool arguments. Added unit tests for new boolSliceValue type. Added godoc documentation. * Added new ipSliceValue type to handle []net.IP arguments. Added unit tests for new ipSliceValue type. Added godoc documentation. * Fix golint warnings. * boolSliceValue: - Use CSV parser for boolean flag arguments, and handle corner cases with extraneous quote characters. - Add unit tests for to parse flags with extraneous quote characters. - Add godoc documentation to undocumented methods. * boolSliceValue: - Refactored boolSlice name to boolStrSlice for clarity. - Fix allocation of out variable to len=0 (not len=cap) - Remove extraneous err declaration in range loop. - Actually append bool to []bool. - Simplify unit test function name. ipSliceValue: - Use csv parser for net.IP flag arguments, and handle corner cases with extraneous quote characters. - Add unit tests to parse flags with extraneous quote characters. - Add godoc documentation to undocumented methods. * boolSliceValue: ipSliceValue: - Use csv utility functions instead of duplicating code for reading and writing CSV flag string values.
- Loading branch information
Showing
7 changed files
with
1,021 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package pflag | ||
|
||
import ( | ||
"io" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// -- boolSlice Value | ||
type boolSliceValue struct { | ||
value *[]bool | ||
changed bool | ||
} | ||
|
||
func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue { | ||
bsv := new(boolSliceValue) | ||
bsv.value = p | ||
*bsv.value = val | ||
return bsv | ||
} | ||
|
||
// Set converts, and assigns, the comma-separated boolean argument string representation as the []bool value of this flag. | ||
// If Set is called on a flag that already has a []bool assigned, the newly converted values will be appended. | ||
func (s *boolSliceValue) Set(val string) error { | ||
|
||
// remove all quote characters | ||
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "") | ||
|
||
// read flag arguments with CSV parser | ||
boolStrSlice, err := readAsCSV(rmQuote.Replace(val)) | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
|
||
// parse boolean values into slice | ||
out := make([]bool, 0, len(boolStrSlice)) | ||
for _, boolStr := range boolStrSlice { | ||
b, err := strconv.ParseBool(strings.TrimSpace(boolStr)) | ||
if err != nil { | ||
return err | ||
} | ||
out = append(out, b) | ||
} | ||
|
||
if !s.changed { | ||
*s.value = out | ||
} else { | ||
*s.value = append(*s.value, out...) | ||
} | ||
|
||
s.changed = true | ||
|
||
return nil | ||
} | ||
|
||
// Type returns a string that uniquely represents this flag's type. | ||
func (s *boolSliceValue) Type() string { | ||
return "boolSlice" | ||
} | ||
|
||
// String defines a "native" format for this boolean slice flag value. | ||
func (s *boolSliceValue) String() string { | ||
|
||
boolStrSlice := make([]string, len(*s.value)) | ||
for i, b := range *s.value { | ||
boolStrSlice[i] = strconv.FormatBool(b) | ||
} | ||
|
||
out, _ := writeAsCSV(boolStrSlice) | ||
|
||
return "[" + out + "]" | ||
} | ||
|
||
func boolSliceConv(val string) (interface{}, error) { | ||
val = strings.Trim(val, "[]") | ||
// Empty string would cause a slice with one (empty) entry | ||
if len(val) == 0 { | ||
return []bool{}, nil | ||
} | ||
ss := strings.Split(val, ",") | ||
out := make([]bool, len(ss)) | ||
for i, t := range ss { | ||
var err error | ||
out[i], err = strconv.ParseBool(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return out, nil | ||
} | ||
|
||
// GetBoolSlice returns the []bool value of a flag with the given name. | ||
func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) { | ||
val, err := f.getFlagType(name, "boolSlice", boolSliceConv) | ||
if err != nil { | ||
return []bool{}, err | ||
} | ||
return val.([]bool), nil | ||
} | ||
|
||
// BoolSliceVar defines a boolSlice flag with specified name, default value, and usage string. | ||
// The argument p points to a []bool variable in which to store the value of the flag. | ||
func (f *FlagSet) BoolSliceVar(p *[]bool, name string, value []bool, usage string) { | ||
f.VarP(newBoolSliceValue(value, p), name, "", usage) | ||
} | ||
|
||
// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. | ||
func (f *FlagSet) BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { | ||
f.VarP(newBoolSliceValue(value, p), name, shorthand, usage) | ||
} | ||
|
||
// BoolSliceVar defines a []bool flag with specified name, default value, and usage string. | ||
// The argument p points to a []bool variable in which to store the value of the flag. | ||
func BoolSliceVar(p *[]bool, name string, value []bool, usage string) { | ||
CommandLine.VarP(newBoolSliceValue(value, p), name, "", usage) | ||
} | ||
|
||
// BoolSliceVarP is like BoolSliceVar, but accepts a shorthand letter that can be used after a single dash. | ||
func BoolSliceVarP(p *[]bool, name, shorthand string, value []bool, usage string) { | ||
CommandLine.VarP(newBoolSliceValue(value, p), name, shorthand, usage) | ||
} | ||
|
||
// BoolSlice defines a []bool flag with specified name, default value, and usage string. | ||
// The return value is the address of a []bool variable that stores the value of the flag. | ||
func (f *FlagSet) BoolSlice(name string, value []bool, usage string) *[]bool { | ||
p := []bool{} | ||
f.BoolSliceVarP(&p, name, "", value, usage) | ||
return &p | ||
} | ||
|
||
// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. | ||
func (f *FlagSet) BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { | ||
p := []bool{} | ||
f.BoolSliceVarP(&p, name, shorthand, value, usage) | ||
return &p | ||
} | ||
|
||
// BoolSlice defines a []bool flag with specified name, default value, and usage string. | ||
// The return value is the address of a []bool variable that stores the value of the flag. | ||
func BoolSlice(name string, value []bool, usage string) *[]bool { | ||
return CommandLine.BoolSliceP(name, "", value, usage) | ||
} | ||
|
||
// BoolSliceP is like BoolSlice, but accepts a shorthand letter that can be used after a single dash. | ||
func BoolSliceP(name, shorthand string, value []bool, usage string) *[]bool { | ||
return CommandLine.BoolSliceP(name, shorthand, value, usage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package pflag | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func setUpBSFlagSet(bsp *[]bool) *FlagSet { | ||
f := NewFlagSet("test", ContinueOnError) | ||
f.BoolSliceVar(bsp, "bs", []bool{}, "Command separated list!") | ||
return f | ||
} | ||
|
||
func setUpBSFlagSetWithDefault(bsp *[]bool) *FlagSet { | ||
f := NewFlagSet("test", ContinueOnError) | ||
f.BoolSliceVar(bsp, "bs", []bool{false, true}, "Command separated list!") | ||
return f | ||
} | ||
|
||
func TestEmptyBS(t *testing.T) { | ||
var bs []bool | ||
f := setUpBSFlagSet(&bs) | ||
err := f.Parse([]string{}) | ||
if err != nil { | ||
t.Fatal("expected no error; got", err) | ||
} | ||
|
||
getBS, err := f.GetBoolSlice("bs") | ||
if err != nil { | ||
t.Fatal("got an error from GetBoolSlice():", err) | ||
} | ||
if len(getBS) != 0 { | ||
t.Fatalf("got bs %v with len=%d but expected length=0", getBS, len(getBS)) | ||
} | ||
} | ||
|
||
func TestBS(t *testing.T) { | ||
var bs []bool | ||
f := setUpBSFlagSet(&bs) | ||
|
||
vals := []string{"1", "F", "TRUE", "0"} | ||
arg := fmt.Sprintf("--bs=%s", strings.Join(vals, ",")) | ||
err := f.Parse([]string{arg}) | ||
if err != nil { | ||
t.Fatal("expected no error; got", err) | ||
} | ||
for i, v := range bs { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected is[%d] to be %s but got: %t", i, vals[i], v) | ||
} | ||
} | ||
getBS, err := f.GetBoolSlice("bs") | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
for i, v := range getBS { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected bs[%d] to be %s but got: %t from GetBoolSlice", i, vals[i], v) | ||
} | ||
} | ||
} | ||
|
||
func TestBSDefault(t *testing.T) { | ||
var bs []bool | ||
f := setUpBSFlagSetWithDefault(&bs) | ||
|
||
vals := []string{"false", "T"} | ||
|
||
err := f.Parse([]string{}) | ||
if err != nil { | ||
t.Fatal("expected no error; got", err) | ||
} | ||
for i, v := range bs { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected bs[%d] to be %t from GetBoolSlice but got: %t", i, b, v) | ||
} | ||
} | ||
|
||
getBS, err := f.GetBoolSlice("bs") | ||
if err != nil { | ||
t.Fatal("got an error from GetBoolSlice():", err) | ||
} | ||
for i, v := range getBS { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatal("got an error from GetBoolSlice():", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected bs[%d] to be %t from GetBoolSlice but got: %t", i, b, v) | ||
} | ||
} | ||
} | ||
|
||
func TestBSWithDefault(t *testing.T) { | ||
var bs []bool | ||
f := setUpBSFlagSetWithDefault(&bs) | ||
|
||
vals := []string{"FALSE", "1"} | ||
arg := fmt.Sprintf("--bs=%s", strings.Join(vals, ",")) | ||
err := f.Parse([]string{arg}) | ||
if err != nil { | ||
t.Fatal("expected no error; got", err) | ||
} | ||
for i, v := range bs { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected bs[%d] to be %t but got: %t", i, b, v) | ||
} | ||
} | ||
|
||
getBS, err := f.GetBoolSlice("bs") | ||
if err != nil { | ||
t.Fatal("got an error from GetBoolSlice():", err) | ||
} | ||
for i, v := range getBS { | ||
b, err := strconv.ParseBool(vals[i]) | ||
if err != nil { | ||
t.Fatalf("got error: %v", err) | ||
} | ||
if b != v { | ||
t.Fatalf("expected bs[%d] to be %t from GetBoolSlice but got: %t", i, b, v) | ||
} | ||
} | ||
} | ||
|
||
func TestBSCalledTwice(t *testing.T) { | ||
var bs []bool | ||
f := setUpBSFlagSet(&bs) | ||
|
||
in := []string{"T,F", "T"} | ||
expected := []bool{true, false, true} | ||
argfmt := "--bs=%s" | ||
arg1 := fmt.Sprintf(argfmt, in[0]) | ||
arg2 := fmt.Sprintf(argfmt, in[1]) | ||
err := f.Parse([]string{arg1, arg2}) | ||
if err != nil { | ||
t.Fatal("expected no error; got", err) | ||
} | ||
for i, v := range bs { | ||
if expected[i] != v { | ||
t.Fatalf("expected bs[%d] to be %t but got %t", i, expected[i], v) | ||
} | ||
} | ||
} | ||
|
||
func TestBSBadQuoting(t *testing.T) { | ||
|
||
tests := []struct { | ||
Want []bool | ||
FlagArg []string | ||
}{ | ||
{ | ||
Want: []bool{true, false, true}, | ||
FlagArg: []string{"1", "0", "true"}, | ||
}, | ||
{ | ||
Want: []bool{true, false}, | ||
FlagArg: []string{"True", "F"}, | ||
}, | ||
{ | ||
Want: []bool{true, false}, | ||
FlagArg: []string{"T", "0"}, | ||
}, | ||
{ | ||
Want: []bool{true, false}, | ||
FlagArg: []string{"1", "0"}, | ||
}, | ||
{ | ||
Want: []bool{true, false, false}, | ||
FlagArg: []string{"true,false", "false"}, | ||
}, | ||
{ | ||
Want: []bool{true, false, false, true, false, true, false}, | ||
FlagArg: []string{`"true,false,false,1,0, T"`, " false "}, | ||
}, | ||
{ | ||
Want: []bool{false, false, true, false, true, false, true}, | ||
FlagArg: []string{`"0, False, T,false , true,F"`, "true"}, | ||
}, | ||
} | ||
|
||
for i, test := range tests { | ||
|
||
var bs []bool | ||
f := setUpBSFlagSet(&bs) | ||
|
||
if err := f.Parse([]string{fmt.Sprintf("--bs=%s", strings.Join(test.FlagArg, ","))}); err != nil { | ||
t.Fatalf("flag parsing failed with error: %s\nparsing:\t%#v\nwant:\t\t%#v", | ||
err, test.FlagArg, test.Want[i]) | ||
} | ||
|
||
for j, b := range bs { | ||
if b != test.Want[j] { | ||
t.Fatalf("bad value parsed for test %d on bool %d:\nwant:\t%t\ngot:\t%t", i, j, test.Want[j], b) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.