Is parsing more complex in Clap 3.2? #3839
-
I'm working through an upgrade to 3.2, which I generally think is going in a great direction. It's not totally clear to me how to upgrade some code like this, though: /// specifies an I2C controller
#[clap(long, short, value_name = "controller",
parse(try_from_str = parse_int::parse),
)]
controller: Option<u8>, I think, from reading the derive reference, that this code should be using I was trying to figure it out, and got this, which doesn't work #[clap(long, short, value_name = "controller",
value_parser(clap::builder::ValueParser::new(parse_int::parse)),
)]
controller: Option<u8>, by saying type annotations are needed. And now I feel a bit stuck. I don't have a novel type here, just an |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Yes, that should be using Working example: #!/usr/bin/env -S rust-script --debug
//! ```cargo
//! [dependencies]
//! clap = { path = "../clap", features = ["derive"] }
//! parse_int = "*"
//! ```
use clap::{Args, Parser, Subcommand};
use std::path;
#[derive(Debug, Parser)]
struct Cli {
#[clap(
long,
short,
value_name = "controller",
value_parser = parse_int::parse::<u8>
)]
controller: Option<u8>,
}
fn main() {
let cli = Cli::parse();
dbg!(cli);
} |
Beta Was this translation helpful? Give feedback.
Yes, that should be using
value_parser
. It seems a downside to the new approach is theu8
isn't showing up anywhere for inference to determine the type forparse_int
, so you need the turbo fish. Unfortunately,. I don't think there is a way to get type inference working.Working example: