forked from spf13/pflag
-
Notifications
You must be signed in to change notification settings - Fork 2
/
string.go
112 lines (92 loc) · 4.08 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pflag
// -- string Value
type stringValue string
func newStringValue(val string, p *string) *stringValue {
*p = val
return (*stringValue)(p)
}
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
func (s *stringValue) Type() string {
return "string"
}
func (s *stringValue) String() string { return string(*s) }
func stringConv(sval string) (interface{}, error) {
return sval, nil
}
// GetString return the string value of a flag with the given name
func (f *FlagSet) GetString(name string) (string, error) {
val, err := f.getFlagType(name, "string", stringConv)
if err != nil {
return "", err
}
return val.(string), nil
}
// MustGetString is like GetString, but panics on error.
func (f *FlagSet) MustGetString(name string) string {
val, err := f.GetString(name)
if err != nil {
panic(err)
}
return val
}
// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
f.StringVarP(p, name, "", value, usage)
}
// StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) {
f.VarP(newStringValue(value, p), name, shorthand, usage)
}
// StringVarS is like StringVar, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) StringVarS(p *string, name, shorthand string, value string, usage string) {
f.VarS(newStringValue(value, p), name, shorthand, usage)
}
// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func StringVar(p *string, name string, value string, usage string) {
CommandLine.StringVar(p, name, value, usage)
}
// StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
func StringVarP(p *string, name, shorthand string, value string, usage string) {
CommandLine.StringVarP(p, name, shorthand, value, usage)
}
// StringVarS is like StringVar, but accepts a shorthand letter that can be used after a single dash, alone.
func StringVarS(p *string, name, shorthand string, value string, usage string) {
CommandLine.StringVarS(p, name, shorthand, value, usage)
}
// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func (f *FlagSet) String(name string, value string, usage string) *string {
return f.StringP(name, "", value, usage)
}
// StringP is like String, but accepts a shorthand letter that can be used after a single dash.
func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string {
p := new(string)
f.StringVarP(p, name, shorthand, value, usage)
return p
}
// StringS is like String, but accepts a shorthand letter that can be used after a single dash, alone.
func (f *FlagSet) StringS(name, shorthand string, value string, usage string) *string {
p := new(string)
f.StringVarS(p, name, shorthand, value, usage)
return p
}
// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func String(name string, value string, usage string) *string {
return CommandLine.String(name, value, usage)
}
// StringP is like String, but accepts a shorthand letter that can be used after a single dash.
func StringP(name, shorthand string, value string, usage string) *string {
return CommandLine.StringP(name, shorthand, value, usage)
}
// StringS is like String, but accepts a shorthand letter that can be used after a single dash, alone.
func StringS(name, shorthand string, value string, usage string) *string {
return CommandLine.StringS(name, shorthand, value, usage)
}