alecthomas / kong

Kong is a command-line parser for Go
MIT License
2.14k stars 139 forks source link

Negative numbers do not work with short version of flag #315

Open Samyak2 opened 2 years ago

Samyak2 commented 2 years ago

Minimal reproducible example

package main

import "github.com/alecthomas/kong"

var Cli struct {
    ArgA float64 `short:"a" description:"Argument A"`
}

func main() {
    kong.Parse(&Cli)
}

The long version works as expected:

go run . --arg-a=-10

The short version:

go run . -a=-10

Errors out:

kong-issue-repro: error: --arg-a: expected a float but got "=-10" (string)
exit status 1

Context

I did look at https://github.com/alecthomas/kong/issues/166#issuecomment-831497480, but there was no info on whether it is expected that the short and long versions behave differently.

I would be willing to contribute a fix if this is an actual bug.

alecthomas commented 2 years ago

= is not valid with short flags, you want -a-10. Strangely that works, but -a -10 does not.

Samyak2 commented 2 years ago

Ah I see. Well, I think I will stick with long versions for flags that can have negative values.

Thank you making kong, I really like how simple it is to use :heart:

Samyak2 commented 2 years ago

Though, if I may ask

= is not valid with short flags

Is that a convention? or is it something that kong does not support?

alecthomas commented 2 years ago

It's a convention from GNU short flags.

Samyak2 commented 2 years ago

I tried this with head and it correctly parses arguments of the form -a -10. Both of these work as expected:

echo "hello world" | head -c 5
echo "hello world" | head -c -5

I think Kong should support this form too. What do you think?

alecthomas commented 2 years ago

Yeah I think it should.

Samyak2 commented 2 years ago

I tried fixing it with this: https://github.com/Samyak2/kong/commit/86d1f36058eb5c30172560250196a28dd9d6e065

It did not work. I don't think that's the right approach as it should only be considered if it's followed by a short flag. Any pointers here?

dbohdan commented 2 weeks ago

I have run into this issue, too. I have just ported a utility from Python and argparse to Go and Kong. In the Python version, the utility accepted arguments like -t -1 to indicate infinite tries. Kong is my favorite Go argument parser so far, but this will be a noticeable regression in the CLI if I stick with it.