Skip to content

Convert flag to optional option #5598

Answered by epage
devmatteini asked this question in Q&A
Discussion options

You must be logged in to vote

Yes, this is implicitly a num_args(0..=1) which comes with a big warning in the docs.

One option not covered there is to use require_equals = true

#!/usr/bin/env nargo
---
[dependencies]
clap = { path = "../clap", features =["derive"] }
---

use clap::Parser;

#[derive(Debug, Parser)]
pub struct Cli {
    #[arg(short, long, require_equals = true)]
    install: Option<Option<String>>,

    repo: String,
}

fn main() {
    let cli = Cli::parse();
    println!("{cli:?}");
}
$ ./clap-5598.rs  --install a
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.09s
     Running `/home/epage/src/personal/cargo/target/debug/cargo -Zscript ./clap-5598.rs --install a`
warning: `package.…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@devmatteini
Comment options

Answer selected by devmatteini
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants