-
Notifications
You must be signed in to change notification settings - Fork 8
/
args.go
76 lines (60 loc) · 1.3 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
package cli
type Arg struct {
Value Value
Name string
Usage Usager
Necessary Necessary
set bool
defaultSaved bool
defaultValue string
defaultEmpty bool
}
func newArg(value Value, opts ArgOptions) Arg {
return Arg{
Value: value,
Name: opts.Name,
Usage: opts.Usage,
Necessary: opts.Necessary,
}
}
func (a *Arg) Type() string {
if t, ok := a.Value.(Typer); ok {
return t.Type()
}
return ""
}
func (a *Arg) Required() bool {
// By default args are required.
return a.Necessary != Optional
}
func (a *Arg) Set() bool {
return a.set
}
func (a *Arg) MarkSet() {
a.set = true
}
func (a *Arg) Default() (v string, empty bool) {
if !a.defaultSaved {
return "", true
}
return a.defaultValue, a.defaultEmpty
}
func (a *Arg) SaveDefault() {
a.defaultValue = a.Value.String()
if ev, ok := a.Value.(Emptier); ok {
a.defaultEmpty = ev.Empty()
} else {
a.defaultEmpty = a.defaultValue == ""
}
a.defaultSaved = true
}
func (a *Arg) String() string {
return "Arg(" + a.Type() + "," + a.Name + ")"
}
func ArgVar(register Register, value Value, name string, options ...ArgOptionApplyer) error {
var opts ArgOptions
opts.applyName(name)
opts.applyArgOptions(options)
return register.RegisterArg(newArg(value, opts))
}
//go:generate python ./generate_args.py