diff options
| author | Jonathan Reyna <[email protected]> | 2017-01-25 10:35:48 -0500 |
|---|---|---|
| committer | Eric Paris <[email protected]> | 2017-01-25 10:35:48 -0500 |
| commit | a9a634f3de0a7529baded7ad6b0c7467d5c6eca7 (patch) | |
| tree | a235f03afd02bdd631d1abbadb1d5b299a5170a6 /uint_slice.go | |
| parent | a232f6d9f87afaaa08bafaff5da685f974b83313 (diff) | |
Add BoolSlice and UintSlice flag types. (#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.
Diffstat (limited to 'uint_slice.go')
| -rw-r--r-- | uint_slice.go | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/uint_slice.go b/uint_slice.go new file mode 100644 index 0000000..edd94c6 --- /dev/null +++ b/uint_slice.go @@ -0,0 +1,126 @@ +package pflag + +import ( + "fmt" + "strconv" + "strings" +) + +// -- uintSlice Value +type uintSliceValue struct { + value *[]uint + changed bool +} + +func newUintSliceValue(val []uint, p *[]uint) *uintSliceValue { + uisv := new(uintSliceValue) + uisv.value = p + *uisv.value = val + return uisv +} + +func (s *uintSliceValue) Set(val string) error { + ss := strings.Split(val, ",") + out := make([]uint, len(ss)) + for i, d := range ss { + u, err := strconv.ParseUint(d, 10, 0) + if err != nil { + return err + } + out[i] = uint(u) + } + if !s.changed { + *s.value = out + } else { + *s.value = append(*s.value, out...) + } + s.changed = true + return nil +} + +func (s *uintSliceValue) Type() string { + return "uintSlice" +} + +func (s *uintSliceValue) String() string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = fmt.Sprintf("%d", d) + } + return "[" + strings.Join(out, ",") + "]" +} + +func uintSliceConv(val string) (interface{}, error) { + val = strings.Trim(val, "[]") + // Empty string would cause a slice with one (empty) entry + if len(val) == 0 { + return []uint{}, nil + } + ss := strings.Split(val, ",") + out := make([]uint, len(ss)) + for i, d := range ss { + u, err := strconv.ParseUint(d, 10, 0) + if err != nil { + return nil, err + } + out[i] = uint(u) + } + return out, nil +} + +// GetUintSlice returns the []uint value of a flag with the given name. +func (f *FlagSet) GetUintSlice(name string) ([]uint, error) { + val, err := f.getFlagType(name, "uintSlice", uintSliceConv) + if err != nil { + return []uint{}, err + } + return val.([]uint), nil +} + +// UintSliceVar defines a uintSlice flag with specified name, default value, and usage string. +// The argument p points to a []uint variable in which to store the value of the flag. +func (f *FlagSet) UintSliceVar(p *[]uint, name string, value []uint, usage string) { + f.VarP(newUintSliceValue(value, p), name, "", usage) +} + +// UintSliceVarP is like UintSliceVar, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { + f.VarP(newUintSliceValue(value, p), name, shorthand, usage) +} + +// UintSliceVar defines a uint[] flag with specified name, default value, and usage string. +// The argument p points to a uint[] variable in which to store the value of the flag. +func UintSliceVar(p *[]uint, name string, value []uint, usage string) { + CommandLine.VarP(newUintSliceValue(value, p), name, "", usage) +} + +// UintSliceVarP is like the UintSliceVar, but accepts a shorthand letter that can be used after a single dash. +func UintSliceVarP(p *[]uint, name, shorthand string, value []uint, usage string) { + CommandLine.VarP(newUintSliceValue(value, p), name, shorthand, usage) +} + +// UintSlice defines a []uint flag with specified name, default value, and usage string. +// The return value is the address of a []uint variable that stores the value of the flag. +func (f *FlagSet) UintSlice(name string, value []uint, usage string) *[]uint { + p := []uint{} + f.UintSliceVarP(&p, name, "", value, usage) + return &p +} + +// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. +func (f *FlagSet) UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { + p := []uint{} + f.UintSliceVarP(&p, name, shorthand, value, usage) + return &p +} + +// UintSlice defines a []uint flag with specified name, default value, and usage string. +// The return value is the address of a []uint variable that stores the value of the flag. +func UintSlice(name string, value []uint, usage string) *[]uint { + return CommandLine.UintSliceP(name, "", value, usage) +} + +// UintSliceP is like UintSlice, but accepts a shorthand letter that can be used after a single dash. +func UintSliceP(name, shorthand string, value []uint, usage string) *[]uint { + return CommandLine.UintSliceP(name, shorthand, value, usage) +} |
