func TestKeyParamFlagSetInvalid(t *testing.T) { var kp flagx.KeyParam v := flag.Value(&kp) var hexKey = cas.Invalid.String() err := v.Set(hexKey) if err == nil { t.Fatalf("expected an error from Set: %v", err) } if g, e := err.Error(), "bad key format"; g != e { t.Errorf("bad string value: %v", g) } }
func TestKeyParamFlagSetOk(t *testing.T) { var kp flagx.KeyParam v := flag.Value(&kp) const hexKey = "095920158295e252b3cb4728713a987875d210eca00d5b69e89e1d2e2e473153ec3de87ada31787b0d5080cdb0f7dcf15ea1f03cec5fef76df027bcc7d57b337" err := v.Set(hexKey) if err != nil { t.Fatalf("unexpected error from Set: %v", err) } if g, e := v.String(), hexKey; g != e { t.Errorf("bad string value: %v", g) } }
func TestKeyParamFlagSetNotHex(t *testing.T) { var kp flagx.KeyParam v := flag.Value(&kp) const hexKey = "i am not even hex!" err := v.Set(hexKey) if err == nil { t.Fatalf("expected an error from Set: %v", err) } if _, ok := err.(hex.InvalidByteError); !ok { t.Fatalf("bad error type Set: %T: %v", err, err) } if g, e := err.Error(), "encoding/hex: invalid byte: U+0069 'i'"; g != e { t.Errorf("bad string value: %v", g) } }
func TestKeyParamFlagSetTooShort(t *testing.T) { var kp flagx.KeyParam v := flag.Value(&kp) const hexKey = "ff" err := v.Set(hexKey) if err == nil { t.Fatalf("expected an error from Set: %v", err) } err2, ok := err.(*cas.BadKeySizeError) if !ok { t.Fatalf("bad error type Set: %T: %v", err, err) } if g, e := string(err2.Key), "\xFF"; g != e { t.Errorf("bad error detail: %x", g) } if g, e := err2.Error(), "Key is bad length 1: ff"; g != e { t.Errorf("bad string value: %v", g) } }
package fstestutil import ( "flag" "log" "strconv" "github.com/maybebtc/interplanetary/Godeps/_workspace/src/bazil.org/fuse" ) type flagDebug bool var debug flagDebug var _ = flag.Value(&debug) func (f *flagDebug) IsBoolFlag() bool { return true } func nop(msg interface{}) {} func (f *flagDebug) Set(s string) error { v, err := strconv.ParseBool(s) if err != nil { return err } *f = flagDebug(v) if v { fuse.Debug = logMsg } else {
package main import ( "flag" "regexp" ) type RegexpValue struct { regexp *regexp.Regexp } func (rv *RegexpValue) Set(s string) error { var err error rv.regexp, err = regexp.Compile(s) return err } func (rv *RegexpValue) String() string { if rv.regexp == nil { return "" } return rv.regexp.String() } var _ = flag.Value(&RegexpValue{})
// map[string]string {"foo": "bar", "baz": ""} package stringmapflag import ( "flag" "fmt" "sort" "strings" ) // Value is a flag.Value implementation that stores arbitrary "key[=value]" // command-line flags as a string map. type Value map[string]string // Assert that Value conforms to the "flag.Value" interface. var _ = flag.Value(new(Value)) func (v *Value) String() string { if len(*v) == 0 { return "" } keys := make([]string, 0, len(*v)) for k := range *v { keys = append(keys, k) } sort.Strings(keys) for idx, k := range keys { if value := (*v)[k]; value != "" { keys[idx] = fmt.Sprintf("%s=%s", k, value)
package flagx import ( "errors" "flag" "path/filepath" ) // AbsPath returns a flag.Value that wraps the given string and sets // it to an absolute path. type AbsPath string var _ = flag.Value(new(AbsPath)) func (a AbsPath) String() string { return string(a) } var EmptyPathError = errors.New("empty path not allowed") func (a *AbsPath) Set(value string) error { if value == "" { return EmptyPathError } path, err := filepath.Abs(value) if err != nil { return err } *a = AbsPath(path) return nil }