Documentation ¶
Overview ¶
Package extensions provides extensions to the github.com/spf13/pflag package.
Example ¶
Example of joint usage with gflag generic type.
package main import ( "fmt" "log" "github.com/fredbi/gflag" "github.com/fredbi/gflag/extensions" "github.com/spf13/pflag" ) func main() { fl := gflag.NewFlagValue(extensions.NewByteSizeValue(nil, 1024), 1024) name := "size" short := name[:1] fs := pflag.NewFlagSet("Example", pflag.ContinueOnError) pf := fs.VarPF(fl, name, short, "byte size") if err := fs.Parse([]string{"--size", "10GB"}); err != nil { log.Fatalln("parsing error:", err) } // retrieve parsed values flag := fs.Lookup(name) fmt.Println(flag.Name) fmt.Println(flag.Value) flag = fs.ShorthandLookup(short) fmt.Println(flag.Name) fmt.Println(flag.Value) // using underlying value fmt.Println(pf.Value) // using type's GetValue() fmt.Printf("%[1]T: %[1]v\n", fl.Value.GetValue()) // return uint64 // using generic GetValue[ByteSizeValue]() fmt.Printf("%[1]T: %[1]v\n", uint64(fl.GetValue())) // GetValue() returns ByteSize fmt.Printf("%[1]T: %[1]v\n", fl.GetValue().GetValue()) // returns uint64 }
Output: size 10GB size 10GB 10GB uint64: 10000000000 uint64: 10000000000 uint64: 10000000000
Index ¶
- func ByteSize(name string, defaultValue uint64, usage string) *uint64
- func ByteSizeP(name, shorthand string, defaultValue uint64, usage string) *uint64
- func ByteSizeVar(p *uint64, name string, defaultValue uint64, usage string)
- func ByteSizeVarP(p *uint64, name, shorthand string, defaultValue uint64, usage string)
- type ByteSizeValue
- func (b ByteSizeValue) GetValue() uint64
- func (b ByteSizeValue) MarshalFlag() (string, error)
- func (b ByteSizeValue) MarshalText() ([]byte, error)
- func (b *ByteSizeValue) Set(value string) error
- func (b ByteSizeValue) String() string
- func (b *ByteSizeValue) Type() string
- func (b *ByteSizeValue) UnmarshalFlag(value string) error
- func (b ByteSizeValue) UnmarshalText(text []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ByteSize ¶
ByteSize defines an uint64 flag with the specified name, default value, and usage string.
The return value is the address of an uint64 variable that stores the value of the flag.
Example ¶
Joint usage with pflag, for a simple bool flag
package main import ( "fmt" "log" "github.com/fredbi/gflag/extensions" "github.com/spf13/pflag" ) func main() { flagVal := extensions.NewByteSizeValue(nil, 1024) name := "size" short := name[:1] fs := pflag.NewFlagSet("Example", pflag.ContinueOnError) fs.VarP(flagVal, name, short, "byte size") if err := fs.Parse([]string{"--size", "20GB"}); err != nil { log.Fatalln("parsing error:", err) } // retrieve parsed values flag := fs.Lookup(name) fmt.Println(flag.Name) fmt.Println(flag.Value) flag = fs.ShorthandLookup(short) fmt.Println(flag.Name) fmt.Println(flag.Value) // using underlying value fmt.Println(flagVal) // using GetValue() fmt.Println(flagVal.GetValue()) }
Output: size 20GB size 20GB 20GB 20000000000
func ByteSizeP ¶
ByteSizeP is like ByteSize, but accepts a shorthand letter that can be used after a single dash.
func ByteSizeVar ¶
ByteSizeVar defines a byte-size flag wih name, default value and usage string. The flag is set on the default pflag.CommandLine flagset.
The flag value is stored at address p.
Types ¶
type ByteSizeValue ¶
type ByteSizeValue uint64
ByteSizeValue is used to pass human-readable byte sizes as flags.
func NewByteSizeValue ¶
func NewByteSizeValue(p *uint64, defaultValue uint64) *ByteSizeValue
NewByteSizeValue builds a ByteSizeValue on some existing uint64 pointer.
If the given pointer is nil, a new uint64 is allocated.
func (ByteSizeValue) GetValue ¶ added in v0.1.0
func (b ByteSizeValue) GetValue() uint64
GetValue returns the size in bytes as a uint64.
func (ByteSizeValue) MarshalFlag ¶
func (b ByteSizeValue) MarshalFlag() (string, error)
MarshalFlag implements go-flags Marshaler interface
func (ByteSizeValue) MarshalText ¶
func (b ByteSizeValue) MarshalText() ([]byte, error)
MarshalText implements encoding.TextMarshaler interface
func (*ByteSizeValue) Set ¶
func (b *ByteSizeValue) Set(value string) error
Set the value of this bytesize (pflag value interfaces)
func (ByteSizeValue) String ¶
func (b ByteSizeValue) String() string
String method for a bytesize (pflag value and stringer interface)
func (*ByteSizeValue) Type ¶
func (b *ByteSizeValue) Type() string
Type returns the type of the pflag value (pflag value interface)
func (*ByteSizeValue) UnmarshalFlag ¶
func (b *ByteSizeValue) UnmarshalFlag(value string) error
UnmarshalFlag implements go-flags Unmarshaler interface
func (ByteSizeValue) UnmarshalText ¶
func (b ByteSizeValue) UnmarshalText(text []byte) error
UnmarshalText implements encoding.TextUnmarshaler interface