blob: 04e0a26ff7f2653b7c569dabe8cb91296d76e288 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package pflag
2
3// -- string Value
4type stringValue string
5
6func newStringValue(val string, p *string) *stringValue {
7 *p = val
8 return (*stringValue)(p)
9}
10
11func (s *stringValue) Set(val string) error {
12 *s = stringValue(val)
13 return nil
14}
15func (s *stringValue) Type() string {
16 return "string"
17}
18
19func (s *stringValue) String() string { return string(*s) }
20
21func stringConv(sval string) (interface{}, error) {
22 return sval, nil
23}
24
25// GetString return the string value of a flag with the given name
26func (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.
36func (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.
41func (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.
47func 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.
52func 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.
58func (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.
65func (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.
73func 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.
78func StringP(name, shorthand string, value string, usage string) *string {
79 return CommandLine.StringP(name, shorthand, value, usage)
80}