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

Fixup cli to handle parsing matches when called as a cargo subcommand #119

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 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.
// 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 = <Self as CommandFactory>::command().get_matches_from(args);
Expand Down Expand Up @@ -257,10 +267,18 @@ mod tests {
&[2]
);

// Simulate being called from Cargo
let (args, matcher) = Cli::get_matches_and_try_parse_from(|| {
["generate-rpm", "-o", "/dev/null"]
.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")));
Expand Down
Loading