summaryrefslogtreecommitdiff
path: root/compflag/gen/flags.go.gotmpl
blob: 07dddc71b5b215d9949343fbea2969aa96b5cc5a (plain)
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
package compflag

{{ range . }}

// {{ .Name }} if a flag function for a flag of type {{ .Type }}.
func {{ .Name }}(name string, value {{ .Type }}, usage string, options ...predict.Option) *{{ .Type }} {
	return CommandLine.{{ .Name }}(name, value, usage, options...)
}

// {{ .Name }}Var if a flag function for a flag of already exiting variable of type {{ .Type }}.
func {{ .Name }}Var(v *{{ .Type }}, name string, value {{ .Type }}, usage string, options ...predict.Option) {
	CommandLine.{{ .Name }}Var(v, name, value, usage, options...)
}

{{ end }}

{{ range . }}

// {{ .Name }} if a flag function for a flag of type {{ .Type }}.
func (fs *FlagSet) {{ .Name }}(name string, value {{ .Type }}, usage string, options ...predict.Option) *{{ .Type }} {
	p := new({{ .Type }})
	fs.{{.Name}}Var(p, name, value, usage, options...)
	return p
}

// {{ .Name }}Var if a flag function for a flag of already exiting variable of type {{ .Type }}.
func (fs *FlagSet) {{ .Name }}Var(p *{{ .Type }}, name string, value {{ .Type }}, usage string, options ...predict.Option) {
	(*flag.FlagSet)(fs).Var({{ .NewInternalTypeFuncName }}(value, p, predict.Options(options...)), name, usage)
}

{{ end }}

{{ range . }}

// ============================================================================================== //

type {{ .InternalTypeName }} struct {
	v *{{ .Type }}
	predict.Config
}

func {{ .NewInternalTypeFuncName }}(val {{ .Type }}, p *{{ .Type }}, c predict.Config) *{{ .InternalTypeName }} {
	*p = val
	return &{{ .InternalTypeName}} {v: p, Config: c}
}

func (v *{{ .InternalTypeName }}) Set(val string) error {
	var err error
	*v.v, err = parse{{ .Name }}(val)
	if err != nil {
		return fmt.Errorf("bad value for {{ .Name }} flag")
	}
	return v.Check(val)
}

func (v *{{ .InternalTypeName }}) Get() interface{} {
	return *v.v
}

func (v *{{ .InternalTypeName }}) String() string {
	if v == nil || v.v == nil {
		return ""
	}
	return format{{ .Name }}(*v.v)
}

{{ if .IsBool }}
func (v *{{ .InternalTypeName }}) IsBoolFlag() bool { return true }
{{ end}}

func (v *{{ .InternalTypeName }}) Predict(prefix string) []string {
	if v.Predictor != nil {
		return v.Predictor.Predict(prefix)
	}
	return {{ if .CustomPredict }}predict{{ .Name }}(*v.v, prefix){{ else }}[]string{""}{{ end }}
}

{{ end }}