File: clause.go

package info (click to toggle)
golang-gopkg-alecthomas-kingpin.v3 3.0~git20180227.b8d601d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 420 kB
  • sloc: makefile: 6
file content (331 lines) | stat: -rw-r--r-- 8,572 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package kingpin

import (
	"net"

	"github.com/alecthomas/units"
)

// A Clause represents a flag or an argument passed by the user.
type Clause struct {
	actionMixin
	completionsMixin

	name          string
	shorthand     rune
	help          string
	placeholder   string
	hidden        bool
	defaultValues []string
	value         Value
	required      bool
	envar         string
	noEnvar       bool
}

func NewClause(name, help string) *Clause {
	return &Clause{
		name: name,
		help: help,
	}
}

func (c *Clause) consumesRemainder() bool {
	if r, ok := c.value.(cumulativeValue); ok {
		return r.IsCumulative()
	}
	return false
}

func (c *Clause) init() error {
	if c.required && len(c.defaultValues) > 0 {
		return TError("required flag '--{{.Arg0}}' with default value that will never be used", V{"Arg0": c.name})
	}
	if c.value == nil {
		return TError("no type defined for --{{.Arg0}} (eg. .String())", V{"Arg0": c.name})
	}
	if v, ok := c.value.(cumulativeValue); (!ok || !v.IsCumulative()) && len(c.defaultValues) > 1 {
		return TError("invalid default for '--{{.Arg0}}', expecting single value", V{"Arg0": c.name})
	}
	return nil
}

func (c *Clause) Help(help string) *Clause {
	c.help = help
	return c
}

// UsageAction adds a PreAction() that will display the given UsageContext.
func (c *Clause) UsageAction(context *UsageContext) *Clause {
	c.PreAction(func(e *ParseElement, c *ParseContext) error {
		c.Application.UsageForContextWithTemplate(context, c)
		c.Application.terminate(0)
		return nil
	})
	return c
}

func (c *Clause) UsageActionTemplate(template string) *Clause {
	return c.UsageAction(&UsageContext{Template: template})
}

// Action adds a callback action to be executed after the command line is parsed and any
// non-terminating builtin actions have completed (eg. help, completion, etc.).
func (c *Clause) Action(action Action) *Clause {
	c.actions = append(c.actions, action)
	return c
}

// PreAction adds a callback action to be executed after flag values are parsed but before
// any other processing, such as help, completion, etc.
func (c *Clause) PreAction(action Action) *Clause {
	c.preActions = append(c.preActions, action)
	return c
}

// HintAction registers a HintAction (function) for the flag to provide completions
func (c *Clause) HintAction(action HintAction) *Clause {
	c.addHintAction(action)
	return c
}

// Envar overrides the default value(s) for a flag from an environment variable,
// if it is set. Several default values can be provided by using new lines to
// separate them.
func (c *Clause) Envar(name string) *Clause {
	c.envar = name
	c.noEnvar = false
	return c
}

// NoEnvar forces environment variable defaults to be disabled for this flag.
// Most useful in conjunction with PrefixedEnvarResolver.
func (c *Clause) NoEnvar() *Clause {
	c.envar = ""
	c.noEnvar = true
	return c
}

func (c *Clause) resolveCompletions() []string {
	var hints []string

	options := c.builtinHintActions
	if len(c.hintActions) > 0 {
		// User specified their own hintActions. Use those instead.
		options = c.hintActions
	}

	for _, hintAction := range options {
		hints = append(hints, hintAction()...)
	}
	return hints
}

// HintOptions registers any number of options for the flag to provide completions
func (c *Clause) HintOptions(options ...string) *Clause {
	c.addHintAction(func() []string {
		return options
	})
	return c
}

// Default values for this flag. They *must* be parseable by the value of the flag.
func (c *Clause) Default(values ...string) *Clause {
	c.defaultValues = values
	return c
}

// PlaceHolder sets the place-holder string used for flag values in the help. The
// default behaviour is to use the value provided by Default() if provided,
// then fall back on the capitalized flag name.
func (c *Clause) PlaceHolder(placeholder string) *Clause {
	c.placeholder = placeholder
	return c
}

// Hidden hides a flag from usage but still allows it to be used.
func (c *Clause) Hidden() *Clause {
	c.hidden = true
	return c
}

// Required makes the flag required. You can not provide a Default() value to a Required() flag.
func (c *Clause) Required() *Clause {
	c.required = true
	return c
}

// Short sets the short flag name.
func (c *Clause) Short(name rune) *Clause {
	c.shorthand = name
	return c
}

func (c *Clause) needsValue(context *ParseContext) bool {
	return c.required && !c.canResolve(context)
}

func (c *Clause) canResolve(context *ParseContext) bool {
	for _, resolver := range context.resolvers {
		rvalues, err := resolver.Resolve(c.name, context)
		if err != nil {
			return false
		}
		if rvalues != nil {
			return true
		}
	}
	return false
}

func (c *Clause) reset() {
	if c, ok := c.value.(cumulativeValue); ok {
		c.Reset()
	}
}

func (c *Clause) setDefault(context *ParseContext) error {
	var values []string
	for _, resolver := range context.resolvers {
		rvalues, err := resolver.Resolve(c.name, context)
		if err != nil {
			return err
		}
		if rvalues != nil {
			values = rvalues
		}
	}

	if values != nil {
		c.reset()
		for _, value := range values {
			if err := c.value.Set(value); err != nil {
				return err
			}
		}
		return nil
	}
	return nil
}

func (c *Clause) SetValue(value Value) {
	c.value = value
}

// StringMap provides key=value parsing into a map.
func (c *Clause) StringMap(options ...AccumulatorOption) (target *map[string]string) {
	target = &(map[string]string{})
	c.StringMapVar(target, options...)
	return
}

// StringMap provides key=value parsing into a map.
func (c *Clause) StringMapVar(target *map[string]string, options ...AccumulatorOption) {
	c.SetValue(newStringMapValue(target, options...))
}

// Bytes parses numeric byte units. eg. 1.5KB
func (c *Clause) Bytes() (target *units.Base2Bytes) {
	target = new(units.Base2Bytes)
	c.BytesVar(target)
	return
}

// ExistingFile sets the parser to one that requires and returns an existing file.
func (c *Clause) ExistingFile() (target *string) {
	target = new(string)
	c.ExistingFileVar(target)
	return
}

// ExistingDir sets the parser to one that requires and returns an existing directory.
func (c *Clause) ExistingDir() (target *string) {
	target = new(string)
	c.ExistingDirVar(target)
	return
}

// ExistingFileOrDir sets the parser to one that requires and returns an existing file OR directory.
func (c *Clause) ExistingFileOrDir() (target *string) {
	target = new(string)
	c.ExistingFileOrDirVar(target)
	return
}

// Float sets the parser to a float64 parser.
func (c *Clause) Float() (target *float64) {
	return c.Float64()
}

// Float sets the parser to a float64 parser.
func (c *Clause) FloatVar(target *float64) {
	c.Float64Var(target)
}

// BytesVar parses numeric byte units. eg. 1.5KB
func (c *Clause) BytesVar(target *units.Base2Bytes) {
	c.SetValue(newBytesValue(target))
}

// ExistingFile sets the parser to one that requires and returns an existing file.
func (c *Clause) ExistingFileVar(target *string) {
	c.SetValue(newExistingFileValue(target))
}

// ExistingDir sets the parser to one that requires and returns an existing directory.
func (c *Clause) ExistingDirVar(target *string) {
	c.SetValue(newExistingDirValue(target))
}

// ExistingDir sets the parser to one that requires and returns an existing directory.
func (c *Clause) ExistingFileOrDirVar(target *string) {
	c.SetValue(newExistingFileOrDirValue(target))
}

// Enum allows a value from a set of options.
func (c *Clause) Enum(options ...string) (target *string) {
	target = new(string)
	c.EnumVar(target, options...)
	return
}

// EnumVar allows a value from a set of options.
func (c *Clause) EnumVar(target *string, options ...string) {
	c.addHintActionBuiltin(func() []string { return options })
	c.SetValue(newEnumFlag(target, options...))
}

// Enums allows a set of values from a set of options.
func (c *Clause) Enums(options ...string) (target *[]string) {
	target = new([]string)
	c.EnumsVar(target, options...)
	return
}

// EnumsVar allows a value from a set of options.
func (c *Clause) EnumsVar(target *[]string, options ...string) {
	c.SetValue(newEnumsFlag(target, options...))
}

// Counter increments a number each time it is encountered.
func (c *Clause) Counter() (target *int) {
	target = new(int)
	c.CounterVar(target)
	return
}

func (c *Clause) CounterVar(target *int) {
	c.SetValue(newCounterValue(target))
}

// IP provides a validated parsed *net.IP value.
func (c *Clause) IP() (target *net.IP) {
	target = new(net.IP)
	c.IPVar(target)
	return
}

// IPVar provides a validated parsed *net.IP value.
func (c *Clause) IPVar(target *net.IP) {
	c.SetValue(newIPValue(target))
}