Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70df99d

Browse files
authored
Merge pull request #404 from GRbit/master
Undersocre support for numbers (set default base for strconv.Parse* to 0)
2 parents c4265d9 + 9090f1a commit 70df99d

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func convert(val string, retval reflect.Value, options multiTag) error {
224224
retval.SetBool(b)
225225
}
226226
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
227-
base, err := getBase(options, 10)
227+
base, err := getBase(options, 0)
228228

229229
if err != nil {
230230
return err
@@ -238,7 +238,7 @@ func convert(val string, retval reflect.Value, options multiTag) error {
238238

239239
retval.SetInt(parsed)
240240
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
241-
base, err := getBase(options, 10)
241+
base, err := getBase(options, 0)
242242

243243
if err != nil {
244244
return err

parser_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import (
1313
)
1414

1515
type defaultOptions struct {
16-
Int int `long:"i"`
17-
IntDefault int `long:"id" default:"1"`
16+
Int int `long:"i"`
17+
IntDefault int `long:"id" default:"1"`
18+
IntUnderscore int `long:"idu" default:"1_0"`
1819

19-
Float64 float64 `long:"f"`
20-
Float64Default float64 `long:"fd" default:"-3.14"`
20+
Float64 float64 `long:"f"`
21+
Float64Default float64 `long:"fd" default:"-3.14"`
22+
Float64Underscore float64 `long:"fdu" default:"-3_3.14"`
2123

2224
NumericFlag bool `short:"3"`
2325

@@ -46,11 +48,13 @@ func TestDefaults(t *testing.T) {
4648
msg: "no arguments, expecting default values",
4749
args: []string{},
4850
expected: defaultOptions{
49-
Int: 0,
50-
IntDefault: 1,
51+
Int: 0,
52+
IntDefault: 1,
53+
IntUnderscore: 10,
5154

52-
Float64: 0.0,
53-
Float64Default: -3.14,
55+
Float64: 0.0,
56+
Float64Default: -3.14,
57+
Float64Underscore: -33.14,
5458

5559
NumericFlag: false,
5660

@@ -69,13 +73,15 @@ func TestDefaults(t *testing.T) {
6973
},
7074
{
7175
msg: "non-zero value arguments, expecting overwritten arguments",
72-
args: []string{"--i=3", "--id=3", "--f=-2.71", "--fd=2.71", "-3", "--str=def", "--strd=def", "--t=3ms", "--td=3ms", "--m=c:3", "--md=c:3", "--s=3", "--sd=3"},
76+
args: []string{"--i=3", "--id=3", "--idu=3_3", "--f=-2.71", "--fd=2.71", "--fdu=2_2.71", "-3", "--str=def", "--strd=def", "--t=3ms", "--td=3ms", "--m=c:3", "--md=c:3", "--s=3", "--sd=3"},
7377
expected: defaultOptions{
74-
Int: 3,
75-
IntDefault: 3,
78+
Int: 3,
79+
IntDefault: 3,
80+
IntUnderscore: 33,
7681

77-
Float64: -2.71,
78-
Float64Default: 2.71,
82+
Float64: -2.71,
83+
Float64Default: 2.71,
84+
Float64Underscore: 22.71,
7985

8086
NumericFlag: true,
8187

@@ -99,13 +105,15 @@ func TestDefaults(t *testing.T) {
99105
},
100106
{
101107
msg: "zero value arguments, expecting overwritten arguments",
102-
args: []string{"--i=0", "--id=0", "--f=0", "--fd=0", "--str", "", "--strd=\"\"", "--t=0ms", "--td=0s", "--m=:0", "--md=:0", "--s=0", "--sd=0"},
108+
args: []string{"--i=0", "--id=0", "--idu=0", "--f=0", "--fd=0", "--fdu=0", "--str", "", "--strd=\"\"", "--t=0ms", "--td=0s", "--m=:0", "--md=:0", "--s=0", "--sd=0"},
103109
expected: defaultOptions{
104-
Int: 0,
105-
IntDefault: 0,
110+
Int: 0,
111+
IntDefault: 0,
112+
IntUnderscore: 0,
106113

107-
Float64: 0,
108-
Float64Default: 0,
114+
Float64: 0,
115+
Float64Default: 0,
116+
Float64Underscore: 0,
109117

110118
String: "",
111119
StringDefault: "",

0 commit comments

Comments
 (0)