Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undersocre support for numbers (set default base for strconv.Parse* to 0) #404

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func convert(val string, retval reflect.Value, options multiTag) error {
retval.SetBool(b)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
base, err := getBase(options, 10)
base, err := getBase(options, 0)

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

retval.SetInt(parsed)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
base, err := getBase(options, 10)
base, err := getBase(options, 0)

if err != nil {
return err
Expand Down
44 changes: 26 additions & 18 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
)

type defaultOptions struct {
Int int `long:"i"`
IntDefault int `long:"id" default:"1"`
Int int `long:"i"`
IntDefault int `long:"id" default:"1"`
IntUnderscore int `long:"idu" default:"1_0"`

Float64 float64 `long:"f"`
Float64Default float64 `long:"fd" default:"-3.14"`
Float64 float64 `long:"f"`
Float64Default float64 `long:"fd" default:"-3.14"`
Float64Underscore float64 `long:"fdu" default:"-3_3.14"`

NumericFlag bool `short:"3"`

Expand Down Expand Up @@ -45,11 +47,13 @@ func TestDefaults(t *testing.T) {
msg: "no arguments, expecting default values",
args: []string{},
expected: defaultOptions{
Int: 0,
IntDefault: 1,
Int: 0,
IntDefault: 1,
IntUnderscore: 10,

Float64: 0.0,
Float64Default: -3.14,
Float64: 0.0,
Float64Default: -3.14,
Float64Underscore: -33.14,

NumericFlag: false,

Expand All @@ -68,13 +72,15 @@ func TestDefaults(t *testing.T) {
},
{
msg: "non-zero value arguments, expecting overwritten arguments",
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"},
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"},
expected: defaultOptions{
Int: 3,
IntDefault: 3,
Int: 3,
IntDefault: 3,
IntUnderscore: 33,

Float64: -2.71,
Float64Default: 2.71,
Float64: -2.71,
Float64Default: 2.71,
Float64Underscore: 22.71,

NumericFlag: true,

Expand All @@ -93,13 +99,15 @@ func TestDefaults(t *testing.T) {
},
{
msg: "zero value arguments, expecting overwritten arguments",
args: []string{"--i=0", "--id=0", "--f=0", "--fd=0", "--str", "", "--strd=\"\"", "--t=0ms", "--td=0s", "--m=:0", "--md=:0", "--s=0", "--sd=0"},
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"},
expected: defaultOptions{
Int: 0,
IntDefault: 0,
Int: 0,
IntDefault: 0,
IntUnderscore: 0,

Float64: 0,
Float64Default: 0,
Float64: 0,
Float64Default: 0,
Float64Underscore: 0,

String: "",
StringDefault: "",
Expand Down