use clap::{builder::PossibleValue, error::ErrorKind, Arg, ArgAction, Command}; #[cfg(feature = "error-context")] use super::utils; #[test] fn possible_values_of_positional() { let m = Command::new("possible_values") .arg(Arg::new("positional").index(1).value_parser(["test123"])) .try_get_matches_from(vec!["myprog", "test123"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("positional")); assert_eq!( m.get_one::("positional").map(|v| v.as_str()), Some("test123") ); } #[test] fn possible_value_arg_value() { let m = Command::new("possible_values") .arg( Arg::new("arg_value") .index(1) .value_parser([PossibleValue::new("test123") .hide(false) .help("It's just a test")]), ) .try_get_matches_from(vec!["myprog", "test123"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("arg_value")); assert_eq!( m.get_one::("arg_value").map(|v| v.as_str()), Some("test123") ); } #[test] fn possible_values_of_positional_fail() { let m = Command::new("possible_values") .arg(Arg::new("positional").index(1).value_parser(["test123"])) .try_get_matches_from(vec!["myprog", "notest"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); } #[test] fn possible_values_of_positional_multiple() { let m = Command::new("possible_values") .arg( Arg::new("positional") .index(1) .action(ArgAction::Set) .value_parser(["test123", "test321"]) .num_args(1..), ) .try_get_matches_from(vec!["myprog", "test123", "test321"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("positional")); assert_eq!( m.get_many::("positional") .unwrap() .map(|v| v.as_str()) .collect::>(), vec!["test123", "test321"] ); } #[test] fn possible_values_of_positional_multiple_fail() { let m = Command::new("possible_values") .arg( Arg::new("positional") .index(1) .action(ArgAction::Set) .value_parser(["test123", "test321"]) .num_args(1..), ) .try_get_matches_from(vec!["myprog", "test123", "notest"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); } #[test] fn possible_values_of_option() { let m = Command::new("possible_values") .arg( Arg::new("option") .short('o') .long("option") .action(ArgAction::Set) .value_parser(["test123"]), ) .try_get_matches_from(vec!["myprog", "--option", "test123"]); assert!(m.is_ok(), "{}", m.unwrap_err()); let m = m.unwrap(); assert!(m.contains_id("option")); assert_eq!( m.get_one::("option").map(|v| v.as_str()), Some("test123") ); } #[test] fn possible_values_of_option_fail() { let m = Command::new("possible_values") .arg( Arg::new("option") .short('o') .long("option") .action(ArgAction::Set) .value_parser(["test123"]), ) .try_get_matches_from(vec!["myprog", "--option", "notest"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); } #[test] fn possible_values_of_option_multiple() { let m = Command::new("possible_values") .arg( Arg::new("option") .short('o') .long("option") .action(ArgAction::Set) .value_parser(["test123", "test321"]) .action(ArgAction::Append), ) .try_get_matches_from(vec!["", "--option", "test123", "--option", "test321"]); 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::>(), vec!["test123", "test321"] ); } #[test] fn possible_values_of_option_multiple_fail() { let m = Command::new("possible_values") .arg( Arg::new("option") .short('o') .long("option") .action(ArgAction::Set) .value_parser(["test123", "test321"]) .action(ArgAction::Append), ) .try_get_matches_from(vec!["", "--option", "test123", "--option", "notest"]); assert!(m.is_err()); assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); } #[test] #[cfg(feature = "error-context")] fn possible_values_output() { #[cfg(feature = "suggestions")] static PV_ERROR: &str = "\ error: invalid value 'slo' for '-O