Replies: 1 comment
-
Sorry, catching up from a couple of conferences and other stuff. For me, I would do something like #[derive(Parser)]
struct CliArgs {
#[command(flatten)]
arg_group: Option<ArgGroup>,
}
#[derive(Args)]
struct ArgGroup {
#[arg(long)]
one: bool, // if group omitted, set this true
#[arg(long)]
two: bool,
#[arg(long)]
three: bool,
}
impl Default for ArgGroup {
fn default() -> Self {
ArgGroup {
one: true,
two: false,
three: false,
}
}
}
cli.arg_group.unwrap_or_default() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have an optional arg group, and if the group is omitted, I want to apply a default value to one of the args in the group. Is this possible?
The end result should be:
one
is set totrue
,two
andthree
are left as false--one
,one
is set totrue
,two
andthree
are left as false--two
,two
is set totrue
,one
andthree
are left as false--three
,three
is set totrue
,one
andtwo
are left as false--one
,--two
or--three
, the cli should return an errorWhat I've tried
args macro
Specifying the
default
parameter in the args macro is no good, because it will apply this value even if the user specifies a different parameter within that group. e.g:Would set both
one
totrue
if the user specifies--two
or--three
impl Default
I figured I could maybe implement the
Default
trait, and clap might just know to use that as the default value for the group when the group is omitted, but that doesnt seem to be the case. I also cant see any way to specify the default value for a group, e.g.Alternative
Of course, I could always apply some post-clap default values, but it feels a bit messy, and would quickly become more complicated if the arg group has more args, args of different types, etc.
I'm using clap version
4.5.17
with thederive
feature.Beta Was this translation helpful? Give feedback.
All reactions