alexflint / go-arg

Struct-based argument parsing in Go
https://pkg.go.dev/github.com/alexflint/go-arg
BSD 2-Clause "Simplified" License
2.04k stars 100 forks source link

[Feature Request] Parsing hexadecimal numbers #266

Closed Kaweees closed 2 months ago

Kaweees commented 2 months ago

I would love to have the ability to specify pass in hexadecimal value(s) as arguments and have them be parsed as decimal values

alexflint commented 2 months ago

Hey @Kaweees , just to confirm, do you mean you would write code like this:

var args struct {
  X int
}
arg.MustParse(&args)

and then on the command line you would write ./myprogram --x 0xFF and x would get the value 255? Seems very reasonable to me.

Kaweees commented 2 months ago

Yes, that would be splendid. It would be nice if it would default to interpreting a decimal number if the number is unable to be parsed as a hexadecmial number.

alexflint commented 2 months ago

Well I think this already works!

package main

import (
    "fmt"
    "github.com/alexflint/go-arg"
)

func main() {
    var args struct {
        X int
    }
    arg.MustParse(&args)

    fmt.Println(args.X)
}
$ go run *.go --x 0xFF
255
$ go run *.go --x 123
123
alexflint commented 2 months ago

Going to close this for now.