forked from jawher/mow.cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargs.go
170 lines (139 loc) · 5.46 KB
/
args.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cli
import (
"fmt"
"reflect"
)
// BoolArg describes a boolean argument
type BoolArg struct {
BoolParam
// The argument name as will be shown in help messages
Name string
// The argument description as will be shown in help messages
Desc string
// A space separated list of environment variables names to be used to initialize this argument
EnvVar string
// The argument's inital value
Value bool
// A boolean to display or not the current value of the argument in the help message
HideValue bool
}
// StringArg describes a string argument
type StringArg struct {
StringParam
// The argument name as will be shown in help messages
Name string
// The argument description as will be shown in help messages
Desc string
// A space separated list of environment variables names to be used to initialize this argument
EnvVar string
// The argument's inital value
Value string
// A boolean to display or not the current value of the argument in the help message
HideValue bool
}
// IntArg describes an int argument
type IntArg struct {
IntParam
// The argument name as will be shown in help messages
Name string
// The argument description as will be shown in help messages
Desc string
// A space separated list of environment variables names to be used to initialize this argument
EnvVar string
// The argument's inital value
Value int
// A boolean to display or not the current value of the argument in the help message
HideValue bool
}
// StringsArg describes a string slice argument
type StringsArg struct {
StringsParam
// The argument name as will be shown in help messages
Name string
// The argument description as will be shown in help messages
Desc string
// A space separated list of environment variables names to be used to initialize this argument.
// The env variable should contain a comma separated list of values
EnvVar string
// The argument's inital value
Value []string
// A boolean to display or not the current value of the argument in the help message
HideValue bool
}
// IntsArg describes an int slice argument
type IntsArg struct {
IntsParam
// The argument name as will be shown in help messages
Name string
// The argument description as will be shown in help messages
Desc string
// A space separated list of environment variables names to be used to initialize this argument.
// The env variable should contain a comma separated list of values
EnvVar string
// The argument's inital value
Value []int
// A boolean to display or not the current value of the argument in the help message
HideValue bool
}
/*
BoolArg defines a boolean argument on the command c named `name`, with an initial value of `value` and a description of `desc` which will be used in help messages.
The result should be stored in a variable (a pointer to a bool) which will be populated when the app is run and the call arguments get parsed
*/
func (c *Cmd) BoolArg(name string, value bool, desc string) *bool {
return c.mkArg(arg{name: name, desc: desc}, value).(*bool)
}
/*
StringArg defines a string argument on the command c named `name`, with an initial value of `value` and a description of `desc` which will be used in help messages.
The result should be stored in a variable (a pointer to a string) which will be populated when the app is run and the call arguments get parsed
*/
func (c *Cmd) StringArg(name string, value string, desc string) *string {
return c.mkArg(arg{name: name, desc: desc}, value).(*string)
}
/*
IntArg defines an int argument on the command c named `name`, with an initial value of `value` and a description of `desc` which will be used in help messages.
The result should be stored in a variable (a pointer to an int) which will be populated when the app is run and the call arguments get parsed
*/
func (c *Cmd) IntArg(name string, value int, desc string) *int {
return c.mkArg(arg{name: name, desc: desc}, value).(*int)
}
/*
StringsArg defines a string slice argument on the command c named `name`, with an initial value of `value` and a description of `desc` which will be used in help messages.
The result should be stored in a variable (a pointer to a string slice) which will be populated when the app is run and the call arguments get parsed
*/
func (c *Cmd) StringsArg(name string, value []string, desc string) *[]string {
return c.mkArg(arg{name: name, desc: desc}, value).(*[]string)
}
/*
IntsArg defines an int slice argument on the command c named `name`, with an initial value of `value` and a description of `desc` which will be used in help messages.
The result should be stored in a variable (a pointer to an int slice) which will be populated when the app is run and the call arguments get parsed
*/
func (c *Cmd) IntsArg(name string, value []int, desc string) *[]int {
return c.mkArg(arg{name: name, desc: desc}, value).(*[]int)
}
type arg struct {
name string
desc string
envVar string
helpFormatter func(interface{}) string
value reflect.Value
hideValue bool
}
func (a *arg) String() string {
return fmt.Sprintf("ARG(%s)", a.name)
}
func (a *arg) get() interface{} {
return a.value.Elem().Interface()
}
func (a *arg) set(s string) error {
return vset(a.value, s)
}
func (c *Cmd) mkArg(arg arg, defaultvalue interface{}) interface{} {
value := reflect.ValueOf(defaultvalue)
res := reflect.New(value.Type())
arg.helpFormatter = formatterFor(value.Type())
vinit(res, arg.envVar, defaultvalue)
arg.value = res
c.args = append(c.args, &arg)
c.argsIdx[arg.name] = &arg
return res.Interface()
}