How to generate mutually exlusive options that works with "many"? #181
-
New to this crate and somewhat relatively new to rust in general (though I have been programming in C++ and Python for a long time, and did Erlang many years ago). My goal is to get a command line API that accepts any one of the following forms: $ my-command single-file
$ my-command -s at least one file
$ my-command -a at least one file So far I have the following, which appears to generate the correct #[derive(Debug, Clone, Bpaf)]
#[bpaf(options, version)]
pub(super) enum Mode {
/// Process a single file
Default(PathBuf),
Add {
/// Add a file to be tracked by chezmoi_mm
#[bpaf(short('a'), long("add"))]
files: Vec<PathBuf>,
},
Smart {
/// Smartly add a file to either chezmoi or chezmoi_mm
#[bpaf(short('s'), long("smart"))]
files: Vec<PathBuf>,
},
} $ my-command
Usage: (<ARG> | -a ARG... | -s ARG...)
Available options:
-a, --add <ARG> Add a file to be tracked by chezmoi_mm
-s, --smart <ARG> Smartly add a file to either chezmoi or chezmoi_mm
-h, --help Prints help information
-V, --version Prints version information $ my-command -a a b
No such command: `b`, did you mean `-s`? It seems the multiple args part is just ignored? Is this a bug or am I using the library wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
So Now you are trying to pass both If the idea to have multiple Error message is a bit confusing, I'll see if it can be improved. |
Beta Was this translation helpful? Give feedback.
So
Mode
can be one ofDefault
,Add
orSmart
whereDefault
takes a single argument and bothAdd
andSmart
take multiple. Usage looks what we expect.Now you are trying to pass both
Default
value (b
) andAdd
value (-a a
) whichMode
can't represent.If the idea to have multiple
Mode
items - you can nest them in some other structure and have aVec
of them.Error message is a bit confusing, I'll see if it can be improved.