Skip to content

Commit 97ed291

Browse files
committed
Add more args
1 parent 547f669 commit 97ed291

File tree

4 files changed

+197
-16
lines changed

4 files changed

+197
-16
lines changed

args.go

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,65 @@ var AnyArguments = []Argument{
8484
},
8585
}
8686

87+
type ArgumentBase[T any, C any, VC ValueCreator[T, C]] struct {
88+
Name string `json:"name"` // the name of this argument
89+
Value T `json:"value"` // the default value of this argument
90+
Destination *T `json:"-"` // the destination point for this argument
91+
UsageText string `json:"usageText"` // the usage text to show
92+
Config C `json:"config"` // config for this argument similar to Flag Config
93+
94+
value *T
95+
}
96+
97+
func (a *ArgumentBase[T, C, VC]) HasName(s string) bool {
98+
return s == a.Name
99+
}
100+
101+
func (a *ArgumentBase[T, C, VC]) Usage() string {
102+
if a.UsageText != "" {
103+
return a.UsageText
104+
}
105+
106+
usageFormat := "%[1]s"
107+
return fmt.Sprintf(usageFormat, a.Name)
108+
}
109+
110+
func (a *ArgumentBase[T, C, VC]) Parse(s []string) ([]string, error) {
111+
tracef("calling arg%[1] parse with args %[2]", a.Name, s)
112+
113+
var vc VC
114+
var t T
115+
value := vc.Create(a.Value, &t, a.Config)
116+
a.value = &t
117+
118+
tracef("attempting arg%[1] parse", &a.Name)
119+
if len(s) > 0 {
120+
if err := value.Set(s[0]); err != nil {
121+
return s, err
122+
}
123+
*a.value = value.Get().(T)
124+
tracef("set arg%[1] one value", a.Name, *a.value)
125+
}
126+
127+
if a.Destination != nil {
128+
tracef("setting destination")
129+
*a.Destination = *a.value
130+
}
131+
132+
if len(s) > 0 {
133+
return s[1:], nil
134+
}
135+
return s, nil
136+
}
137+
138+
func (a *ArgumentBase[T, C, VC]) Get() any {
139+
if a.value != nil {
140+
return *a.value
141+
}
142+
return a.Value
143+
}
144+
145+
// ArgumentsBase is a base type for slice arguments
87146
type ArgumentsBase[T any, C any, VC ValueCreator[T, C]] struct {
88147
Name string `json:"name"` // the name of this argument
89148
Value T `json:"value"` // the default value of this argument
@@ -167,10 +226,16 @@ func (a *ArgumentsBase[T, C, VC]) Get() any {
167226
}
168227

169228
type (
229+
FloatArg = ArgumentBase[float64, NoConfig, floatValue]
230+
IntArg = ArgumentBase[int64, IntegerConfig, intValue]
231+
StringArg = ArgumentBase[string, StringConfig, stringValue]
232+
StringMapArgs = ArgumentBase[map[string]string, StringConfig, StringMap]
233+
TimestampArg = ArgumentBase[time.Time, TimestampConfig, timestampValue]
234+
UintArg = ArgumentBase[uint64, IntegerConfig, uintValue]
235+
170236
FloatArgs = ArgumentsBase[float64, NoConfig, floatValue]
171237
IntArgs = ArgumentsBase[int64, IntegerConfig, intValue]
172238
StringArgs = ArgumentsBase[string, StringConfig, stringValue]
173-
StringMapArgs = ArgumentsBase[map[string]string, StringConfig, StringMap]
174239
TimestampArgs = ArgumentsBase[time.Time, TimestampConfig, timestampValue]
175240
UintArgs = ArgumentsBase[uint64, IntegerConfig, uintValue]
176241
)
@@ -187,6 +252,14 @@ func (c *Command) getArgValue(name string) any {
187252
return nil
188253
}
189254

255+
func (c *Command) StringArg(name string) string {
256+
val := c.getArgValue(name)
257+
if a, ok := val.(string); ok {
258+
return a
259+
}
260+
return ""
261+
}
262+
190263
func (c *Command) StringArgs(name string) []string {
191264
val := c.getArgValue(name)
192265
if a, ok := val.([]string); ok {
@@ -195,15 +268,30 @@ func (c *Command) StringArgs(name string) []string {
195268
return nil
196269
}
197270

271+
func (c *Command) FloatArg(name string) float64 {
272+
val := c.getArgValue(name)
273+
if a, ok := val.(float64); ok {
274+
return a
275+
}
276+
return 0
277+
}
278+
198279
func (c *Command) FloatArgs(name string) []float64 {
199280
val := c.getArgValue(name)
200-
tracef("command %s found args %s %v", c.Name, name, val)
201281
if a, ok := val.([]float64); ok {
202282
return a
203283
}
204284
return nil
205285
}
206286

287+
func (c *Command) IntArg(name string) int64 {
288+
val := c.getArgValue(name)
289+
if a, ok := val.(int64); ok {
290+
return a
291+
}
292+
return 0
293+
}
294+
207295
func (c *Command) IntArgs(name string) []int64 {
208296
val := c.getArgValue(name)
209297
if a, ok := val.([]int64); ok {
@@ -212,6 +300,14 @@ func (c *Command) IntArgs(name string) []int64 {
212300
return nil
213301
}
214302

303+
func (c *Command) UintArg(name string) uint64 {
304+
val := c.getArgValue(name)
305+
if a, ok := val.(uint64); ok {
306+
return a
307+
}
308+
return 0
309+
}
310+
215311
func (c *Command) UintArgs(name string) []uint64 {
216312
val := c.getArgValue(name)
217313
if a, ok := val.([]uint64); ok {
@@ -220,6 +316,14 @@ func (c *Command) UintArgs(name string) []uint64 {
220316
return nil
221317
}
222318

319+
func (c *Command) TimestampArg(name string) time.Time {
320+
val := c.getArgValue(name)
321+
if a, ok := val.(time.Time); ok {
322+
return a
323+
}
324+
return time.Time{}
325+
}
326+
223327
func (c *Command) TimestampArgs(name string) []time.Time {
224328
val := c.getArgValue(name)
225329
if a, ok := val.([]time.Time); ok {

args_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -307,32 +307,31 @@ func TestSingleOptionalArg(t *testing.T) {
307307
name string
308308
args []string
309309
argValue string
310-
exp []string
310+
exp string
311311
}{
312312
{
313313
name: "no args",
314314
args: []string{"foo"},
315-
exp: []string{},
315+
exp: "",
316+
},
317+
{
318+
name: "no arg with def value",
319+
args: []string{"foo"},
320+
argValue: "bar",
321+
exp: "bar",
316322
},
317-
/*{
318-
name: "no arg with def value",
319-
args: []string{"foo"},
320-
exp: []string{"bar"},
321-
},*/
322323
{
323324
name: "one arg",
324325
args: []string{"foo", "zbar"},
325-
exp: []string{"zbar"},
326+
exp: "zbar",
326327
},
327328
}
328329

329330
for _, test := range tests {
330331
t.Run(test.name, func(t *testing.T) {
331332
cmd := buildMinimalTestCommand()
332-
var s1 []string
333-
arg := &StringArgs{
334-
Min: 0,
335-
Max: 1,
333+
var s1 string
334+
arg := &StringArg{
336335
Value: test.argValue,
337336
Destination: &s1,
338337
}

godoc-current.txt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,24 @@ type Argument interface {
261261
}
262262
Argument captures a positional argument that can be parsed
263263

264+
type ArgumentBase[T any, C any, VC ValueCreator[T, C]] struct {
265+
Name string `json:"name"` // the name of this argument
266+
Value T `json:"value"` // the default value of this argument
267+
Destination *T `json:"-"` // the destination point for this argument
268+
UsageText string `json:"usageText"` // the usage text to show
269+
Config C `json:"config"` // config for this argument similar to Flag Config
270+
271+
// Has unexported fields.
272+
}
273+
274+
func (a *ArgumentBase[T, C, VC]) Get() any
275+
276+
func (a *ArgumentBase[T, C, VC]) HasName(s string) bool
277+
278+
func (a *ArgumentBase[T, C, VC]) Parse(s []string) ([]string, error)
279+
280+
func (a *ArgumentBase[T, C, VC]) Usage() string
281+
264282
type ArgumentsBase[T any, C any, VC ValueCreator[T, C]] struct {
265283
Name string `json:"name"` // the name of this argument
266284
Value T `json:"value"` // the default value of this argument
@@ -272,6 +290,7 @@ type ArgumentsBase[T any, C any, VC ValueCreator[T, C]] struct {
272290

273291
// Has unexported fields.
274292
}
293+
ArgumentsBase is a base type for slice arguments
275294

276295
func (a *ArgumentsBase[T, C, VC]) Get() any
277296

@@ -509,6 +528,8 @@ func (cmd *Command) FlagNames() []string
509528
func (cmd *Command) Float(name string) float64
510529
Float looks up the value of a local FloatFlag, returns 0 if not found
511530

531+
func (c *Command) FloatArg(name string) float64
532+
512533
func (c *Command) FloatArgs(name string) []float64
513534

514535
func (cmd *Command) FloatSlice(name string) []float64
@@ -528,6 +549,8 @@ func (cmd *Command) HasName(name string) bool
528549
func (cmd *Command) Int(name string) int
529550
Int looks up the value of a local Int64Flag, returns 0 if not found
530551

552+
func (c *Command) IntArg(name string) int64
553+
531554
func (c *Command) IntArgs(name string) []int64
532555

533556
func (cmd *Command) IntSlice(name string) []int64
@@ -566,6 +589,8 @@ func (cmd *Command) Set(name, value string) error
566589

567590
func (cmd *Command) String(name string) string
568591

592+
func (c *Command) StringArg(name string) string
593+
569594
func (c *Command) StringArgs(name string) []string
570595

571596
func (cmd *Command) StringMap(name string) map[string]string
@@ -579,6 +604,8 @@ func (cmd *Command) StringSlice(name string) []string
579604
func (cmd *Command) Timestamp(name string) time.Time
580605
Timestamp gets the timestamp from a flag name
581606

607+
func (c *Command) TimestampArg(name string) time.Time
608+
582609
func (c *Command) TimestampArgs(name string) []time.Time
583610

584611
func (cmd *Command) ToFishCompletion() (string, error)
@@ -588,6 +615,8 @@ func (cmd *Command) ToFishCompletion() (string, error)
588615
func (cmd *Command) Uint(name string) uint
589616
Uint looks up the value of a local Uint64Flag, returns 0 if not found
590617

618+
func (c *Command) UintArg(name string) uint64
619+
591620
func (c *Command) UintArgs(name string) []uint64
592621

593622
func (cmd *Command) UintSlice(name string) []uint64
@@ -906,6 +935,8 @@ func (f FlagsByName) Less(i, j int) bool
906935

907936
func (f FlagsByName) Swap(i, j int)
908937

938+
type FloatArg = ArgumentBase[float64, NoConfig, floatValue]
939+
909940
type FloatArgs = ArgumentsBase[float64, NoConfig, floatValue]
910941

911942
type FloatFlag = FlagBase[float64, NoConfig, floatValue]
@@ -916,6 +947,8 @@ type FloatSliceFlag = FlagBase[[]float64, NoConfig, FloatSlice]
916947

917948
type GenericFlag = FlagBase[Value, NoConfig, genericValue]
918949

950+
type IntArg = ArgumentBase[int64, IntegerConfig, intValue]
951+
919952
type IntArgs = ArgumentsBase[int64, IntegerConfig, intValue]
920953

921954
type Int16SliceFlag = FlagBase[[]int16, IntegerConfig, Int16Slice]
@@ -1081,6 +1114,8 @@ func (i SliceBase[T, C, VC]) ToString(t []T) string
10811114
func (i *SliceBase[T, C, VC]) Value() []T
10821115
Value returns the slice of values set by this flag
10831116

1117+
type StringArg = ArgumentBase[string, StringConfig, stringValue]
1118+
10841119
type StringArgs = ArgumentsBase[string, StringConfig, stringValue]
10851120

10861121
type StringConfig struct {
@@ -1093,7 +1128,7 @@ type StringFlag = FlagBase[string, StringConfig, stringValue]
10931128

10941129
type StringMap = MapBase[string, StringConfig, stringValue]
10951130

1096-
type StringMapArgs = ArgumentsBase[map[string]string, StringConfig, StringMap]
1131+
type StringMapArgs = ArgumentBase[map[string]string, StringConfig, StringMap]
10971132

10981133
type StringMapFlag = FlagBase[map[string]string, StringConfig, StringMap]
10991134

@@ -1105,6 +1140,8 @@ type SuggestCommandFunc func(commands []*Command, provided string) string
11051140

11061141
type SuggestFlagFunc func(flags []Flag, provided string, hideHelp bool) string
11071142

1143+
type TimestampArg = ArgumentBase[time.Time, TimestampConfig, timestampValue]
1144+
11081145
type TimestampArgs = ArgumentsBase[time.Time, TimestampConfig, timestampValue]
11091146

11101147
type TimestampConfig struct {
@@ -1120,6 +1157,8 @@ type TimestampConfig struct {
11201157

11211158
type TimestampFlag = FlagBase[time.Time, TimestampConfig, timestampValue]
11221159

1160+
type UintArg = ArgumentBase[uint64, IntegerConfig, uintValue]
1161+
11231162
type UintArgs = ArgumentsBase[uint64, IntegerConfig, uintValue]
11241163

11251164
type UintFlag = FlagBase[uint, IntegerConfig, uintValue[uint]]

0 commit comments

Comments
 (0)