From a592b663c4069451a89c8801807b39d8bfb0e5da Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Thu, 16 May 2019 02:17:34 -0400 Subject: [PATCH] Parse options without names vector The only time the names vector has more than one `Name` in it is when parsing contiguous short options (`-abc`). We would continue putting `Name`s into the vector until we hit a short option that takes an argument. At that point, we break, so we can process the argument. So there's no point to reprocess the head of that vector, we already know they don't take arguments. Only the last one matters. --- src/lib.rs | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9a9d5d20..d0a9f80f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -380,7 +380,7 @@ impl Options { free.extend(args); break; } else { - let mut names; + let mut name = None; let mut i_arg = None; let mut was_long = true; if cur.as_bytes()[1] == b'-' || self.long_only { @@ -391,57 +391,53 @@ impl Options { &cur[1..] }; let mut parts = tail.splitn(2, '='); - names = vec![Name::from_str(parts.next().unwrap())]; + name = Some(Name::from_str(parts.next().unwrap())); if let Some(rest) = parts.next() { i_arg = Some(rest.to_string()); } } else { was_long = false; - names = Vec::new(); for (j, ch) in cur.char_indices().skip(1) { let opt = Short(ch); - /* In a series of potential options (eg. -aheJ), if we - see one which takes an argument, we assume all - subsequent characters make up the argument. This - allows options such as -L/usr/local/lib/foo to be - interpreted correctly - */ - let opt_id = match find_opt(&opts, &opt) { Some(id) => id, None => return Err(UnrecognizedOption(opt.to_string())), }; - names.push(opt); - + // In a series of potential options (eg. -aheJ), if we + // see one which takes an argument, we assume all + // subsequent characters make up the argument. This + // allows options such as -L/usr/local/lib/foo to be + // interpreted correctly let arg_follows = match opts[opt_id].hasarg { Yes | Maybe => true, No => false, }; if arg_follows { + name = Some(opt); let next = j + ch.len_utf8(); if next < cur.len() { i_arg = Some(cur[next..].to_string()); break; } + } else { + vals[opt_id].push((arg_pos, Given)); } } } - let mut name_pos = 0; - for nm in names.iter() { - name_pos += 1; - let optid = match find_opt(&opts, &nm) { + if let Some(nm) = name { + let opt_id = match find_opt(&opts, &nm) { Some(id) => id, None => return Err(UnrecognizedOption(nm.to_string())), }; - match opts[optid].hasarg { + match opts[opt_id].hasarg { No => { - if name_pos == names.len() && i_arg.is_some() { + if i_arg.is_some() { return Err(UnexpectedArgument(nm.to_string())); } - vals[optid].push((arg_pos, Given)); + vals[opt_id].push((arg_pos, Given)); } Maybe => { // Note that here we do not handle `--arg value`. @@ -451,21 +447,20 @@ 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((arg_pos, Val(i_arg))); + vals[opt_id].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((arg_pos, Given)); + vals[opt_id].push((arg_pos, Given)); } else { - vals[optid].push((arg_pos, Val(args.next().unwrap()))); + vals[opt_id].push((arg_pos, Val(args.next().unwrap()))); } } Yes => { if let Some(i_arg) = i_arg.take() { - vals[optid].push((arg_pos, Val(i_arg))); + vals[opt_id].push((arg_pos, Val(i_arg))); } else if let Some(n) = args.next() { - vals[optid].push((arg_pos, Val(n))); + vals[opt_id].push((arg_pos, Val(n))); } else { return Err(ArgumentMissing(nm.to_string())); }