Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ type boolFlag interface {
}

// -- bool Value
type boolValue bool
type BoolValue bool

func newBoolValue(val bool, p *bool) *boolValue {
func NewBoolValue(val bool, p *bool) *BoolValue {
*p = val
return (*boolValue)(p)
return (*BoolValue)(p)
}

func (b *boolValue) Set(s string) error {
func (b *BoolValue) Set(s string) error {
v, err := strconv.ParseBool(s)
*b = boolValue(v)
*b = BoolValue(v)
return err
}

func (b *boolValue) Type() string {
func (b *BoolValue) Type() string {
return "bool"
}

func (b *boolValue) String() string { return strconv.FormatBool(bool(*b)) }
func (b *BoolValue) String() string { return strconv.FormatBool(bool(*b)) }

func (b *boolValue) IsBoolFlag() bool { return true }
func (b *BoolValue) IsBoolFlag() bool { return true }

func boolConv(sval string) (interface{}, error) {
return strconv.ParseBool(sval)
Expand All @@ -52,7 +52,7 @@ func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string) {

// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
flag := f.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag := f.VarPF(NewBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
}

Expand All @@ -64,7 +64,7 @@ func BoolVar(p *bool, name string, value bool, usage string) {

// BoolVarP is like BoolVar, but accepts a shorthand letter that can be used after a single dash.
func BoolVarP(p *bool, name, shorthand string, value bool, usage string) {
flag := CommandLine.VarPF(newBoolValue(value, p), name, shorthand, usage)
flag := CommandLine.VarPF(NewBoolValue(value, p), name, shorthand, usage)
flag.NoOptDefVal = "true"
}

Expand Down
30 changes: 15 additions & 15 deletions bool_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
)

// -- boolSlice Value
type boolSliceValue struct {
type BoolSliceValue struct {
value *[]bool
changed bool
}

func newBoolSliceValue(val []bool, p *[]bool) *boolSliceValue {
bsv := new(boolSliceValue)
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 {
func (s *BoolSliceValue) Set(val string) error {

// remove all quote characters
rmQuote := strings.NewReplacer(`"`, "", `'`, "", "`", "")
Expand Down Expand Up @@ -54,12 +54,12 @@ func (s *boolSliceValue) Set(val string) error {
}

// Type returns a string that uniquely represents this flag's type.
func (s *boolSliceValue) Type() string {
func (s *BoolSliceValue) Type() string {
return "boolSlice"
}

// String defines a "native" format for this boolean slice flag value.
func (s *boolSliceValue) String() string {
func (s *BoolSliceValue) String() string {

boolStrSlice := make([]string, len(*s.value))
for i, b := range *s.value {
Expand All @@ -71,15 +71,15 @@ func (s *boolSliceValue) String() string {
return "[" + out + "]"
}

func (s *boolSliceValue) fromString(val string) (bool, error) {
func (s *BoolSliceValue) fromString(val string) (bool, error) {
return strconv.ParseBool(val)
}

func (s *boolSliceValue) toString(val bool) string {
func (s *BoolSliceValue) toString(val bool) string {
return strconv.FormatBool(val)
}

func (s *boolSliceValue) Append(val string) error {
func (s *BoolSliceValue) Append(val string) error {
i, err := s.fromString(val)
if err != nil {
return err
Expand All @@ -88,7 +88,7 @@ func (s *boolSliceValue) Append(val string) error {
return nil
}

func (s *boolSliceValue) Replace(val []string) error {
func (s *BoolSliceValue) Replace(val []string) error {
out := make([]bool, len(val))
for i, d := range val {
var err error
Expand All @@ -101,7 +101,7 @@ func (s *boolSliceValue) Replace(val []string) error {
return nil
}

func (s *boolSliceValue) GetSlice() []string {
func (s *BoolSliceValue) GetSlice() []string {
out := make([]string, len(*s.value))
for i, d := range *s.value {
out[i] = s.toString(d)
Expand Down Expand Up @@ -139,23 +139,23 @@ func (f *FlagSet) GetBoolSlice(name string) ([]bool, error) {
// 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)
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)
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)
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)
CommandLine.VarP(NewBoolSliceValue(value, p), name, shorthand, usage)
}

// BoolSlice defines a []bool flag with specified name, default value, and usage string.
Expand Down
40 changes: 20 additions & 20 deletions bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

// BytesHex adapts []byte for use as a flag. Value of flag is HEX encoded
type bytesHexValue []byte
type BytesHexValue []byte

// String implements pflag.Value.String.
func (bytesHex bytesHexValue) String() string {
func (bytesHex BytesHexValue) String() string {
return fmt.Sprintf("%X", []byte(bytesHex))
}

// Set implements pflag.Value.Set.
func (bytesHex *bytesHexValue) Set(value string) error {
func (bytesHex *BytesHexValue) Set(value string) error {
bin, err := hex.DecodeString(strings.TrimSpace(value))

if err != nil {
Expand All @@ -29,13 +29,13 @@ func (bytesHex *bytesHexValue) Set(value string) error {
}

// Type implements pflag.Value.Type.
func (*bytesHexValue) Type() string {
func (*BytesHexValue) Type() string {
return "bytesHex"
}

func newBytesHexValue(val []byte, p *[]byte) *bytesHexValue {
func NewBytesHexValue(val []byte, p *[]byte) *BytesHexValue {
*p = val
return (*bytesHexValue)(p)
return (*BytesHexValue)(p)
}

func bytesHexConv(sval string) (interface{}, error) {
Expand Down Expand Up @@ -63,23 +63,23 @@ func (f *FlagSet) GetBytesHex(name string) ([]byte, error) {
// BytesHexVar defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func (f *FlagSet) BytesHexVar(p *[]byte, name string, value []byte, usage string) {
f.VarP(newBytesHexValue(value, p), name, "", usage)
f.VarP(NewBytesHexValue(value, p), name, "", usage)
}

// BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
f.VarP(newBytesHexValue(value, p), name, shorthand, usage)
f.VarP(NewBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHexVar defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func BytesHexVar(p *[]byte, name string, value []byte, usage string) {
CommandLine.VarP(newBytesHexValue(value, p), name, "", usage)
CommandLine.VarP(NewBytesHexValue(value, p), name, "", usage)
}

// BytesHexVarP is like BytesHexVar, but accepts a shorthand letter that can be used after a single dash.
func BytesHexVarP(p *[]byte, name, shorthand string, value []byte, usage string) {
CommandLine.VarP(newBytesHexValue(value, p), name, shorthand, usage)
CommandLine.VarP(NewBytesHexValue(value, p), name, shorthand, usage)
}

// BytesHex defines an []byte flag with specified name, default value, and usage string.
Expand Down Expand Up @@ -109,15 +109,15 @@ func BytesHexP(name, shorthand string, value []byte, usage string) *[]byte {
}

// BytesBase64 adapts []byte for use as a flag. Value of flag is Base64 encoded
type bytesBase64Value []byte
type BytesBase64Value []byte

// String implements pflag.Value.String.
func (bytesBase64 bytesBase64Value) String() string {
func (bytesBase64 BytesBase64Value) String() string {
return base64.StdEncoding.EncodeToString([]byte(bytesBase64))
}

// Set implements pflag.Value.Set.
func (bytesBase64 *bytesBase64Value) Set(value string) error {
func (bytesBase64 *BytesBase64Value) Set(value string) error {
bin, err := base64.StdEncoding.DecodeString(strings.TrimSpace(value))

if err != nil {
Expand All @@ -130,13 +130,13 @@ func (bytesBase64 *bytesBase64Value) Set(value string) error {
}

// Type implements pflag.Value.Type.
func (*bytesBase64Value) Type() string {
func (*BytesBase64Value) Type() string {
return "bytesBase64"
}

func newBytesBase64Value(val []byte, p *[]byte) *bytesBase64Value {
func NewBytesBase64Value(val []byte, p *[]byte) *BytesBase64Value {
*p = val
return (*bytesBase64Value)(p)
return (*BytesBase64Value)(p)
}

func bytesBase64ValueConv(sval string) (interface{}, error) {
Expand All @@ -163,23 +163,23 @@ func (f *FlagSet) GetBytesBase64(name string) ([]byte, error) {
// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func (f *FlagSet) BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
f.VarP(newBytesBase64Value(value, p), name, "", usage)
f.VarP(NewBytesBase64Value(value, p), name, "", usage)
}

// BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
f.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
f.VarP(NewBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64Var defines an []byte flag with specified name, default value, and usage string.
// The argument p points to an []byte variable in which to store the value of the flag.
func BytesBase64Var(p *[]byte, name string, value []byte, usage string) {
CommandLine.VarP(newBytesBase64Value(value, p), name, "", usage)
CommandLine.VarP(NewBytesBase64Value(value, p), name, "", usage)
}

// BytesBase64VarP is like BytesBase64Var, but accepts a shorthand letter that can be used after a single dash.
func BytesBase64VarP(p *[]byte, name, shorthand string, value []byte, usage string) {
CommandLine.VarP(newBytesBase64Value(value, p), name, shorthand, usage)
CommandLine.VarP(NewBytesBase64Value(value, p), name, shorthand, usage)
}

// BytesBase64 defines an []byte flag with specified name, default value, and usage string.
Expand Down
18 changes: 9 additions & 9 deletions count.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ package pflag
import "strconv"

// -- count Value
type countValue int
type CountValue int

func newCountValue(val int, p *int) *countValue {
func NewCountValue(val int, p *int) *CountValue {
*p = val
return (*countValue)(p)
return (*CountValue)(p)
}

func (i *countValue) Set(s string) error {
func (i *CountValue) Set(s string) error {
// "+1" means that no specific value was passed, so increment
if s == "+1" {
*i = countValue(*i + 1)
*i = CountValue(*i + 1)
return nil
}
v, err := strconv.ParseInt(s, 0, 0)
*i = countValue(v)
*i = CountValue(v)
return err
}

func (i *countValue) Type() string {
func (i *CountValue) Type() string {
return "count"
}

func (i *countValue) String() string { return strconv.Itoa(int(*i)) }
func (i *CountValue) String() string { return strconv.Itoa(int(*i)) }

func countConv(sval string) (interface{}, error) {
i, err := strconv.Atoi(sval)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (f *FlagSet) CountVar(p *int, name string, usage string) {

// CountVarP is like CountVar only take a shorthand for the flag name.
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
flag := f.VarPF(NewCountValue(0, p), name, shorthand, usage)
flag.NoOptDefVal = "+1"
}

Expand Down
22 changes: 11 additions & 11 deletions duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
)

// -- time.Duration Value
type durationValue time.Duration
type DurationValue time.Duration

func newDurationValue(val time.Duration, p *time.Duration) *durationValue {
func NewDurationValue(val time.Duration, p *time.Duration) *DurationValue {
*p = val
return (*durationValue)(p)
return (*DurationValue)(p)
}

func (d *durationValue) Set(s string) error {
func (d *DurationValue) Set(s string) error {
v, err := time.ParseDuration(s)
*d = durationValue(v)
*d = DurationValue(v)
return err
}

func (d *durationValue) Type() string {
func (d *DurationValue) Type() string {
return "duration"
}

func (d *durationValue) String() string { return (*time.Duration)(d).String() }
func (d *DurationValue) String() string { return (*time.Duration)(d).String() }

func durationConv(sval string) (interface{}, error) {
return time.ParseDuration(sval)
Expand All @@ -40,23 +40,23 @@ func (f *FlagSet) GetDuration(name string) (time.Duration, error) {
// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag.
func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
f.VarP(newDurationValue(value, p), name, "", usage)
f.VarP(NewDurationValue(value, p), name, "", usage)
}

// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
f.VarP(newDurationValue(value, p), name, shorthand, usage)
f.VarP(NewDurationValue(value, p), name, shorthand, usage)
}

// DurationVar defines a time.Duration flag with specified name, default value, and usage string.
// The argument p points to a time.Duration variable in which to store the value of the flag.
func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
CommandLine.VarP(newDurationValue(value, p), name, "", usage)
CommandLine.VarP(NewDurationValue(value, p), name, "", usage)
}

// DurationVarP is like DurationVar, but accepts a shorthand letter that can be used after a single dash.
func DurationVarP(p *time.Duration, name, shorthand string, value time.Duration, usage string) {
CommandLine.VarP(newDurationValue(value, p), name, shorthand, usage)
CommandLine.VarP(NewDurationValue(value, p), name, shorthand, usage)
}

// Duration defines a time.Duration flag with specified name, default value, and usage string.
Expand Down
Loading
Loading