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

feat: Auto-detect FromStr, TryFrom<&OsStr>, etc., in clap_derive #2298

Closed
wants to merge 2 commits into from

Commits on Jan 17, 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 Jan 17, 2021
    Configuration menu
    Copy the full SHA
    b8b112b View commit details
    Browse the repository at this point in the history

Commits on Jan 18, 2021

  1. feat: Auto-detect FromStr, TryFrom<&OsStr>, etc., in clap_derive

    Clap has several different options for parsing strings into values:
    `try_from_str`, `try_from_os_str`, `from_str`, `from_os_str`, each
    corresponding to a different way of parsing strings.
    
    This patch adds a new option: `auto`, which auto-detects which parsing
    traits a type supports, out of `clap::ArgEnum`, `TryFrom<&OsStr>`,
    `FromStr`, `TryFrom<&str>`, `From<&OsStr>`, and `From<&str>`, and selects
    the best one to use automatically. This way, users can use any type which
    implements any of these traits, and clap_derive automatically figures out
    what to do.
    
    It's implemented via [autoref specialization], a limited form of
    specialization which only works in macro contexts, but that turns out to
    be enough for clap_derive.
    
    This changes the default to `auto`. The previous default `try_from_str`
    uses the `FromStr` trait. `auto` auto-detects `FromStr`, so this is a
    mostly backwards-compatible change.
    
    [autoref specialization]: http://lukaskalbertodt.github.io/2019/12/05/generalized-autoref-based-specialization.html
    sunfishcode committed Jan 18, 2021
    Configuration menu
    Copy the full SHA
    7bf3928 View commit details
    Browse the repository at this point in the history