Skip to content

Commit baeaab6

Browse files
authored
Merge pull request #82 from jridgewell/gnu-optional
Parse optional short options like gnu getopt
2 parents f457f9a + 96ff776 commit baeaab6

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

src/lib.rs

Lines changed: 8 additions & 15 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) {
@@ -382,7 +382,6 @@ impl Options {
382382
} else {
383383
let mut names;
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..]
@@ -396,7 +395,6 @@ impl Options {
396395
i_arg = Some(rest.to_string());
397396
}
398397
} else {
399-
was_long = false;
400398
names = Vec::new();
401399
for (j, ch) in cur.char_indices().skip(1) {
402400
let opt = Short(ch);
@@ -444,21 +442,16 @@ impl Options {
444442
vals[optid].push((arg_pos, Given));
445443
}
446444
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.
445+
// Note that here we do not handle `--arg value` or
446+
// `-a value`. This matches GNU getopt behavior; but
447+
// also makes sense, because if this were accepted,
448+
// then users could only write a "Maybe" option at
449+
// the end of the arguments when FloatingFrees is in
450+
// use.
453451
if let Some(i_arg) = i_arg.take() {
454452
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));
460453
} else {
461-
vals[optid].push((arg_pos, Val(args.next().unwrap())));
454+
vals[optid].push((arg_pos, Given));
462455
}
463456
}
464457
Yes => {

src/tests/mod.rs

Lines changed: 4 additions & 4 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
}
@@ -1148,7 +1148,7 @@ fn test_opt_get() {
11481148
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
11491149
opts.long_only(false);
11501150

1151-
let args: Vec<String> = ["-i", "true", "-p", "1.1"]
1151+
let args: Vec<String> = ["--ignore=true", "-p1.1"]
11521152
.iter()
11531153
.map(|x| x.to_string())
11541154
.collect();
@@ -1173,7 +1173,7 @@ fn test_opt_get_default() {
11731173
opts.optflagopt("p", "percent", "Description", "0.0 .. 10.0");
11741174
opts.long_only(false);
11751175

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

0 commit comments

Comments
 (0)