diff --git a/clap_derive/src/utils/ty.rs b/clap_derive/src/utils/ty.rs index 32bc2c4fc98..a87ab16962f 100644 --- a/clap_derive/src/utils/ty.rs +++ b/clap_derive/src/utils/ty.rs @@ -26,21 +26,13 @@ impl Ty { if is_unit_ty(ty) { t(Unit) - } else if let Some(subty) = subty_if_name(ty, "Vec") { - if is_generic_ty(subty, "Vec") { - t(VecVec) - } else { - t(Vec) - } + } else if let Some(vt) = get_vec_ty(ty, Vec, VecVec) { + t(vt) } else if let Some(subty) = subty_if_name(ty, "Option") { if is_generic_ty(subty, "Option") { t(OptionOption) - } else if let Some(subty) = subty_if_name(subty, "Vec") { - if is_generic_ty(subty, "Vec") { - t(OptionVecVec) - } else { - t(OptionVec) - } + } else if let Some(vt) = get_vec_ty(subty, OptionVec, OptionVecVec) { + t(vt) } else { t(Option) } @@ -155,3 +147,19 @@ where { iter.next().filter(|_| iter.next().is_none()) } + +#[cfg(feature = "unstable-v5")] +fn get_vec_ty(ty: &Type, vec_ty: Ty, vecvec_ty: Ty) -> Option { + subty_if_name(ty, "Vec").map(|subty| { + if is_generic_ty(subty, "Vec") { + vecvec_ty + } else { + vec_ty + } + }) +} + +#[cfg(not(feature = "unstable-v5"))] +fn get_vec_ty(ty: &Type, vec_ty: Ty, _vecvec_ty: Ty) -> Option { + is_generic_ty(ty, "Vec").then(|| vec_ty) +} diff --git a/tests/derive/occurrences.rs b/tests/derive/occurrences.rs index c8cbb9e37ab..db4ae37eb03 100644 --- a/tests/derive/occurrences.rs +++ b/tests/derive/occurrences.rs @@ -1,4 +1,4 @@ -#![cfg(feature = "unstable-grouped")] +#![cfg(all(feature = "unstable-grouped", feature = "unstable-v5"))] use clap::Parser; #[test] @@ -19,23 +19,21 @@ fn test_vec_of_vec() { #[test] fn test_vec_of_vec_opt_out() { - fn parser(s: &str) -> Result>, std::convert::Infallible> { - Ok(s.split(':') - .map(|v| v.split(',').map(str::to_owned).collect()) - .collect()) + fn parser(s: &str) -> Result, std::convert::Infallible> { + Ok(s.split(',').map(str::to_owned).collect()) } #[derive(Parser, PartialEq, Debug)] struct Opt { #[arg(value_parser = parser, short = 'p')] - arg: ::std::vec::Vec>, + arg: Vec<::std::vec::Vec>, } assert_eq!( Opt { arg: vec![vec!["1".into(), "2".into()], vec!["a".into(), "b".into()]], }, - Opt::try_parse_from(["test", "-p", "1,2:a,b"]).unwrap(), + Opt::try_parse_from(["test", "-p", "1,2", "-p", "a,b"]).unwrap(), ); }