-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharg.go
More file actions
132 lines (110 loc) · 3.15 KB
/
Copy patharg.go
File metadata and controls
132 lines (110 loc) · 3.15 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
package applause
// Arg represents a command-line argument (positional by default, becomes flag when short/long is set)
type Arg struct {
name string
short string
long string
help string
required bool
valueType ValueType
multiple bool
validators []Validator
index int // for positional args
value string // single value for backward compatibility
values []string // multiple values when multiple=true
found bool
defaultValue string // default value if not provided
envVar string // environment variable name to check
}
// NewArg creates a new argument builder
func NewArg(name string) *Arg {
return &Arg{
name: name,
valueType: StringType, // Default to string type
}
}
// Short sets the short flag for the argument (e.g., -c)
func (a *Arg) Short(short string) *Arg {
a.short = short
return a
}
// Long sets the long flag for the argument (e.g., --config)
func (a *Arg) Long(long string) *Arg {
a.long = long
return a
}
// Help sets the help text for the argument
func (a *Arg) Help(help string) *Arg {
a.help = help
return a
}
// Required marks the argument as required
func (a *Arg) Required(required bool) *Arg {
a.required = required
return a
}
// IsFlag returns true if this argument is a flag (has short or long set)
func (a *Arg) IsFlag() bool {
return a.short != "" || a.long != ""
}
// IsPositional returns true if this argument is positional (no short or long set)
func (a *Arg) IsPositional() bool {
return !a.IsFlag()
}
// ValueType sets the expected type for the argument value
func (a *Arg) ValueType(vt ValueType) *Arg {
a.valueType = vt
return a
}
// GetValueType returns the expected type for this argument
func (a *Arg) GetValueType() ValueType {
return a.valueType
}
// Multiple marks the argument as accepting multiple values
func (a *Arg) Multiple(multiple bool) *Arg {
a.multiple = multiple
return a
}
// IsMultiple returns true if this argument accepts multiple values
func (a *Arg) IsMultiple() bool {
return a.multiple
}
// Convenience methods for setting common types
// AsString sets the argument type to string (default)
func (a *Arg) AsString() *Arg {
return a.ValueType(StringType)
}
// AsInt sets the argument type to int
func (a *Arg) AsInt() *Arg {
return a.ValueType(IntType)
}
// AsBool sets the argument type to bool
func (a *Arg) AsBool() *Arg {
return a.ValueType(BoolType)
}
// AsFloat64 sets the argument type to float64
func (a *Arg) AsFloat64() *Arg {
return a.ValueType(Float64Type)
}
// AsDuration sets the argument type to duration
func (a *Arg) AsDuration() *Arg {
return a.ValueType(DurationType)
}
// Default sets the default value for the argument
func (a *Arg) Default(defaultValue string) *Arg {
a.defaultValue = defaultValue
return a
}
// GetDefault returns the default value for the argument
func (a *Arg) GetDefault() string {
return a.defaultValue
}
// Env sets the environment variable name to check for the argument value
func (a *Arg) Env(envVar string) *Arg {
a.envVar = envVar
return a
}
// GetEnv returns the environment variable name for the argument
func (a *Arg) GetEnv() string {
return a.envVar
}