Skip to content

Commit

Permalink
Fixup cli to handle parsing matches when called as a cargo subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dsteeley committed Jul 16, 2024
1 parent dd6fa65 commit f8ddff6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <CargoWrapper as CommandFactory>::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 = <Self as CommandFactory>::command().get_matches_from(args);
Expand Down Expand Up @@ -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()
})
Expand Down

0 comments on commit f8ddff6

Please sign in to comment.