I'm building a tool that uses the clap crate. I want to have a few options and/or flags that are required, but exclusive to each other (only one of a grouping can be used).
Intuitively I thought an enum would do the trick but that didn't work.
I've done this kind of thing before but with the builder API, never with the derive API. When using the builder API I would group the options together then do something like this:
Also the examples are a more complicated then needs be. In the example they use the build pattern on top of the struct when in reality anyone searching for derive patterns would want to use the derive API on the fields them selves like I have done below.
#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
struct Cli {
/// The PO csv file to be used
#[clap(short, long)]
input: PathBuf,
/// The destination directory where the processed POs will be saved
#[clap(short, long)]
output: PathBuf,
/// The text file that contains all of the store numbers to be processed
#[clap(short, long)]
list: PathBuf,
/// Print all RFIDs including items marked with a '$'
#[clap(short = 'a', long = "print-all", conflicts_with = "report")]
printall: bool,
/// Produce a report of selected PO
#[clap(short, long, conflicts_with = "printall")]
report: bool,
}
I'm going to fix that on their examples soon. I'm guessing the examples were made before the API was finished.
Speaking of simplification, conflicts_with is bi-directional according to it's documentation, and thus should be sufficient to use only on either item, and is not needed for both...
I am not 100% sure so please dont's take it for granted, but i believe that the buidler excerpt for the group was needed for code to properly generate help strings for the exclusive arguments (e.g. to show --A | --B in help instead of standalone arguments next to each other)