Skip to content

Commit 711d710

Browse files
committed
auto merge of #16512 : wickerwaka/rust/getopt-16348, r=brson
I don't know if anything else was relying on the old behavior, this seems more correct. Fixes #16348 If '-F' is allowed to have an optional argument, with the previous version '-FF' would be translated to '-F -F'. In this new version '-FF' translates to '-F' with argument 'F'
2 parents b43596b + 08d7fc7 commit 711d710

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

src/libgetopts/lib.rs

+33-22
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,6 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
571571
}
572572
} else {
573573
let mut j = 1;
574-
let mut last_valid_opt_id = None;
575574
names = Vec::new();
576575
while j < curlen {
577576
let range = cur.as_slice().char_range_at(j);
@@ -584,27 +583,24 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
584583
interpreted correctly
585584
*/
586585

587-
match find_opt(opts.as_slice(), opt.clone()) {
588-
Some(id) => last_valid_opt_id = Some(id),
589-
None => {
590-
let arg_follows =
591-
last_valid_opt_id.is_some() &&
592-
match opts[last_valid_opt_id.unwrap()]
593-
.hasarg {
594-
595-
Yes | Maybe => true,
596-
No => false
597-
};
598-
if arg_follows && j < curlen {
599-
i_arg = Some(cur.as_slice()
600-
.slice(j, curlen).to_string());
601-
break;
602-
} else {
603-
last_valid_opt_id = None;
604-
}
605-
}
606-
}
586+
let opt_id = match find_opt(opts.as_slice(), opt.clone()) {
587+
Some(id) => id,
588+
None => return Err(UnrecognizedOption(opt.to_string()))
589+
};
590+
607591
names.push(opt);
592+
593+
let arg_follows = match opts[opt_id].hasarg {
594+
Yes | Maybe => true,
595+
No => false
596+
};
597+
598+
if arg_follows && range.next < curlen {
599+
i_arg = Some(cur.as_slice()
600+
.slice(range.next, curlen).to_string());
601+
break;
602+
}
603+
608604
j = range.next;
609605
}
610606
}
@@ -617,7 +613,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result {
617613
};
618614
match opts[optid].hasarg {
619615
No => {
620-
if !i_arg.is_none() {
616+
if name_pos == names.len() && !i_arg.is_none() {
621617
return Err(UnexpectedArgument(nm.to_string()));
622618
}
623619
vals.get_mut(optid).push(Given);
@@ -1441,6 +1437,21 @@ mod tests {
14411437

14421438
}
14431439

1440+
#[test]
1441+
fn test_nospace_conflict() {
1442+
let args = vec!("-vvLverbose".to_string(), "-v".to_string() );
1443+
let opts = vec!(optmulti("L", "", "library directory", "LIB"),
1444+
optflagmulti("v", "verbose", "Verbose"));
1445+
let matches = &match getopts(args.as_slice(), opts.as_slice()) {
1446+
result::Ok(m) => m,
1447+
result::Err(e) => fail!( "{}", e )
1448+
};
1449+
assert!(matches.opts_present(["L".to_string()]));
1450+
assert_eq!(matches.opts_str(["L".to_string()]).unwrap(), "verbose".to_string());
1451+
assert!(matches.opts_present(["v".to_string()]));
1452+
assert_eq!(3, matches.opt_count("v"));
1453+
}
1454+
14441455
#[test]
14451456
fn test_long_to_short() {
14461457
let mut short = Opt {

0 commit comments

Comments
 (0)