Commander icon indicating copy to clipboard operation
Commander copied to clipboard

Impossible to pass a negative number as the value for an option

Open rpendleton opened this issue 3 years ago • 3 comments

If you configure an option to accept a number, it seems it's impossible to pass a negative number for the value of the option since it's interpreted as a flag instead.

I'm not 100% sure what the right way to resolve this would be without making a mess of things, but I figure it's something that should at least have a workaround.

Group {
    $0.command("submit", Option<Double>("nice", default: 0, flag: "n")) { niceValue in
        print(niceValue)
    }
}.run()

Attempts to use that flag:

$ swiftmatrix submit --nice 123
123.0

$ swiftmatrix submit --nice -123
An error occurred: Unexpected flag `-123` as a value for `--nice`

$ swiftmatrix submit --nice -0.123
An error occurred: Unexpected flag `-0.123` as a value for `--nice`

$ swiftmatrix submit --nice=-123
Unknown Arguments: --nice=-123

Options:
    --nice [default: 0.0]

$ swiftmatrix submit --nice=123
Unknown Arguments: --nice=123

Options:
    --nice [default: 0.0]

rpendleton avatar Mar 23 '22 05:03 rpendleton

I think supporting = would make the most sense, that would be consistent with other tools I've used in the past.

# valid
$ gateways list -f=-5m -t=now
$ gateways list --from=-5m --to=now

# not valid
$ gateways list -f -5m -t now
$ gateways list --from -5m --to now

kylef avatar Mar 25 '22 10:03 kylef

@rpendleton There are some changes in the repo with f22f0a07e02d924406a539d06c0d59402886de56 and e1ba47ea7f998f80f2ea5acc65379752438e79ab to support using equals with options and flags if you'd like to give that a go.

kylef avatar Mar 26 '22 09:03 kylef

Thanks. I finally got around to testing the changes in those two commits, and while I can confirm = can be used to specify arguments now, it seems it doesn't work for negative values.

Using the same code from my initial issue description:

$ swiftmatrix submit --nice 123
123.0

$ swiftmatrix submit --nice=123
123.0

$ swiftmatrix submit --nice=-123
An error occurred: Missing argument

rpendleton avatar Apr 06 '22 19:04 rpendleton