Skip to content

Commit e908328

Browse files
committed
feat!: add more integers and unsigned integers
* Reintroduced all variants of integers and unsigned integers. * Fixed the use of base by using `IntegerConfig.Base` and defaulting to base 10 when formatting. Fixes: #2050 Signed-off-by: Tobias Dahlberg <lokskada@live.se>
1 parent efab65a commit e908328

18 files changed

+2032
-287
lines changed

args.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,17 @@ func (a *ArgumentBase[T, C, VC]) Parse(s []string) ([]string, error) {
152152

153153
type (
154154
FloatArg = ArgumentBase[float64, NoConfig, floatValue]
155-
IntArg = ArgumentBase[int64, IntegerConfig, intValue]
155+
IntArg = ArgumentBase[int, IntegerConfig, intValue[int]]
156+
Int8Arg = ArgumentBase[int8, IntegerConfig, intValue[int8]]
157+
Int16Arg = ArgumentBase[int16, IntegerConfig, intValue[int16]]
158+
Int32Arg = ArgumentBase[int32, IntegerConfig, intValue[int32]]
159+
Int64Arg = ArgumentBase[int64, IntegerConfig, intValue[int64]]
156160
StringArg = ArgumentBase[string, StringConfig, stringValue]
157161
StringMapArg = ArgumentBase[map[string]string, StringConfig, StringMap]
158162
TimestampArg = ArgumentBase[time.Time, TimestampConfig, timestampValue]
159-
UintArg = ArgumentBase[uint64, IntegerConfig, uintValue]
163+
UintArg = ArgumentBase[uint, IntegerConfig, uintValue[uint]]
164+
Uint8Arg = ArgumentBase[uint8, IntegerConfig, uintValue[uint8]]
165+
Uint16Arg = ArgumentBase[uint16, IntegerConfig, uintValue[uint16]]
166+
Uint32Arg = ArgumentBase[uint32, IntegerConfig, uintValue[uint32]]
167+
Uint64Arg = ArgumentBase[uint64, IntegerConfig, uintValue[uint64]]
160168
)

args_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestArgumentsRootCommand(t *testing.T) {
1515
var fval float64
1616
var fvals []float64
1717
cmd.Arguments = []Argument{
18-
&IntArg{
18+
&Int64Arg{
1919
Name: "ia",
2020
Min: 1,
2121
Max: 1,
@@ -49,7 +49,7 @@ func TestArgumentsRootCommand(t *testing.T) {
4949
&StringArg{
5050
Name: "sa",
5151
},
52-
&UintArg{
52+
&Uint64Arg{
5353
Name: "ua",
5454
Min: 2,
5555
Max: 1, // max is less than min
@@ -68,7 +68,7 @@ func TestArgumentsSubcommand(t *testing.T) {
6868
{
6969
Name: "subcmd",
7070
Flags: []Flag{
71-
&IntFlag{
71+
&Int64Flag{
7272
Name: "foo",
7373
Value: 10,
7474
Destination: &ifval,
@@ -114,7 +114,7 @@ func TestArgumentsSubcommand(t *testing.T) {
114114
}
115115

116116
func TestArgsUsage(t *testing.T) {
117-
arg := &IntArg{
117+
arg := &Int64Arg{
118118
Name: "ia",
119119
Min: 0,
120120
Max: 1,

command_test.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func TestCommand_OnUsageError_hasCommandContext(t *testing.T) {
355355
cmd := &Command{
356356
Name: "bar",
357357
Flags: []Flag{
358-
&IntFlag{Name: "flag"},
358+
&Int64Flag{Name: "flag"},
359359
},
360360
OnUsageError: func(_ context.Context, cmd *Command, err error, _ bool) error {
361361
return fmt.Errorf("intercepted in %s: %s", cmd.Name, err.Error())
@@ -370,7 +370,7 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
370370
cmd := &Command{
371371
Name: "bar",
372372
Flags: []Flag{
373-
&IntFlag{Name: "flag"},
373+
&Int64Flag{Name: "flag"},
374374
},
375375
OnUsageError: func(_ context.Context, _ *Command, err error, _ bool) error {
376376
assert.ErrorContains(t, err, "strconv.ParseInt: parsing \"wrong\"")
@@ -391,7 +391,7 @@ func TestCommand_OnUsageError_WithSubcommand(t *testing.T) {
391391
},
392392
},
393393
Flags: []Flag{
394-
&IntFlag{Name: "flag"},
394+
&Int64Flag{Name: "flag"},
395395
},
396396
OnUsageError: func(_ context.Context, _ *Command, err error, _ bool) error {
397397
assert.ErrorContains(t, err, "parsing \"wrong\": invalid syntax")
@@ -478,7 +478,7 @@ func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
478478
Name: "bar",
479479
Usage: "this is for testing",
480480
Flags: []Flag{
481-
&IntFlag{
481+
&Int64Flag{
482482
Name: "number",
483483
Usage: "A number to parse",
484484
},
@@ -558,7 +558,7 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
558558
Name: "strd1", // no category set and also hidden
559559
Hidden: true,
560560
},
561-
&IntFlag{
561+
&Int64Flag{
562562
Name: "intd",
563563
Aliases: []string{"altd1", "altd2"},
564564
Category: "cat1",
@@ -1253,11 +1253,11 @@ func TestCommand_ParseSliceFlags(t *testing.T) {
12531253
{
12541254
Name: "cmd",
12551255
Flags: []Flag{
1256-
&IntSliceFlag{Name: "p", Value: []int64{}, Usage: "set one or more ip addr"},
1256+
&Int64SliceFlag{Name: "p", Value: []int64{}, Usage: "set one or more ip addr"},
12571257
&StringSliceFlag{Name: "ip", Value: []string{}, Usage: "set one or more ports to open"},
12581258
},
12591259
Action: func(_ context.Context, cmd *Command) error {
1260-
parsedIntSlice = cmd.IntSlice("p")
1260+
parsedIntSlice = cmd.Int64Slice("p")
12611261
parsedStringSlice = cmd.StringSlice("ip")
12621262
return nil
12631263
},
@@ -1281,11 +1281,11 @@ func TestCommand_ParseSliceFlagsWithMissingValue(t *testing.T) {
12811281
{
12821282
Name: "cmd",
12831283
Flags: []Flag{
1284-
&IntSliceFlag{Name: "a", Usage: "set numbers"},
1284+
&Int64SliceFlag{Name: "a", Usage: "set numbers"},
12851285
&StringSliceFlag{Name: "str", Usage: "set strings"},
12861286
},
12871287
Action: func(_ context.Context, cmd *Command) error {
1288-
parsedIntSlice = cmd.IntSlice("a")
1288+
parsedIntSlice = cmd.Int64Slice("a")
12891289
parsedStringSlice = cmd.StringSlice("str")
12901290
return nil
12911291
},
@@ -2301,7 +2301,7 @@ func TestCommand_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
23012301
func TestCommand_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
23022302
cmd := &Command{
23032303
Flags: []Flag{
2304-
&IntFlag{Name: "flag"},
2304+
&Int64Flag{Name: "flag"},
23052305
},
23062306
OnUsageError: func(_ context.Context, _ *Command, err error, isSubcommand bool) error {
23072307
assert.False(t, isSubcommand, "Expect subcommand")
@@ -2455,7 +2455,7 @@ func TestHandleExitCoder_Custom(t *testing.T) {
24552455
func TestShellCompletionForIncompleteFlags(t *testing.T) {
24562456
cmd := &Command{
24572457
Flags: []Flag{
2458-
&IntFlag{
2458+
&Int64Flag{
24592459
Name: "test-completion",
24602460
},
24612461
},
@@ -2808,7 +2808,7 @@ func TestFlagAction(t *testing.T) {
28082808
return err
28092809
},
28102810
},
2811-
&IntFlag{
2811+
&Int64Flag{
28122812
Name: "f_int",
28132813
Local: true,
28142814
Action: func(_ context.Context, cmd *Command, v int64) error {
@@ -2819,7 +2819,7 @@ func TestFlagAction(t *testing.T) {
28192819
return err
28202820
},
28212821
},
2822-
&IntSliceFlag{
2822+
&Int64SliceFlag{
28232823
Name: "f_int_slice",
28242824
Local: true,
28252825
Action: func(_ context.Context, cmd *Command, v []int64) error {
@@ -2846,7 +2846,7 @@ func TestFlagAction(t *testing.T) {
28462846
return err
28472847
},
28482848
},
2849-
&UintFlag{
2849+
&Uint64Flag{
28502850
Name: "f_uint",
28512851
Local: true,
28522852
Action: func(_ context.Context, cmd *Command, v uint64) error {
@@ -2906,15 +2906,15 @@ func TestPersistentFlag(t *testing.T) {
29062906
return nil
29072907
},
29082908
},
2909-
&IntSliceFlag{
2909+
&Int64SliceFlag{
29102910
Name: "persistentCommandSliceFlag",
29112911
Destination: &persistentCommandSliceInt,
29122912
},
29132913
&FloatSliceFlag{
29142914
Name: "persistentCommandFloatSliceFlag",
29152915
Value: []float64{11.3, 12.5},
29162916
},
2917-
&IntFlag{
2917+
&Int64Flag{
29182918
Name: "persistentCommandOverrideFlag",
29192919
Destination: &appOverrideInt,
29202920
},
@@ -2928,16 +2928,16 @@ func TestPersistentFlag(t *testing.T) {
29282928
{
29292929
Name: "cmd",
29302930
Flags: []Flag{
2931-
&IntFlag{
2931+
&Int64Flag{
29322932
Name: "cmdFlag",
29332933
Destination: &topInt,
29342934
Local: true,
29352935
},
2936-
&IntFlag{
2936+
&Int64Flag{
29372937
Name: "cmdPersistentFlag",
29382938
Destination: &topPersistentInt,
29392939
},
2940-
&IntFlag{
2940+
&Int64Flag{
29412941
Name: "paof",
29422942
Aliases: []string{"persistentCommandOverrideFlag"},
29432943
Destination: &appOverrideCmdInt,
@@ -2948,7 +2948,7 @@ func TestPersistentFlag(t *testing.T) {
29482948
{
29492949
Name: "subcmd",
29502950
Flags: []Flag{
2951-
&IntFlag{
2951+
&Int64Flag{
29522952
Name: "cmdFlag",
29532953
Destination: &subCommandInt,
29542954
Local: true,
@@ -3080,7 +3080,7 @@ func TestRequiredFlagDelayed(t *testing.T) {
30803080
{
30813081
Name: "sub",
30823082
Flags: []Flag{
3083-
&IntFlag{
3083+
&Int64Flag{
30843084
Name: "if",
30853085
Required: true,
30863086
},
@@ -3169,7 +3169,7 @@ func TestFlagDuplicates(t *testing.T) {
31693169
Name: "sflag",
31703170
OnlyOnce: true,
31713171
},
3172-
&IntSliceFlag{
3172+
&Int64SliceFlag{
31733173
Name: "isflag",
31743174
},
31753175
&FloatSliceFlag{
@@ -3180,7 +3180,7 @@ func TestFlagDuplicates(t *testing.T) {
31803180
Name: "bifflag",
31813181
OnlyOnce: true,
31823182
},
3183-
&IntFlag{
3183+
&Int64Flag{
31843184
Name: "iflag",
31853185
},
31863186
},
@@ -3261,47 +3261,47 @@ func TestShorthandCommand(t *testing.T) {
32613261
func TestCommand_Int(t *testing.T) {
32623262
pCmd := &Command{
32633263
Flags: []Flag{
3264-
&IntFlag{
3264+
&Int64Flag{
32653265
Name: "myflag",
32663266
Value: 12,
32673267
},
32683268
},
32693269
}
32703270
cmd := &Command{
32713271
Flags: []Flag{
3272-
&IntFlag{
3272+
&Int64Flag{
32733273
Name: "top-flag",
32743274
Value: 13,
32753275
},
32763276
},
32773277
parent: pCmd,
32783278
}
32793279

3280-
require.Equal(t, int64(12), cmd.Int("myflag"))
3281-
require.Equal(t, int64(13), cmd.Int("top-flag"))
3280+
require.Equal(t, int64(12), cmd.Int64("myflag"))
3281+
require.Equal(t, int64(13), cmd.Int64("top-flag"))
32823282
}
32833283

32843284
func TestCommand_Uint(t *testing.T) {
32853285
pCmd := &Command{
32863286
Flags: []Flag{
3287-
&UintFlag{
3287+
&Uint64Flag{
32883288
Name: "myflagUint",
32893289
Value: 13,
32903290
},
32913291
},
32923292
}
32933293
cmd := &Command{
32943294
Flags: []Flag{
3295-
&UintFlag{
3295+
&Uint64Flag{
32963296
Name: "top-flag",
32973297
Value: 14,
32983298
},
32993299
},
33003300
parent: pCmd,
33013301
}
33023302

3303-
require.Equal(t, uint64(13), cmd.Uint("myflagUint"))
3304-
require.Equal(t, uint64(14), cmd.Uint("top-flag"))
3303+
require.Equal(t, uint64(13), cmd.Uint64("myflagUint"))
3304+
require.Equal(t, uint64(14), cmd.Uint64("top-flag"))
33053305
}
33063306

33073307
func TestCommand_Float64(t *testing.T) {
@@ -3442,7 +3442,7 @@ func TestCommand_Value(t *testing.T) {
34423442
subCmd := &Command{
34433443
Name: "test",
34443444
Flags: []Flag{
3445-
&IntFlag{
3445+
&Int64Flag{
34463446
Name: "myflag",
34473447
Usage: "doc",
34483448
Aliases: []string{"m", "mf"},
@@ -3455,7 +3455,7 @@ func TestCommand_Value(t *testing.T) {
34553455

34563456
cmd := &Command{
34573457
Flags: []Flag{
3458-
&IntFlag{
3458+
&Int64Flag{
34593459
Name: "top-flag",
34603460
Usage: "doc",
34613461
Aliases: []string{"t", "tf"},
@@ -3675,7 +3675,7 @@ func TestCommand_NumFlags(t *testing.T) {
36753675
func TestCommand_Set(t *testing.T) {
36763676
cmd := &Command{
36773677
Flags: []Flag{
3678-
&IntFlag{
3678+
&Int64Flag{
36793679
Name: "int",
36803680
Value: 5,
36813681
},
@@ -3685,7 +3685,7 @@ func TestCommand_Set(t *testing.T) {
36853685

36863686
r.False(cmd.IsSet("int"))
36873687
r.NoError(cmd.Set("int", "1"))
3688-
r.Equal(int64(1), cmd.Int("int"))
3688+
r.Equal(int64(1), cmd.Int64("int"))
36893689
r.True(cmd.IsSet("int"))
36903690
}
36913691

@@ -4151,7 +4151,7 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
41514151
cmd.Reader, err = os.Open(fp.Name())
41524152
r.NoError(err)
41534153
cmd.Flags = []Flag{
4154-
&IntFlag{
4154+
&Int64Flag{
41554155
Name: "if",
41564156
},
41574157
&FloatFlag{
@@ -4164,7 +4164,7 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
41644164

41654165
actionCalled := false
41664166
cmd.Action = func(ctx context.Context, c *Command) error {
4167-
r.Equal(tst.expectedInt, c.Int("if"))
4167+
r.Equal(tst.expectedInt, c.Int64("if"))
41684168
r.Equal(tst.expectedFloat, c.Float("ff"))
41694169
r.Equal(tst.expectedSlice, c.StringSlice("ssf"))
41704170
actionCalled = true
@@ -4189,15 +4189,15 @@ func TestZeroValueCommand(t *testing.T) {
41894189

41904190
func TestCommandInvalidName(t *testing.T) {
41914191
var cmd Command
4192-
assert.Equal(t, int64(0), cmd.Int("foo"))
4193-
assert.Equal(t, uint64(0), cmd.Uint("foo"))
4192+
assert.Equal(t, int64(0), cmd.Int64("foo"))
4193+
assert.Equal(t, uint64(0), cmd.Uint64("foo"))
41944194
assert.Equal(t, float64(0), cmd.Float("foo"))
41954195
assert.Equal(t, "", cmd.String("foo"))
41964196
assert.Equal(t, time.Time{}, cmd.Timestamp("foo"))
41974197
assert.Equal(t, time.Duration(0), cmd.Duration("foo"))
41984198

4199-
assert.Equal(t, []int64(nil), cmd.IntSlice("foo"))
4200-
assert.Equal(t, []uint64(nil), cmd.UintSlice("foo"))
4199+
assert.Equal(t, []int64(nil), cmd.Int64Slice("foo"))
4200+
assert.Equal(t, []uint64(nil), cmd.Uint64Slice("foo"))
42014201
assert.Equal(t, []float64(nil), cmd.FloatSlice("foo"))
42024202
assert.Equal(t, []string(nil), cmd.StringSlice("foo"))
42034203
}
@@ -4501,7 +4501,7 @@ func TestSliceStringFlagParsing(t *testing.T) {
45014501
func TestJSONExportCommand(t *testing.T) {
45024502
cmd := buildExtendedTestCommand()
45034503
cmd.Arguments = []Argument{
4504-
&IntArg{
4504+
&Int64Arg{
45054505
Name: "fooi",
45064506
},
45074507
}

0 commit comments

Comments
 (0)