/
cli_flags_generated.gotpl
87 lines (76 loc) · 2.53 KB
/
cli_flags_generated.gotpl
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
// Code generated by fg; DO NOT EDIT.
package {{ .PackageName }}
import (
"flag"
"fmt"
"strconv"
"time"
)
{{ range $i, $flag := .Flags }}
// {{ $flag.Name }}Flag is a flag with type {{ $flag.Type }}{{ $flag.Doctail }}
type {{ $flag.Name }}Flag struct {
Name string
Usage string
EnvVar string
FilePath string
Required bool
Hidden bool
TakesFile bool
{{- if eq $flag.Value true }}
Value {{ $flag.Type }}
{{- end }}
{{- if eq $flag.Destination true }}
Destination *{{ $flag.Type }}
{{- end }}
}
// String returns a readable representation of this value
// (for usage defaults)
func (f {{ $flag.Name }}Flag) String() string {
return FlagStringer(f)
}
// GetName returns the name of the flag
func (f {{ $flag.Name }}Flag) GetName() string {
return f.Name
}
// IsRequired returns whether or not the flag is required
func (f {{ $flag.Name }}Flag) IsRequired() bool {
return f.Required
}
// TakesValue returns true of the flag takes a value, otherwise false
func (f {{ $flag.Name }}Flag) TakesValue() bool {
return {{ $flag.Value }}
}
// GetUsage returns the usage string for the flag
func (f {{ $flag.Name }}Flag) GetUsage() string {
return f.Usage
}
// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
func (f {{ $flag.Name }}Flag) GetValue() string {
{{ $flag.ValueString }}
}
// {{ $flag.Name }} looks up the value of a local {{ $flag.Name }}Flag, returns
// {{ $flag.ContextDefault }} if not found
func (c *Context) {{ $flag.Name }}(name string) {{ if ne .ContextType "" }}{{ $flag.ContextType }}{{ else }}{{ $flag.Type }}{{- end }} {
return lookup{{ $flag.Name }}(name, c.flagSet)
}
// Global{{ $flag.Name }} looks up the value of a global {{ $flag.Name }}Flag, returns
// {{ $flag.ContextDefault }} if not found
func (c *Context) Global{{ $flag.Name }}(name string) {{ if ne .ContextType "" }}{{ $flag.ContextType }}{{ else }}{{ $flag.Type }}{{- end }} {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookup{{ $flag.Name }}(name, fs)
}
return {{ $flag.ContextDefault }}
}
func lookup{{ $flag.Name }}(name string, set *flag.FlagSet) {{ if ne .ContextType "" }}{{ $flag.ContextType }}{{ else }}{{ $flag.Type }}{{- end }} {
f := set.Lookup(name)
if f != nil {
{{ if ne .Parser "" }}parsed, err := {{ $flag.Parser }}{{ else }}parsed, err := f.Value, error(nil){{ end }}
if err != nil {
return {{ $flag.ContextDefault }}
}
{{ if ne .ParserCast "" }}return {{ $flag.ParserCast }}{{ else }}return parsed{{ end }}
}
return {{ $flag.ContextDefault }}
}
{{ end }}