Skip to content

Commit 58436e3

Browse files
committed
Revert #81, #82, #83
This commit reverts three recent PRs to the `getopts` crate. One of the main consumers of `getopts` is `rustc`, which isn't allowed to have breaking changes. In #82, however, a breaking change was landed. It looks like #83 builds on this change, and while #81 seems unrelated the diffs were somewhat tangled.
1 parent f457f9a commit 58436e3

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

src/lib.rs

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl Options {
364364
.ok_or_else(|| Fail::UnrecognizedOption(format!("{:?}", i.as_ref())))
365365
.map(|s| s.to_owned())
366366
}).collect::<::std::result::Result<Vec<_>, _>>()?;
367-
let mut args = args.into_iter().peekable();
367+
let mut args = args.into_iter();
368368
let mut arg_pos = 0;
369369
while let Some(cur) = args.next() {
370370
if !is_arg(&cur) {
@@ -380,9 +380,8 @@ impl Options {
380380
free.extend(args);
381381
break;
382382
} else {
383-
let mut names;
383+
let mut name = None;
384384
let mut i_arg = None;
385-
let mut was_long = true;
386385
if cur.as_bytes()[1] == b'-' || self.long_only {
387386
let tail = if cur.as_bytes()[1] == b'-' {
388387
&cur[2..]
@@ -391,81 +390,71 @@ impl Options {
391390
&cur[1..]
392391
};
393392
let mut parts = tail.splitn(2, '=');
394-
names = vec![Name::from_str(parts.next().unwrap())];
393+
name = Some(Name::from_str(parts.next().unwrap()));
395394
if let Some(rest) = parts.next() {
396395
i_arg = Some(rest.to_string());
397396
}
398397
} else {
399-
was_long = false;
400-
names = Vec::new();
401398
for (j, ch) in cur.char_indices().skip(1) {
402399
let opt = Short(ch);
403400

404-
/* In a series of potential options (eg. -aheJ), if we
405-
see one which takes an argument, we assume all
406-
subsequent characters make up the argument. This
407-
allows options such as -L/usr/local/lib/foo to be
408-
interpreted correctly
409-
*/
410-
411401
let opt_id = match find_opt(&opts, &opt) {
412402
Some(id) => id,
413403
None => return Err(UnrecognizedOption(opt.to_string())),
414404
};
415405

416-
names.push(opt);
417-
406+
// In a series of potential options (eg. -aheJ), if we
407+
// see one which takes an argument, we assume all
408+
// subsequent characters make up the argument. This
409+
// allows options such as -L/usr/local/lib/foo to be
410+
// interpreted correctly
418411
let arg_follows = match opts[opt_id].hasarg {
419412
Yes | Maybe => true,
420413
No => false,
421414
};
422415

423416
if arg_follows {
417+
name = Some(opt);
424418
let next = j + ch.len_utf8();
425419
if next < cur.len() {
426420
i_arg = Some(cur[next..].to_string());
427421
break;
428422
}
423+
} else {
424+
vals[opt_id].push((arg_pos, Given));
429425
}
430426
}
431427
}
432-
let mut name_pos = 0;
433-
for nm in names.iter() {
434-
name_pos += 1;
435-
let optid = match find_opt(&opts, &nm) {
428+
if let Some(nm) = name {
429+
let opt_id = match find_opt(&opts, &nm) {
436430
Some(id) => id,
437431
None => return Err(UnrecognizedOption(nm.to_string())),
438432
};
439-
match opts[optid].hasarg {
433+
match opts[opt_id].hasarg {
440434
No => {
441-
if name_pos == names.len() && i_arg.is_some() {
435+
if i_arg.is_some() {
442436
return Err(UnexpectedArgument(nm.to_string()));
443437
}
444-
vals[optid].push((arg_pos, Given));
438+
vals[opt_id].push((arg_pos, Given));
445439
}
446440
Maybe => {
447-
// Note that here we do not handle `--arg value`.
448-
// This matches GNU getopt behavior; but also
449-
// makes sense, because if this were accepted,
450-
// then users could only write a "Maybe" long
451-
// option at the end of the arguments when
452-
// FloatingFrees is in use.
441+
// Note that here we do not handle `--arg value` or
442+
// `-a value`. This matches GNU getopt behavior; but
443+
// also makes sense, because if this were accepted,
444+
// then users could only write a "Maybe" option at
445+
// the end of the arguments when FloatingFrees is in
446+
// use.
453447
if let Some(i_arg) = i_arg.take() {
454-
vals[optid].push((arg_pos, Val(i_arg)));
455-
} else if was_long
456-
|| name_pos < names.len()
457-
|| args.peek().map_or(true, |n| is_arg(&n))
458-
{
459-
vals[optid].push((arg_pos, Given));
448+
vals[opt_id].push((arg_pos, Val(i_arg)));
460449
} else {
461-
vals[optid].push((arg_pos, Val(args.next().unwrap())));
450+
vals[opt_id].push((arg_pos, Given));
462451
}
463452
}
464453
Yes => {
465454
if let Some(i_arg) = i_arg.take() {
466-
vals[optid].push((arg_pos, Val(i_arg)));
455+
vals[opt_id].push((arg_pos, Val(i_arg)));
467456
} else if let Some(n) = args.next() {
468-
vals[optid].push((arg_pos, Val(n)));
457+
vals[opt_id].push((arg_pos, Val(n)));
469458
} else {
470459
return Err(ArgumentMissing(nm.to_string()));
471460
}
@@ -551,10 +540,6 @@ impl Options {
551540
row.push_str(&short_name);
552541
if long_name.width() > 0 {
553542
row.push_str(", ");
554-
} else {
555-
// Only a single space here, so that any
556-
// argument is printed in the correct spot.
557-
row.push(' ');
558543
}
559544
}
560545
// FIXME: refer issue #7.
@@ -567,16 +552,21 @@ impl Options {
567552
_ => {
568553
row.push_str(if self.long_only { "-" } else { "--" });
569554
row.push_str(&long_name);
570-
row.push(' ');
571555
}
572556
}
573557

574558
// arg
575559
match hasarg {
576560
No => {}
577-
Yes => row.push_str(&hint),
561+
Yes => {
562+
row.push(' ');
563+
row.push_str(&hint);
564+
}
578565
Maybe => {
579566
row.push('[');
567+
if !long_name.is_empty() {
568+
row.push('=');
569+
}
580570
row.push_str(&hint);
581571
row.push(']');
582572
}
@@ -979,9 +969,13 @@ fn format_option(opt: &OptGroup) -> String {
979969
}
980970

981971
if opt.hasarg != No {
982-
line.push(' ');
983972
if opt.hasarg == Maybe {
984973
line.push('[');
974+
if opt.short_name.is_empty() {
975+
line.push('=');
976+
}
977+
} else {
978+
line.push(' ');
985979
}
986980
line.push_str(&opt.hint);
987981
if opt.hasarg == Maybe {

src/tests/mod.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ fn test_optflagopt() {
379379
let short_args = vec!["-t".to_string(), "x".to_string()];
380380
match opts.parse(&short_args) {
381381
Ok(ref m) => {
382-
assert_eq!(m.opt_str("t").unwrap(), "x");
383-
assert_eq!(m.opt_str("test").unwrap(), "x");
382+
assert_eq!(m.opt_str("t"), None);
383+
assert_eq!(m.opt_str("test"), None);
384384
}
385385
_ => panic!(),
386386
}
@@ -763,6 +763,8 @@ fn test_usage() {
763763
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
764764
opts.optflag("k", "kiwi", "Desc");
765765
opts.optflagopt("p", "", "Desc", "VAL");
766+
opts.optflagopt("", "pea", "Desc", "VAL");
767+
opts.optflagopt("c", "cherry", "Desc", "VAL");
766768
opts.optmulti("l", "", "Desc", "VAL");
767769
opts.optflag("", "starfruit", "Starfruit");
768770

@@ -773,7 +775,9 @@ Options:
773775
-a, --012345678901234567890123456789 VAL
774776
Desc
775777
-k, --kiwi Desc
776-
-p [VAL] Desc
778+
-p[VAL] Desc
779+
--pea[=VAL] Desc
780+
-c, --cherry[=VAL] Desc
777781
-l VAL Desc
778782
--starfruit Starfruit
779783
";
@@ -820,7 +824,7 @@ Options:
820824

821825
debug!("expected: <<{}>>", expected);
822826
debug!("generated: <<{}>>", usage);
823-
assert!(usage == expected)
827+
assert_eq!(usage, expected)
824828
}
825829

826830
#[test]
@@ -851,7 +855,7 @@ Options:
851855

852856
debug!("expected: <<{}>>", expected);
853857
debug!("generated: <<{}>>", usage);
854-
assert!(usage == expected)
858+
assert_eq!(usage, expected)
855859
}
856860

857861
#[test]
@@ -882,7 +886,7 @@ Options:
882886

883887
debug!("expected: <<{}>>", expected);
884888
debug!("generated: <<{}>>", usage);
885-
assert!(usage == expected)
889+
assert_eq!(usage, expected)
886890
}
887891

888892
#[test]
@@ -909,7 +913,7 @@ Options:
909913
-c, --brûlée brûlée quite long description
910914
-k, --kiwi€ kiwi description
911915
-o, --orange‹ orange description
912-
-r, --raspberry-but-making-this-option-way-too-long\u{0020}
916+
-r, --raspberry-but-making-this-option-way-too-long
913917
raspberry description is also quite long indeed longer
914918
than every other piece of text we might encounter here
915919
and thus will be automatically broken up
@@ -919,7 +923,7 @@ Options:
919923

920924
debug!("expected: <<{}>>", expected);
921925
debug!("generated: <<{}>>", usage);
922-
assert!(usage == expected)
926+
assert_eq!(usage, expected)
923927
}
924928

925929
#[test]
@@ -934,13 +938,13 @@ fn test_usage_short_only() {
934938
Options:
935939
-k VAL Kiwi
936940
-s Starfruit
937-
-a [TYPE] Apple
941+
-a[TYPE] Apple
938942
";
939943

940944
let usage = opts.usage("Usage: fruits");
941945
debug!("expected: <<{}>>", expected);
942946
debug!("generated: <<{}>>", usage);
943-
assert!(usage == expected)
947+
assert_eq!(usage, expected)
944948
}
945949

946950
#[test]
@@ -955,13 +959,13 @@ fn test_usage_long_only() {
955959
Options:
956960
--kiwi VAL Kiwi
957961
--starfruit Starfruit
958-
--apple [TYPE] Apple
962+
--apple[=TYPE] Apple
959963
";
960964

961965
let usage = opts.usage("Usage: fruits");
962966
debug!("expected: <<{}>>", expected);
963967
debug!("generated: <<{}>>", usage);
964-
assert!(usage == expected)
968+
assert_eq!(usage, expected)
965969
}
966970

967971
#[test]
@@ -971,9 +975,10 @@ fn test_short_usage() {
971975
opts.optopt("a", "012345678901234567890123456789", "Desc", "VAL");
972976
opts.optflag("k", "kiwi", "Desc");
973977
opts.optflagopt("p", "", "Desc", "VAL");
974-
opts.optmulti("l", "", "Desc", "VAL");
978+
opts.optflagopt("", "pea", "Desc", "VAL");
979+
opts.optflagopt("c", "cherry", "Desc", "VAL");
975980

976-
let expected = "Usage: fruits -b VAL [-a VAL] [-k] [-p [VAL]] [-l VAL]..".to_string();
981+
let expected = "Usage: fruits -b VAL [-a VAL] [-k] [-p[VAL]] [--pea[=VAL]] [-c[VAL]]".to_string();
977982
let generated_usage = opts.short_usage("fruits");
978983

979984
debug!("expected: <<{}>>", expected);
@@ -1026,7 +1031,7 @@ Options:
10261031

10271032
debug!("expected: <<{}>>", expected);
10281033
debug!("generated: <<{}>>", usage);
1029-
assert!(usage == expected)
1034+
assert_eq!(usage, expected)
10301035
}
10311036

10321037
#[test]
@@ -1148,7 +1153,7 @@ fn test_opt_get() {
11481153
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
11491154
opts.long_only(false);
11501155

1151-
let args: Vec<String> = ["-i", "true", "-p", "1.1"]
1156+
let args: Vec<String> = ["--ignore=true", "-p1.1"]
11521157
.iter()
11531158
.map(|x| x.to_string())
11541159
.collect();
@@ -1173,7 +1178,7 @@ fn test_opt_get_default() {
11731178
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
11741179
opts.long_only(false);
11751180

1176-
let args: Vec<String> = ["-i", "true", "-p", "1.1"]
1181+
let args: Vec<String> = ["--ignore=true", "-p1.1"]
11771182
.iter()
11781183
.map(|x| x.to_string())
11791184
.collect();

0 commit comments

Comments
 (0)