blyxxyz / lexopt

Minimalist pedantic command line parser
MIT License
294 stars 9 forks source link

Add setting to forbid numeric option names #18

Open jwodder opened 1 year ago

jwodder commented 1 year ago

I'm using lexopt to write a command that supports negative numbers and dates (i.e., things starting with - and a digit) as arguments. If the user doesn't pass -- before such an argument, then lexopt treats, say, -1234 as the short option -1, and I have to use Parser::optional_value() to get the rest of the argument, append it to the -1, and parse the result. This works, but it's a little inconvenient, and it doesn't forbid arguments like -v1234.

I therefore request that lexopt add some setting (likely through a ParserBuilder) for not treating a hyphen followed by a digit as a short option. If you think this isn't sufficiently minimalist for lexopt, that's OK with me; I just thought I'd throw the idea out there.

blyxxyz commented 1 year ago

The current suggested workaround for this uses try_raw_args(), see examples/nonstandard.rs. (For your use case you'd want to get rid of .strip_prefix('-')?.)

But it's a little unwieldy and it's not too obvious and you're not the first one to want to handle -123 specially. So maybe it deserves more attention.

At a minimum I can call it out in the README as an example. (It's linked from the "Command line syntax" section but the description is on the cryptic side.)

A dedicated feature could be a setting or it could even be something else. Some considerations:

Since there's a full workaround I'm not in a hurry to implement it but I will keep this issue open. Thanks for bringing it up!