Skip to content

Commit

Permalink
Fix separator handling in argument parsing (#11)
Browse files Browse the repository at this point in the history
* Make argument parsing correctly handle = as a separator. Hopefully. Fixes #9

* Add basic tests for '=' separator

* Fix '=' separator handling
  • Loading branch information
Shnatsel authored Feb 25, 2022
1 parent b15d8dc commit da7d1ad
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ impl Subcommand {
let mut examples = false;
let mut bins = false;
let mut quiet = false;
while let Some(name) = args.next() {
let value = if let Some(value) = args.peek() {
while let Some(mut name) = args.next() {
let value = if let Some(position) = name.as_str().find('=') {
name.remove(position); // drop the '=' sign so we can cleanly split the string in two
Some(name.split_off(position))
} else if let Some(value) = args.peek() {
if !value.starts_with("-") {
args.next()
} else {
Expand Down Expand Up @@ -185,3 +188,22 @@ impl Subcommand {
self.quiet
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_separator_space() {
let args = ["cargo", "subcommand", "build", "--target", "x86_64-unknown-linux-gnu"].iter().map(|s| s.to_string());
let cmd = Subcommand::new(args, "subcommand", |_, _| Ok(false)).unwrap();
assert_eq!(cmd.target(), Some("x86_64-unknown-linux-gnu"));
}

#[test]
fn test_separator_equals() {
let args = ["cargo", "subcommand", "build", "--target=x86_64-unknown-linux-gnu"].iter().map(|s| s.to_string());
let cmd = Subcommand::new(args, "subcommand", |_, _| Ok(false)).unwrap();
assert_eq!(cmd.target(), Some("x86_64-unknown-linux-gnu"));
}
}

0 comments on commit da7d1ad

Please # to comment.