How to use Option<Args>
with #[command(flatten)]
?
#5687
Answered
by
epage
LazyMechanic
asked this question in
Q&A
-
How can I get this behavior: use clap::{Args, Parser};
#[test]
fn test() {
#[derive(Parser, Debug, Eq, PartialEq)]
pub struct CliArgs {
#[command(flatten)]
pub opt_args: Option<OptArgs>,
}
#[derive(Args, Debug, Eq, PartialEq)]
pub struct OptArgs {
pub arg1: i32,
pub arg2: i32,
}
let result = CliArgs::try_parse_from([""]);
assert!(result.is_ok());
assert_eq!(result.unwrap(), CliArgs { opt_args: None });
let result = CliArgs::try_parse_from(["--arg1=1"]);
assert!(result.is_err());
let result = CliArgs::try_parse_from(["--arg2=1"]);
assert!(result.is_err());
let result = CliArgs::try_parse_from(["--arg1=1", "--arg2=2"]);
assert!(result.is_ok());
assert_eq!(result.unwrap(), CliArgs { opt_args: Some(OptArgs { arg1: 1, arg2: 2 }) });
} |
Beta Was this translation helpful? Give feedback.
Answered by
epage
Aug 21, 2024
Replies: 1 comment 2 replies
-
To double check my understanding, what you are wanting is
Is that correct? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We don't have a built-in concept of "this entire ArgGroup is required". For now, you could specify that each argument
requires
the other arguments. You'll also need to setrequired = true
becauseOptArgs
doesn't know its being used in this way.