-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.go
More file actions
151 lines (139 loc) · 2.71 KB
/
flag.go
File metadata and controls
151 lines (139 loc) · 2.71 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
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
package go_flags
import (
"flag"
"fmt"
"strings"
)
type (
// Flag is a custom flag type that restricts its value to a predefined set of allowed strings.
Flag struct {
defaultValue *string
value *string
allowed []string
name string
usage string
}
)
// NewFlag creates a new Flag with a default value and a list of allowed values.
//
// Parameters:
//
// defaultValue - the default value for the flag.
// allowed - slice of allowed string values.
// name - the name of the flag.
// usage - the usage description for the flag.
//
// Returns:
//
// A pointer to the created Flag.
func NewFlag(
defaultValue *string,
allowed []string,
name string,
usage string,
) *Flag {
return &Flag{
defaultValue: defaultValue,
allowed: allowed,
name: name,
usage: usage,
}
}
// Default returns the default value of the flag.
//
// Returns:
//
// The default value of the flag as a string.
func (f *Flag) Default() string {
if f == nil || f.defaultValue == nil {
return ""
}
return *f.defaultValue
}
// String returns the string representation of the flag's current value.
//
// Returns:
//
// The current value of the flag as a string.
func (f *Flag) String() string {
if f == nil {
return ""
}
if f.value != nil {
return *f.value
}
if f.defaultValue == nil {
return ""
}
return *f.defaultValue
}
// Value returns the current value of the flag.
//
// Returns:
//
// The current value of the flag as a string.
func (f *Flag) Value() string {
if f == nil {
return ""
}
return f.String()
}
// Allowed returns the list of allowed values for the flag.
//
// Returns:
//
// The allowed values.
func (f *Flag) Allowed() []string {
if f == nil {
return nil
}
return f.allowed
}
// Set validates and sets the flag's value.
//
// Parameters:
//
// value - the value to set.
//
// Returns:
//
// An error if the value is not allowed, otherwise nil.
func (f *Flag) Set(value string) error {
if f == nil {
return ErrNilFlag
}
// Check if the allowed values are defined
if f.allowed == nil {
f.value = &value
return nil
}
// Validate the value against the allowed values
for _, v := range f.allowed {
if value == v {
f.value = &value
return nil
}
}
return fmt.Errorf(
ErrInvalidValue, value,
strings.Join(f.allowed, ", "),
)
}
// SetFlag registers the custom flag with the standard flag package.
func (f *Flag) SetFlag() {
SetFlag(f, f.name, f.usage)
}
// SetFlag registers the custom flag with the standard flag package.
//
// Parameters:
//
// value - the flag.Value to register.
// name - the flag name.
// usage - the usage description for the flag.
func SetFlag(value flag.Value, name string, usage string) {
flag.Var(
value,
name,
usage,
)
}