Skip to content

Commit

Permalink
fix(from_usage): removes bug where usage strings have no help text
Browse files Browse the repository at this point in the history
Creating arguments from usage strings with no help text previously
dropped the final character i.e. --flags -> --flag
This commit fixes that issue by testing if we are at the end of the
string

Closes #83
  • Loading branch information
kbknapp committed Apr 29, 2015
1 parent 06304bf commit ad4e545
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ mod tests {
assert_eq!(e.short.unwrap(), 'f');
assert_eq!(e.help.unwrap(), "some help info");
assert!(e.multiple);

let e = Arg::from_usage("--flags");
assert_eq!(e.name, "flags");
assert_eq!(e.long.unwrap(), "flags");

let e = Arg::from_usage("--flags...");
assert_eq!(e.name, "flags");
assert_eq!(e.long.unwrap(), "flags");
assert!(e.multiple);

let e = Arg::from_usage("[flags] -f");
assert_eq!(e.name, "flags");
assert_eq!(e.short.unwrap(), 'f');

let e = Arg::from_usage("[flags] -f...");
assert_eq!(e.name, "flags");
assert_eq!(e.short.unwrap(), 'f');
assert!(e.multiple);
}

#[test]
Expand Down Expand Up @@ -125,6 +143,16 @@ mod tests {
assert_eq!(d.help.unwrap(), "some help info");
assert!(d.multiple);
assert!(d.required);

let b = Arg::from_usage("<pos>");
assert_eq!(b.name, "pos");
assert!(!b.multiple);
assert!(b.required);

let c = Arg::from_usage("[pos]...");
assert_eq!(c.name, "pos");
assert!(c.multiple);
assert!(!c.required);
}

#[test]
Expand Down
11 changes: 10 additions & 1 deletion src/usageparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ impl<'u> Iterator for UsageParser<'u> {
}
if self.e > self.usage.len() { return None }

if self.e == self.usage.len() - 1 {
return Some(UsageToken::Long(&self.usage[self.s..]))
}
return Some(UsageToken::Long(&self.usage[self.s..self.e]))
},
Some(c) => {
Expand All @@ -108,7 +111,13 @@ impl<'u> Iterator for UsageParser<'u> {
match self.chars.next() {
// longs consume one '.' so they match '.. ' whereas shorts can match '...'
Some('.') | Some(' ') => { mult = true; },
_ => { mult = false; break; }
_ => {
// if there is no help or following space all we can match is '..'
if self.e == self.usage.len() - 1 {
mult = true;
}
break;
}
}
}
if mult { return Some(UsageToken::Multiple) }
Expand Down

0 comments on commit ad4e545

Please sign in to comment.