-
I'm trying to use Code example:
What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
epage
Feb 5, 2024
Replies: 2 comments 4 replies
-
As a cargo script #!/usr/bin/env nargo
---
[dependencies]
clap = { version = "4", features = ["derive"] }
---
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
/// blas ccc
struct Cli {
/// one
#[arg(short, long, default_value = "no")]
one: String,
/// test
#[arg(short, long, requires_if("yes", "one"))]
two: Option<String>,
}
fn parse_and_print(args: Vec<&str>) {
println!("Parsing: {:?}", &args);
let cli = Cli::parse_from(args);
println!("\t we get {:?}", cli);
}
fn main() {
parse_and_print(vec![""]);
parse_and_print(vec!["", "--one", "test1"]);
parse_and_print(vec!["", "--one", "test2"]);
parse_and_print(vec!["", "--two", "test1"]);
parse_and_print(vec!["", "--two", "test2"]);
parse_and_print(vec!["", "--one", "test1", "--two", "test2"]);
parse_and_print(vec!["", "--one", "yes", "--two", "OK?"]);
} and output:
|
Beta Was this translation helpful? Give feedback.
3 replies
-
Oh, so going back to my first example, I would need to do something like: use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
/// blas ccc
struct Cli {
/// one
#[arg(short, long, default_value = "no", requires_if("yes", "two"))]
one: String,
/// test
#[arg(short, long)]
two: Option<String>,
}
fn parse_and_print(args: Vec<&str>) {
println!("Parsing: {:?}", &args);
let cli = Cli::try_parse_from(args);
println!("\t we get {:?}", cli);
}
fn main() {
parse_and_print(vec![""]);
parse_and_print(vec!["", "--one", "test1"]);
parse_and_print(vec!["", "--one", "test2"]);
parse_and_print(vec!["", "--two", "test1"]);
parse_and_print(vec!["", "--two", "test2"]);
parse_and_print(vec!["", "--one", "test1", "--two", "test2"]);
parse_and_print(vec!["", "--one", "yes", "--two", "OK?"]);
parse_and_print(vec!["", "--one", "yes"]);
} To make the argument Thanks. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Next time, I'd recommend
If I'm understanding what you are talking about, you are looking at