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

imp: Make clap_derive call FromStr::from_str only once per argument. #2206

Closed
wants to merge 1 commit into from

Commits on Jul 13, 2021

  1. imp: Make clap_derive call FromStr::from_str only once per argument.

    Currently the way `clap_derive` uses a `from_str` function is to call
    it once as a validator, discarding the parsed value, and then to call
    it again to recompute the parsed value. This is unfortunate in
    cases where `from_str` is expensive or has side effects.
    
    This PR changes `clap_derive` to not register `from_str` as a validator
    so that it doesn't do the first of these two calls. Then, instead of
    doing `unwrap()` on the other call, it handles the error. This eliminates
    the redundancy, and also avoids the small performance hit mentioned in
    [the documentation about validators].
    
    [the documentation about validators]: https://docs.rs/clap-v3/3.0.0-beta.1/clap_v3/struct.Arg.html#method.validator
    
    This PR doesn't yet use colorized messages for errors generated during
    parsing because the `ColorWhen` setting isn't currently available.
    That's fixable with some refactoring, but I'm interested in getting
    feedback on the overall approach here first.
    sunfishcode committed Jul 13, 2021
    Configuration menu
    Copy the full SHA
    db8c060 View commit details
    Browse the repository at this point in the history