use clap::{error::ErrorKind, Arg, ArgAction, Command}; use snapbox::assert_data_eq; use snapbox::str; #[test] fn option_long() { let m = Command::new("multiple_values") .arg( Arg::new("option") .long("option") .help("multiple options") .action(ArgAction::Set) .num_args(1..) .action(ArgAction::Append), ) .try_get_matches_from(vec![ "", "--option", "val1", "--option", "val2", "--option", "val3", ]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3"] ); } #[test] fn option_short() { let m = Command::new("multiple_values") .arg( Arg::new("option") .short('o') .help("multiple options") .action(ArgAction::Set) .num_args(1..) .action(ArgAction::Append), ) .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3"] ); } #[test] fn option_mixed() { let m = Command::new("multiple_values") .arg( Arg::new("option") .long("option") .short('o') .help("multiple options") .action(ArgAction::Set) .num_args(1..) .action(ArgAction::Append), ) .try_get_matches_from(vec![ "", "-o", "val1", "--option", "val2", "--option", "val3", "-o", "val4", ]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3", "val4"] ); } #[test] fn option_exact_exact() { let m = Command::new("multiple_values") .arg( Arg::new("option") .short('o') .help("multiple options") .num_args(3) .action(ArgAction::Append), ) .try_get_matches_from(vec![ "", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6", ]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3", "val4", "val5", "val6"] ); } #[test] fn option_exact_exact_not_mult() { let m = Command::new("multiple_values") .arg( Arg::new("option") .short('o') .help("multiple options") .num_args(3), ) .try_get_matches_from(vec!["", "-o", "val1", "val2", "val3"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3"] ); } #[test] fn option_exact_exact_mult() { let m = Command::new("multiple_values") .arg( Arg::new("option") .short('o') .help("multiple options") .num_args(3) .action(ArgAction::Append), ) .try_get_matches_from(vec![ "", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6", ]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_many::("option") .unwrap() .map(|v| v.as_str()) .collect::>(), ["val1", "val2", "val3", "val4", "val5", "val6"] ); } #[test] fn option_exact_less() { let m = Command::new("multiple_values") .arg( Arg::new("option") .short('o') .help("multiple options") .num_args(3) .action(ArgAction::Append), ) .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]); assert!(m.is_err()); let err = m.unwrap_err(); assert_eq!(err.kind(), ErrorKind::WrongNumberOfValues); #[cfg(feature = "error-context")] assert_data_eq!(err.to_string(), str![[r#" error: 3 values required for '-o