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

Support for type converters / custom validators? #30

Open
bluenote10 opened this issue Oct 10, 2019 · 2 comments
Open

Support for type converters / custom validators? #30

bluenote10 opened this issue Oct 10, 2019 · 2 comments

Comments

@bluenote10
Copy link

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:

  • Specify that a certain argument needs to be a valid float.
  • Ensure that a certain argument is e.g. an even integer.

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

@iffy
Copy link
Owner

iffy commented Oct 15, 2019

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

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

  • it makes it more clear that it is some string => T function with the possibility of failed parsing,
  • matches well with Nim's parseX functions.

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

2 participants