Skip to content
Discussion options

You must be logged in to vote

clap can group these per occurrence. give the arg num_args(1..) so it collects values until the next -flag, plus ArgAction::Append so each -s starts its own occurrence, then read it back with get_occurrences instead of get_many.

ran it on -s a b -s c d --foo (clap 4.6.1):

Arg::new("s").short('s').action(ArgAction::Append).num_args(1..)
// then:
m.get_occurrences::<String>("s").unwrap()
 .map(|o| o.cloned().collect::<Vec<_>>()).collect::<Vec<_>>()

that gives [["a", "b"], ["c", "d"]], and --foo still parses fine since it ends the last group. get_many flattens the same data to ["a", "b", "c", "d"], which is the usual trap here. get_occurrences is the stabilized form of the old grouped_values_of

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@epage
Comment options

Answer selected by bwo
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants