-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflag.go
More file actions
78 lines (65 loc) · 1.43 KB
/
Copy pathflag.go
File metadata and controls
78 lines (65 loc) · 1.43 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
package argparse
import "fmt"
type FlagType int
const (
_ FlagType = iota
TypeBool
TypeString
TypeEnum
TypeList
TypeInt
TypeFloat
)
type Flag struct {
Type FlagType // The type of the flag
Value any // The value of the flag
Index int // The index of the flag in the input arguments
Set bool // Whether the flag was set, otherwise this flag is the default value
}
type Enum struct {
key string
value any
}
// newDefaultFlag returns a [Flag] to be used as a default value
func newDefaultFlag(t FlagType, v any) *Flag {
return &Flag{Type: t, Value: v, Index: -1}
}
func (f *Flag) Bool() bool {
if f.Type != TypeBool {
panic("flag is not of type bool")
}
return f.Value.(bool)
}
func (f *Flag) Int() int {
if f.Type != TypeInt {
panic("flag is not of type int")
}
return f.Value.(int)
}
func (f *Flag) Float() float64 {
if f.Type != TypeFloat {
panic("flag is not of type float")
}
return f.Value.(float64)
}
func (f *Flag) String() string {
if f.Type != TypeString {
return fmt.Sprintf("%v", f.Value)
}
return f.Value.(string)
}
func (f *Flag) Enum() *Enum {
if f.Type != TypeEnum {
panic("flag is not of type enum")
}
return f.Value.(*Enum)
}
// EnumValue is f.Enum().Value()
func (f *Flag) EnumValue() any {
if f.Type != TypeEnum {
panic("flag is not of type enum")
}
return f.Value.(*Enum).value
}
func (e *Enum) Key() string { return e.key }
func (e *Enum) Value() any { return e.value }