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

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

Open
Samyak2 opened this issue Jul 5, 2022 · 8 comments
Open

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

Samyak2 opened this issue Jul 5, 2022 · 8 comments

Comments

@Samyak2
Copy link

Samyak2 commented Jul 5, 2022

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 #166 (comment), 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
Copy link
Owner

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

@Samyak2
Copy link
Author

Samyak2 commented Jul 5, 2022

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 ❤️

@Samyak2
Copy link
Author

Samyak2 commented Jul 5, 2022

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
Copy link
Owner

It's a convention from GNU short flags.

@Samyak2
Copy link
Author

Samyak2 commented Jul 6, 2022

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
Copy link
Owner

Yeah I think it should.

@Samyak2
Copy link
Author

Samyak2 commented Jul 6, 2022

I tried fixing it with this: Samyak2@86d1f36

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
Copy link

dbohdan commented Oct 20, 2024

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.

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

3 participants