summaryrefslogtreecommitdiff
path: root/parse_test.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2025-05-23 11:01:34 -0400
committerAlex Flint <[email protected]>2025-05-23 11:01:34 -0400
commite778224db2fd0e9e61d93335c98cd6f1ae369077 (patch)
tree2123d9cb5444a1e7a4bd3d92f7aaa535cb71290e /parse_test.go
parent7155e7e98695d85dafd6453d129ff983fc9b560f (diff)
improve handling of negative numbers
Diffstat (limited to 'parse_test.go')
-rw-r--r--parse_test.go89
1 files changed, 77 insertions, 12 deletions
diff --git a/parse_test.go b/parse_test.go
index 249cbf3..4d2df63 100644
--- a/parse_test.go
+++ b/parse_test.go
@@ -120,17 +120,91 @@ func TestNegativeInt(t *testing.T) {
assert.EqualValues(t, args.Foo, -100)
}
+func TestNegativeFloat(t *testing.T) {
+ var args struct {
+ Foo float64
+ }
+ err := parse("-foo -99", &args)
+ require.NoError(t, err)
+ assert.EqualValues(t, args.Foo, -99)
+}
+
+func TestNumericFlag(t *testing.T) {
+ var args struct {
+ UseIPv6 bool `arg:"-6"`
+ Foo int
+ }
+ err := parse("-6", &args)
+ require.NoError(t, err)
+ assert.EqualValues(t, args.UseIPv6, true)
+}
+
+func TestNumericFlagTakesPrecedence(t *testing.T) {
+ var args struct {
+ UseIPv6 bool `arg:"-6"`
+ Foo int
+ }
+ err := parse("-foo -6", &args)
+ require.Error(t, err)
+}
+
+func TestRepeatedNegativeInts(t *testing.T) {
+ var args struct {
+ Ints []int `arg:"--numbers"`
+ }
+ err := parse("--numbers -1 -2 -6", &args)
+ require.NoError(t, err)
+ assert.EqualValues(t, args.Ints, []int{-1, -2, -6})
+}
+
+func TestRepeatedNegativeFloats(t *testing.T) {
+ var args struct {
+ Floats []float32 `arg:"--numbers"`
+ }
+ err := parse("--numbers -1 -2 -6", &args)
+ require.NoError(t, err)
+ assert.EqualValues(t, args.Floats, []float32{-1, -2, -6})
+}
+
+func TestRepeatedNegativeFloatsThenNumericFlag(t *testing.T) {
+ var args struct {
+ Floats []float32 `arg:"--numbers"`
+ UseIPv6 bool `arg:"-6"`
+ }
+ err := parse("--numbers -1 -2 -6", &args)
+ require.NoError(t, err)
+ assert.EqualValues(t, args.Floats, []float32{-1, -2})
+ assert.True(t, args.UseIPv6)
+}
+
+func TestRepeatedNegativeFloatsThenNonexistentFlag(t *testing.T) {
+ var args struct {
+ Floats []float32 `arg:"--numbers"`
+ UseIPv6 bool `arg:"-6"`
+ }
+ err := parse("--numbers -1 -2 -n", &args)
+ require.Error(t, err, "unknown argument -n")
+}
+
+func TestRepeatedNegativeIntsThenFloat(t *testing.T) {
+ var args struct {
+ Ints []int `arg:"--numbers"`
+ }
+ err := parse("--numbers -1 -2 -0.1", &args)
+ require.Error(t, err, "unknown argument -0.1")
+}
+
func TestNegativeIntAndFloatAndTricks(t *testing.T) {
var args struct {
Foo int
Bar float64
N int `arg:"--100"`
}
- err := parse("-foo -100 -bar -60.14 -100 -100", &args)
+ err := parse("-foo -99 -bar -60.14 -100 -101", &args)
require.NoError(t, err)
- assert.EqualValues(t, args.Foo, -100)
+ assert.EqualValues(t, args.Foo, -99)
assert.EqualValues(t, args.Bar, -60.14)
- assert.EqualValues(t, args.N, -100)
+ assert.EqualValues(t, args.N, -101)
}
func TestUint(t *testing.T) {
@@ -525,15 +599,6 @@ func TestMissingValueInMiddle(t *testing.T) {
assert.Error(t, err)
}
-func TestNegativeValue(t *testing.T) {
- var args struct {
- Foo int
- }
- err := parse("--foo -123", &args)
- require.NoError(t, err)
- assert.Equal(t, -123, args.Foo)
-}
-
func TestInvalidInt(t *testing.T) {
var args struct {
Foo int