Skip to content

Commit

Permalink
Add a test for positional escape sequence -- (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
parasyte authored Feb 19, 2024
1 parent bc9ee3f commit aa62e50
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions onlyargs_derive/tests/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,55 @@ fn test_required_positional() -> Result<(), CliError> {

Ok(())
}

#[test]
fn test_positional_escape() -> Result<(), CliError> {
#[derive(Debug, OnlyArgs)]
struct Args {
opt_str: Option<String>,

#[positional]
rest: Vec<String>,
}

// All args are optional.
let args = Args::parse(vec![])?;

assert_eq!(args.opt_str, None);
assert!(args.rest.is_empty());

// Captures positional args.
let args = Args::parse(
["Alice", "--name", "Bob"]
.into_iter()
.map(OsString::from)
.collect(),
)?;

assert_eq!(args.opt_str, None);
assert_eq!(args.rest, ["Alice", "--name", "Bob"]);

// Captures the optional string anywhere...
let args = Args::parse(
["Alice", "--opt-str", "--name", "Bob"]
.into_iter()
.map(OsString::from)
.collect(),
)?;

assert_eq!(args.opt_str, Some("--name".to_string()));
assert_eq!(args.rest, ["Alice", "Bob"]);

// ... Unless the `--` escape sequence is encountered.
let args = Args::parse(
["Alice", "--", "--opt-str", "--name", "Bob"]
.into_iter()
.map(OsString::from)
.collect(),
)?;

assert_eq!(args.opt_str, None);
assert_eq!(args.rest, ["Alice", "--opt-str", "--name", "Bob"]);

Ok(())
}

0 comments on commit aa62e50

Please sign in to comment.