From f8ddff6bc0e60c06334f680ad30a8c5662078564 Mon Sep 17 00:00:00 2001 From: david steele Date: Tue, 16 Jul 2024 09:36:23 +0100 Subject: [PATCH 1/2] Fixup cli to handle parsing matches when called as a cargo subcommand --- src/cli.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index a664ad0..79fe77a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -101,10 +101,20 @@ impl Cli { let mut args = args_fn(); if args.nth(1) == Some(OsString::from("generate-rpm")) { let args = args_fn(); + // This is the matches of the cargo command let matches = ::command().get_matches_from(args); let CargoWrapper::GenerateRpm(arg) = CargoWrapper::from_arg_matches_mut(&mut matches.clone())?; - Ok((arg, matches)) + // matches are the args on the "cargo" call, generate-rpm is a subcommand + // we need to get the subcommand arguments from matches and return those. + match matches.subcommand_matches("generate-rpm") { + Some(subcommand_matches) => { + Ok((arg, subcommand_matches.to_owned())) + } + None => { + Ok((arg, matches)) + } + } } else { let args = args_fn(); let matches = ::command().get_matches_from(args); @@ -257,8 +267,9 @@ mod tests { &[2] ); + // Simulate being called from Cargo let (args, matcher) = Cli::get_matches_and_try_parse_from(|| { - ["generate-rpm", "-o", "/dev/null"] + ["cargo", "generate-rpm", "-o", "/dev/null", "-s", "release=1.foo"] .map(&OsString::from) .into_iter() }) From 360cb26a5c0eea069db3b5bb409fca73eca76a02 Mon Sep 17 00:00:00 2001 From: david steele Date: Fri, 19 Jul 2024 14:02:47 +0100 Subject: [PATCH 2/2] Markups --- src/cli.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 79fe77a..73119da 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -107,14 +107,14 @@ impl Cli { CargoWrapper::from_arg_matches_mut(&mut matches.clone())?; // matches are the args on the "cargo" call, generate-rpm is a subcommand // we need to get the subcommand arguments from matches and return those. - match matches.subcommand_matches("generate-rpm") { - Some(subcommand_matches) => { - Ok((arg, subcommand_matches.to_owned())) - } - None => { - Ok((arg, matches)) - } - } + // It's acceptable to unwrap here because we know that the subcommand is present based on the check above. + Ok(( + arg, + matches + .subcommand_matches("generate-rpm") + .unwrap() + .to_owned(), + )) } else { let args = args_fn(); let matches = ::command().get_matches_from(args); @@ -269,9 +269,16 @@ mod tests { // Simulate being called from Cargo let (args, matcher) = Cli::get_matches_and_try_parse_from(|| { - ["cargo", "generate-rpm", "-o", "/dev/null", "-s", "release=1.foo"] - .map(&OsString::from) - .into_iter() + [ + "cargo", + "generate-rpm", + "-o", + "/dev/null", + "-s", + "release=1.foo", + ] + .map(&OsString::from) + .into_iter() }) .unwrap(); assert_eq!(args.output, Some(PathBuf::from("/dev/null")));