-
Notifications
You must be signed in to change notification settings - Fork 4
/
custom.go
79 lines (66 loc) · 1.85 KB
/
custom.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
77
78
79
package libcmd
import (
"fmt"
"reflect"
"strings"
)
// CustomArg is the interface used to create customized argument types.
// As long as you can read and write to a string, you can use this.
//
// TypeName is the name to be used to represent this type on help messages
// (e. g. int, string, value, foo). This will only be used if the user does
// not supply a sencond help message.
//
// Explain is a (optional) short string describing the custom type. This
// method receives a template string(that might be empty) in Printf style
// and 'injects' the type explanation on it. It may return different results
// based on the existence or not of a template string.
//
// Note that a empty string ("") is assumed to be the zero value
// of your custom type
type CustomArg interface {
Get() string
Set(value string) error
TypeName() string
Explain(template string) string
}
var customArgType = reflect.TypeOf(new(CustomArg)).Elem()
type choiceString struct {
value *string
choices []string
}
func newChoice(target *string, choices []string) *choiceString {
return &choiceString{
value: target,
choices: choices,
}
}
func (c *choiceString) Get() string {
return *c.value
}
func (c *choiceString) Set(value string) error {
if value == "" {
*c.value = value
return nil
}
for _, s := range c.choices {
if s == value {
*c.value = value
return nil
}
}
return parserError{err: fmt.Errorf("'%s' is not a valid value (possible values: %s)", value, strings.Join(c.choices, ","))}
}
func (c *choiceString) TypeName() string {
return "value"
}
func (c *choiceString) Explain(template string) string {
choices := strings.Join(c.choices, ",")
choices = strings.Trim(choices, ",")
if strings.Contains(template, "%s") {
return fmt.Sprintf(template, choices)
} else if template != "" {
return template
}
return "Valid values: " + choices + "."
}