Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deriving grouped options for Vec<Vec<T>> #4600

Merged
merged 3 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 20 additions & 12 deletions clap_derive/src/utils/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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<Ty> {
subty_if_name(ty, "Vec").map(|subty| {
if is_generic_ty(subty, "Vec") {
vecvec_ty
} else {
vec_ty
}
})
}

#[cfg(not(feature = "unstable-v5"))]
epage marked this conversation as resolved.
Show resolved Hide resolved
fn get_vec_ty(ty: &Type, vec_ty: Ty, _vecvec_ty: Ty) -> Option<Ty> {
is_generic_ty(ty, "Vec").then(|| vec_ty)
}
epage marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 5 additions & 7 deletions tests/derive/occurrences.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "unstable-grouped")]
#![cfg(all(feature = "unstable-grouped", feature = "unstable-v5"))]
use clap::Parser;

#[test]
Expand All @@ -19,23 +19,21 @@ fn test_vec_of_vec() {

#[test]
fn test_vec_of_vec_opt_out() {
fn parser(s: &str) -> Result<Vec<Vec<String>>, std::convert::Infallible> {
Ok(s.split(':')
.map(|v| v.split(',').map(str::to_owned).collect())
.collect())
fn parser(s: &str) -> Result<Vec<String>, 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<Vec<String>>,
arg: Vec<::std::vec::Vec<String>>,
}

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(),
);
}

Expand Down