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
#[clap(parse(try_from_str))]
doesn't use try_from(&str)
#2437
Comments
#[clap(parse(try_from_str)]
doesn't use try_from(&str)
#[clap(parse(try_from_str))]
doesn't use try_from(&str)
It looks like this also exists in structopt. The challenge is naming. For our Granted, if we did auto-detection like #2298, this problem goes away except when the user wants to override for a specific case. |
Forgot to add, from observation, it looks like the naming scheme has less to do with what trait is the default but describes what the function signature is when providing a version of it. From that perspective, I'd be tempted to Close this as Won't Fix. |
I've not confirmed it but I believe the user workaround would be |
The workaround works for most cases, but some use weird types where a wrapper is still necessary. Here are examples for both: use clap::Clap;
use reqwest::Url;
use scraper::Selector;
use std::convert::TryFrom;
/*
Available functions:
reqwest::Url::try_from(s: &'a str) -> Result<Self, Self::Error>
reqwest::Url::from_str(input: &str) -> Result<Url, crate::ParseError>
scraper::Selector::try_from(s: &'i str) -> Result<Self, Self::Error>
*/
#[derive(Clap, Debug)]
#[clap(name = env!("CARGO_PKG_NAME"))]
struct Args {
// There is no problem with parsing url. it's just a working example (also from `try_from`)
#[clap(
takes_value = true,
parse(try_from_str = Url::try_from),
)]
url: Url,
#[clap( // Fails with many compile errors
takes_value = true,
parse(try_from_str = Selector::try_from),
)]
selector: Selector,
}
fn main() {
let args = Args::parse();
println!("{:#?}", args);
}
That sadly doesn't fix it completely, because the If you don't have to add anything this can be closed, thx |
Let's keep this open since I have the impression of tackling this as part of #2683. |
url: Url,
#[clap( // Fails with many compile errors
takes_value = true,
parse(try_from_str = Selector::try_from),
)]
selector: Selector,*/ The errors have less to do with Technically, you do not need a full wrapper type to resolve this. You can do it just by defining a function and passing that in instead. |
Since the names are intended to convey the signature, not the trait, this is working as intended and I'm going to go ahead and close this. @pksunkara if there are parts of this you want to resolve in #2683, please comment in that issue with it. If there are any concerns about this, let us know! |
Please complete the following tasks
I have searched the discussions
I have searched the existing issues
Rust Version
rustc 1.50.0 (cb75ad5db 2021-02-10)
Clap Version
3.0.0-beta.2
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run -- https://exameple.com body
Actual Behaviour
It does not compile because
#[clap(parse(try_from_str))]
doesn't findSelector::try_from(&str)
.Expected Behaviour
It compiles and finds the
try_from(&str)
ofSelector
.Additional Context
From the name
try_from_str
I would expect Clap to usetry_from(&str) -> Result<_>
rather than (/both)from(&str) -> Result<_>
.#1661
Debug Output
The text was updated successfully, but these errors were encountered: