-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter.go
More file actions
55 lines (46 loc) · 1.91 KB
/
parameter.go
File metadata and controls
55 lines (46 loc) · 1.91 KB
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
package cli
import "strings"
//Parameter is a value struct for a parameter in the command line arguments.
type Parameter struct {
//Name is the name of the Parameter as it will appear in help and error output.
Name string
//Optional denotes whether or not the parameter's presence in command line
//arguments is optional. False for required.
Optional bool
//Many denotes whether or not this Parameter can have a variable number of
//command line arguments for input.
Many bool
}
//ParameterSetter provides the interface for a cli working with command line parameters.
type ParameterSetter interface {
//ParameterUsage returns the Parameters used and a possible usage string to
//describe parameters in more detail.
//These values are used in help and error output.
ParameterUsage() (params []*Parameter, usage string)
//SetParameters allows implementations to receive parameter arguments during
//argument parsing.
//An error should be returned if values cannot be correctly parsed as parameters
//expected by the implementation.
//Implementations should not retain references to values.
SetParameters(values []string) error
}
//FormatParameters calls format() for each Parameter in params and returns
//the result joined by " ".
func FormatParameters(params []*Parameter, format func(p *Parameter) string) string {
formats := make([]string, 0, len(params))
for _, param := range params {
formats = append(formats, format(param))
}
return strings.Join(formats, " ")
}
//FormatParameter returns a string representation of p appropriate for help and
//error output.
func FormatParameter(p *Parameter) string {
return FormatArgument(FormatParameterName(p.Name), p.Optional, p.Many)
}
//FormatParameterName returns a string representation of a Parameter name appropriate
//for help and error output.
//It returns the result of strings.ToUpper(name).
func FormatParameterName(name string) string {
return strings.ToUpper(name)
}