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

show better error message when a validator fails #744

Closed
BurntSushi opened this issue Nov 13, 2016 · 2 comments
Closed

show better error message when a validator fails #744

BurntSushi opened this issue Nov 13, 2016 · 2 comments
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations

Comments

@BurntSushi
Copy link
Contributor

BurntSushi commented Nov 13, 2016

Let's say a valid command line invocation is this:

$ rg -j10 foo

but a user types this instead:

$ rg -j10a foo

I have a validator function on the j flag that checks whether it's a valid number or not. If not, it returns an error so that the output looks like this:

error: invalid digit found in string

This is fine as far as it goes, but let's take a look at the output of an error when I try to use a value that clap knows is wrong for a different flag:

$ rg --color wat foo
error: 'wat' isn't a valid value for '--color <WHEN>'
        [values: always, auto, never]


USAGE:
    
    rg [OPTIONS] <pattern> [<path> ...]
    rg [OPTIONS] -e PATTERN ... [<path> ...]
    rg [OPTIONS] --files [<path> ...]
    rg [OPTIONS] --type-list

For more information try --help

This error message is a lot better. In particular, the really critical piece of information is the name of the flag that's causing the problem. For example, consider a more complex command line:

$ rg -C3 -j10a -maxdepth 3 -m1 foo

There are a lot more numbers here, so it's harder to match up the error message with the actual CLI usage.

I think clap knows the flag name when a validator function fails. Could it include that in the error message?

(This is easily my favorite benefit of clap btw. Its failure modes are oh so much better than Docopt. Of course, I knew that before, but still, it's nice. :-))

@kbknapp
Copy link
Member

kbknapp commented Nov 14, 2016

Sorry for the wait, it's been a busy few days in my offline life! 😄

The unsophisticated work around is to use a different validator per argument, at which point the error you return could include the name of argument. I know that's not optimal, just saying for completeness sake 😉

I could easily add a prefix to the error such as, error: argument '-j <val>' failed validation with message: blah Or something to that effect. Thoughts?

@kbknapp kbknapp added D: easy C-enhancement Category: Raise on the bar on expectations A-validators Area: ArgMatches validation logi labels Nov 14, 2016
@BurntSushi
Copy link
Contributor Author

That sounds great! I might even shorten it to error: invalid argument '-j <val>': blah but either sounds good to me!

@homu homu closed this as completed in 65eb338 Nov 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

No branches or pull requests

2 participants