Replies: 1 comment 1 reply
-
For now, I am parsing it this way, which parses the str as a single value. #[clap(
long,
help = "Set the default filter for events.",
value_parser = tracer_event_filter_parser,
)]
pub filter: BitFlags<TracerEventKind>, fn tracer_event_filter_parser(filter: &str) -> Result<BitFlags<TracerEventKind>, String> {
let mut result = BitFlags::empty();
for f in filter.split(',') {
let kind = TracerEventKind::from_str(f, false)?;
if result.contains(kind) {
return Err(format!(
"Event kind '{}' is already included in the filter",
kind
));
}
result |= kind;
}
Ok(result)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I have a bitflags enum using https://docs.rs/enumflags2/latest/enumflags2/ .
I would like to know the best practice of parsing bitflags from clap.
Let's say I have the following enum:
And a command line option
--test
that takes combinations of the bitflags e.g.--test A,B,C
:I only want unique values so
num_args = 1..
doesn't work correctly here.I also tried to use custom
value_parser
function in combination withnum_args
. But it appears that value_parse only parses one value at a time which means it can't throw an error when a non-unique value appears. And I can't really OR the bitflags together.I think such use cases should be fairly common in CLI applications so I would like to know the current best practice.
And a stateful value_parser function would be really nice to have, like:
Beta Was this translation helpful? Give feedback.
All reactions