Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to Matches to support getting the index of an argument #79

Merged
merged 3 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ impl Options {

let mut vals = (0..opts.len())
.map(|_| Vec::new())
.collect::<Vec<Vec<Optval>>>();
.collect::<Vec<Vec<(usize, Optval)>>>();
let mut free: Vec<String> = Vec::new();
let args = args
.into_iter()
Expand All @@ -365,6 +365,7 @@ impl Options {
.map(|s| s.to_owned())
}).collect::<::std::result::Result<Vec<_>, _>>()?;
let mut args = args.into_iter().peekable();
let mut arg_pos = 0;
while let Some(cur) = args.next() {
if !is_arg(&cur) {
free.push(cur);
Expand Down Expand Up @@ -440,7 +441,7 @@ impl Options {
if name_pos == names.len() && i_arg.is_some() {
return Err(UnexpectedArgument(nm.to_string()));
}
vals[optid].push(Given);
vals[optid].push((arg_pos, Given));
}
Maybe => {
// Note that here we do not handle `--arg value`.
Expand All @@ -450,28 +451,29 @@ impl Options {
// option at the end of the arguments when
// FloatingFrees is in use.
if let Some(i_arg) = i_arg.take() {
vals[optid].push(Val(i_arg));
vals[optid].push((arg_pos, Val(i_arg)));
} else if was_long
|| name_pos < names.len()
|| args.peek().map_or(true, |n| is_arg(&n))
{
vals[optid].push(Given);
vals[optid].push((arg_pos, Given));
} else {
vals[optid].push(Val(args.next().unwrap()));
vals[optid].push((arg_pos, Val(args.next().unwrap())));
}
}
Yes => {
if let Some(i_arg) = i_arg.take() {
vals[optid].push(Val(i_arg));
vals[optid].push((arg_pos, Val(i_arg)));
} else if let Some(n) = args.next() {
vals[optid].push(Val(n));
vals[optid].push((arg_pos, Val(n)));
} else {
return Err(ArgumentMissing(nm.to_string()));
}
}
}
}
}
arg_pos += 1;
}
debug_assert_eq!(vals.len(), opts.len());
for (vals, opt) in vals.iter().zip(opts.iter()) {
Expand Down Expand Up @@ -701,8 +703,8 @@ enum Optval {
pub struct Matches {
/// Options that matched
opts: Vec<Opt>,
/// Values of the Options that matched
vals: Vec<Vec<Optval>>,
/// Values of the Options that matched and their positions
vals: Vec<Vec<(usize, Optval)>>,
/// Free string fragments
pub free: Vec<String>,
}
Expand Down Expand Up @@ -799,15 +801,15 @@ impl OptGroup {
}

impl Matches {
fn opt_vals(&self, nm: &str) -> Vec<Optval> {
fn opt_vals(&self, nm: &str) -> Vec<(usize, Optval)> {
match find_opt(&self.opts, &Name::from_str(nm)) {
Some(id) => self.vals[id].clone(),
None => panic!("No option '{}' defined", nm),
}
}

fn opt_val(&self, nm: &str) -> Option<Optval> {
self.opt_vals(nm).into_iter().next()
self.opt_vals(nm).into_iter().map(|(_, o)| o).next()
}
/// Returns true if an option was defined
pub fn opt_defined(&self, nm: &str) -> bool {
Expand All @@ -824,6 +826,11 @@ impl Matches {
self.opt_vals(nm).len()
}

/// Returns a vector of all the positions in which an option was matched.
pub fn opt_positions(&self, nm: &str) -> Vec<usize> {
self.opt_vals(nm).into_iter().map(|(pos, _)| pos).collect()
}

/// Returns true if any of several options were matched.
pub fn opts_present(&self, names: &[String]) -> bool {
names
Expand Down Expand Up @@ -851,12 +858,25 @@ impl Matches {
pub fn opt_strs(&self, nm: &str) -> Vec<String> {
self.opt_vals(nm)
.into_iter()
.filter_map(|v| match v {
.filter_map(|(_, v)| match v {
Val(s) => Some(s),
_ => None,
}).collect()
}

/// Returns a vector of the arguments provided to all matches of the given
/// option, together with their positions.
///
/// Used when an option accepts multiple values.
pub fn opt_strs_pos(&self, nm: &str) -> Vec<(usize, String)> {
self.opt_vals(nm)
.into_iter()
.filter_map(|(p, v)| match v {
Val(s) => Some((p, s)),
_ => None,
}).collect()
}

/// Returns the string argument supplied to a matching option or `None`.
pub fn opt_str(&self, nm: &str) -> Option<String> {
match self.opt_val(nm) {
Expand Down
52 changes: 51 additions & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ Options:
-c, --brûlée brûlée quite long description
-k, --kiwi€ kiwi description
-o, --orange‹ orange description
-r, --raspberry-but-making-this-option-way-too-long
-r, --raspberry-but-making-this-option-way-too-long\u{0020}
raspberry description is also quite long indeed longer
than every other piece of text we might encounter here
and thus will be automatically broken up
Expand Down Expand Up @@ -1188,3 +1188,53 @@ fn test_opt_get_default() {
let p_arg = matches.opt_get_default("p", 10.2);
assert_eq!(p_arg, Ok(1.1));
}

#[test]
fn test_opt_positions() {
let mut opts = Options::new();
opts.optflagmulti("a", "act", "Description");
opts.optflagmulti("e", "enact", "Description");
opts.optflagmulti("r", "react", "Description");

let args: Vec<String> = ["-a", "-a", "-r", "-a", "-r", "-r"]
.iter()
.map(|x| x.to_string())
.collect();

let matches = &match opts.parse(&args) {
Ok(m) => m,
Err(e) => panic!("{}", e),
};

let a_pos = matches.opt_positions("a");
assert_eq!(a_pos, vec![0, 1, 3]);
let e_pos = matches.opt_positions("e");
assert_eq!(e_pos, vec![]);
let r_pos = matches.opt_positions("r");
assert_eq!(r_pos, vec![2, 4, 5]);
}

#[test]
fn test_opt_strs_pos() {
let mut opts = Options::new();
opts.optmulti("a", "act", "Description", "NUM");
opts.optmulti("e", "enact", "Description", "NUM");
opts.optmulti("r", "react", "Description", "NUM");

let args: Vec<String> = ["-a1", "-a2", "-r3", "-a4", "-r5", "-r6"]
.iter()
.map(|x| x.to_string())
.collect();

let matches = &match opts.parse(&args) {
Ok(m) => m,
Err(e) => panic!("{}", e),
};

let a_pos = matches.opt_strs_pos("a");
assert_eq!(a_pos, vec![(0, "1".to_string()), (1, "2".to_string()), (3, "4".to_string())]);
let e_pos = matches.opt_strs_pos("e");
assert_eq!(e_pos, vec![]);
let r_pos = matches.opt_strs_pos("r");
assert_eq!(r_pos, vec![(2, "3".to_string()), (4, "5".to_string()), (5, "6".to_string())]);
}