Skip to content

Commit 8a8aa6a

Browse files
committed
Add Matches::opt_strs_pos
1 parent ef06b9a commit 8a8aa6a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,19 @@ impl Matches {
864864
}).collect()
865865
}
866866

867+
/// Returns a vector of the arguments provided to all matches of the given
868+
/// option, together with their positions.
869+
///
870+
/// Used when an option accepts multiple values.
871+
pub fn opt_strs_pos(&self, nm: &str) -> Vec<(usize, String)> {
872+
self.opt_vals(nm)
873+
.into_iter()
874+
.filter_map(|(p, v)| match v {
875+
Val(s) => Some((p, s)),
876+
_ => None,
877+
}).collect()
878+
}
879+
867880
/// Returns the string argument supplied to a matching option or `None`.
868881
pub fn opt_str(&self, nm: &str) -> Option<String> {
869882
match self.opt_val(nm) {

src/tests/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ Options:
909909
-c, --brûlée brûlée quite long description
910910
-k, --kiwi€ kiwi description
911911
-o, --orange‹ orange description
912-
-r, --raspberry-but-making-this-option-way-too-long
912+
-r, --raspberry-but-making-this-option-way-too-long\u{0020}
913913
raspberry description is also quite long indeed longer
914914
than every other piece of text we might encounter here
915915
and thus will be automatically broken up
@@ -1213,3 +1213,28 @@ fn test_opt_positions() {
12131213
let r_pos = matches.opt_positions("r");
12141214
assert_eq!(r_pos, vec![2, 4, 5]);
12151215
}
1216+
1217+
#[test]
1218+
fn test_opt_strs_pos() {
1219+
let mut opts = Options::new();
1220+
opts.optmulti("a", "act", "Description", "NUM");
1221+
opts.optmulti("e", "enact", "Description", "NUM");
1222+
opts.optmulti("r", "react", "Description", "NUM");
1223+
1224+
let args: Vec<String> = ["-a1", "-a2", "-r3", "-a4", "-r5", "-r6"]
1225+
.iter()
1226+
.map(|x| x.to_string())
1227+
.collect();
1228+
1229+
let matches = &match opts.parse(&args) {
1230+
Ok(m) => m,
1231+
Err(e) => panic!("{}", e),
1232+
};
1233+
1234+
let a_pos = matches.opt_strs_pos("a");
1235+
assert_eq!(a_pos, vec![(0, "1".to_string()), (1, "2".to_string()), (3, "4".to_string())]);
1236+
let e_pos = matches.opt_strs_pos("e");
1237+
assert_eq!(e_pos, vec![]);
1238+
let r_pos = matches.opt_strs_pos("r");
1239+
assert_eq!(r_pos, vec![(2, "3".to_string()), (4, "5".to_string()), (5, "6".to_string())]);
1240+
}

0 commit comments

Comments
 (0)