string.go (2925B)
1 package pflag 2 3 // -- string Value 4 type stringValue string 5 6 func newStringValue(val string, p *string) *stringValue { 7 *p = val 8 return (*stringValue)(p) 9 } 10 11 func (s *stringValue) Set(val string) error { 12 *s = stringValue(val) 13 return nil 14 } 15 func (s *stringValue) Type() string { 16 return "string" 17 } 18 19 func (s *stringValue) String() string { return string(*s) } 20 21 func stringConv(sval string) (interface{}, error) { 22 return sval, nil 23 } 24 25 // GetString return the string value of a flag with the given name 26 func (f *FlagSet) GetString(name string) (string, error) { 27 val, err := f.getFlagType(name, "string", stringConv) 28 if err != nil { 29 return "", err 30 } 31 return val.(string), nil 32 } 33 34 // StringVar defines a string flag with specified name, default value, and usage string. 35 // The argument p points to a string variable in which to store the value of the flag. 36 func (f *FlagSet) StringVar(p *string, name string, value string, usage string) { 37 f.VarP(newStringValue(value, p), name, "", usage) 38 } 39 40 // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash. 41 func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) { 42 f.VarP(newStringValue(value, p), name, shorthand, usage) 43 } 44 45 // StringVar defines a string flag with specified name, default value, and usage string. 46 // The argument p points to a string variable in which to store the value of the flag. 47 func StringVar(p *string, name string, value string, usage string) { 48 CommandLine.VarP(newStringValue(value, p), name, "", usage) 49 } 50 51 // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash. 52 func StringVarP(p *string, name, shorthand string, value string, usage string) { 53 CommandLine.VarP(newStringValue(value, p), name, shorthand, usage) 54 } 55 56 // String defines a string flag with specified name, default value, and usage string. 57 // The return value is the address of a string variable that stores the value of the flag. 58 func (f *FlagSet) String(name string, value string, usage string) *string { 59 p := new(string) 60 f.StringVarP(p, name, "", value, usage) 61 return p 62 } 63 64 // StringP is like String, but accepts a shorthand letter that can be used after a single dash. 65 func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string { 66 p := new(string) 67 f.StringVarP(p, name, shorthand, value, usage) 68 return p 69 } 70 71 // String defines a string flag with specified name, default value, and usage string. 72 // The return value is the address of a string variable that stores the value of the flag. 73 func String(name string, value string, usage string) *string { 74 return CommandLine.StringP(name, "", value, usage) 75 } 76 77 // StringP is like String, but accepts a shorthand letter that can be used after a single dash. 78 func StringP(name, shorthand string, value string, usage string) *string { 79 return CommandLine.StringP(name, shorthand, value, usage) 80 }