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

Can't specify negative numbers for an int #190

Closed
prashantv opened this issue Aug 27, 2016 · 2 comments
Closed

Can't specify negative numbers for an int #190

prashantv opened this issue Aug 27, 2016 · 2 comments

Comments

@prashantv
Copy link

Example code:

type Options struct {
        Num int `short:"n" long:"num" description:"Number"`
}

func main() {
        var opts Options
        _, err := flags.Parse(&opts)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Println("Num", opts.Num)
}

Works fine for 0/positive numbers:

$ ./flags -n 0
Num 0
$ ./flags -n 1
Num 1

But it fails for negative numbers, since it thinks the - is part of an options:

$ ./flags -n -1
expected argument for flag `-n, --num', but got option `-1'

This works fine with the standard library

var n = flag.Int("n", 0, "Num")

func main() {
        flag.Parse()
        fmt.Println("Num:", *n)
}
$ ./stdflags -n 0
Num: 0
$ ./stdflags -n 1
Num: 1
$ ./stdflags -n -1
Num: -1
@jessevdk
Copy link
Owner

Normally the answer is that this is not supported, if you want to pass anything as an argument starting with -, you need to use the -n=-1 syntax. However, I made an exception for numbers since it's relatively obvious what the user wants in that case.

@prashantv
Copy link
Author

Thanks for the quick fix @jessevdk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants