iffy / nim-argparse

Argument parsing for Nim
MIT License
121 stars 8 forks source link

Support for type converters / custom validators? #30

Open bluenote10 opened 4 years ago

bluenote10 commented 4 years ago

First of all: Great project! Finally writing CLIs will become convenient in Nim ;).

I was wondering if it is possible to have type converters / custom validators similar to Python/argparse's type that can be used for use cases like:

It would be nice if these use cases would not require a manual post-parsers validation + convert. Is that already possible?

iffy commented 4 years ago

This would be nice, I agree. It's not currently possible. I haven't done it yet because I don't know the best way to do it.

For instance, the Nim proc that changes a string to an int is parseInt, so should the argument use type=parseInt? If so, it'll have to figure out how to extract the return type of that function so that the opts object has the right type.

Another option would be to require people to pass both type=int and validator=parseInt. That would be easier for argparse but maybe not as nice for users?

bluenote10 commented 4 years ago

I would simply go for parser=parseInt. It should be possible to infer the return type statically by something like type(parser("")).

In my opinion, the keyword parser might be more appropriate than copying Python's type=... because