I have a case where I want the command to have multiple steps. I want that if the user specified a single step then do that step but if the user does not specify any then we do all steps.
step1
step2
step3
If I do just progname then we do all 3 steps. However if I do progname step2 then I do only step2.
Currently I have them as booleans. I check if all booleans are false then I set all to true. Wondering if there was something in Clap which can achieve this directly.
If I can do multiple steps as well then that works too. E.g: progname step2 step3, will do the 2 steps and skip step1 but this is nice to have.
Would a subcommand work? Clap doesn't support running multiple subcommands in a single invocation, but it does support optional subcommands:
use clap::Parser;
#[derive(Debug, Parser)]
struct Args {
#[command(subcommand)]
step: Option<Step>
}
#[derive(Debug, clap::Subcommand)]
enum Step {
One,
Two,
Three
}
fn main() {
let args = Args::parse();
todo!()
}
If the associated program is run as example, then args.step is None and you can use things like unwrap_or to map that to a default behaviour, while if the program is run as example one then args.step is Some(Step::One), which can be used to select step-specific behaviours.