You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use clap::Parser;typeOptVal = Option<String>;#[derive(Parser,Debug)]structCli{#[arg(long, value_parser = ["turbo","auto","slow"])]mode:OptVal,}fnmain(){let args = Cli::parse();dbg!(&args.mode);}
Steps to reproduce the bug with the above code
cargo r -- --mode auto
Actual Behaviour
thread 'main' panicked at 'Mismatch between definition and access of `mode`. Could not downcast to core::option::Option<alloc::string::String>, need to downcast to alloc::string::String
', src/main.rs:117:11
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Expected Behaviour
[src/main.rs:21] &args.mode = Some(
"auto",
)
Additional Context
If I don't use OptVal type, and use Option<String>, I won't have this problem.
Debug Output
[dependencies]
clap = { version = "4.0.32", features = ["derive", "wrap_help", "unicode"] }
The text was updated successfully, but these errors were encountered:
That application is defined to require a subcommand, so if one isn't specified you get that error.
To make something optional (arg, subcommand), you need to wrap it in an Option. One way to think of it is there isn't a way to construct Cli right now as what will we put in the run field when no subcommand is specified.
2moe
changed the title
value_parser conflicts with subcommand
Could not downcast to type alias
Jan 13, 2023
@epage Thanks.
I have amended this issue and I am not sure if this is appropriate.
For type OptVal = Option<String>, clap will not resolve this type alias(OptVal), but will resolve Option<String>.
Is this an intentional approach?
Clap does a textual comparison on the type to determine what behavior is assumed so people can override it, so type aliases are intentionally treated as unique types. #4626 is discussing changing / improving the situation.
Please complete the following tasks
Rust Version
rustc 1.68.0-nightly (61a415be5 2023-01-12)
Clap Version
4.0.32
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo r -- --mode auto
Actual Behaviour
Expected Behaviour
Additional Context
If I don't use OptVal type, and use
Option<String>
, I won't have this problem.Debug Output
The text was updated successfully, but these errors were encountered: