Skip to content

Commit

Permalink
refactor of moving cargo wrapper handling to src/cli.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
cat-in-136 committed Jun 2, 2024
1 parent 56007e2 commit 1a226c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
26 changes: 22 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{
builder::{PathBufValueParser, PossibleValuesParser, TypedValueParser, ValueParserFactory},
Arg, Command, Parser, ValueEnum,
Arg, ArgMatches, Command, CommandFactory, FromArgMatches, Parser, ValueEnum,
};
use std::ffi::OsStr;
use std::path::PathBuf;
Expand All @@ -9,7 +9,7 @@ use std::path::PathBuf;
#[derive(Debug, Parser)]
#[command(name = "cargo")]
#[command(bin_name = "cargo")]
pub enum CargoWrapper {
enum CargoWrapper {
GenerateRpm(Cli),
}

Expand Down Expand Up @@ -88,6 +88,20 @@ pub struct Cli {
pub variant: Vec<String>,
}

impl Cli {
pub fn get_matches_and_try_parse() -> Result<(Self, ArgMatches), clap::Error> {
let mut args = std::env::args();
let mut matches = if let Some("generate-rpm") = args.nth(1).as_deref() {
<CargoWrapper as CommandFactory>::command().get_matches()
} else {
<Self as CommandFactory>::command().get_matches()
};
let arg = Self::from_arg_matches_mut(&mut matches)?;

Ok((arg, matches))
}
}

impl Default for Cli {
fn default() -> Self {
Cli::parse_from([""])
Expand Down Expand Up @@ -176,8 +190,12 @@ mod tests {
use super::*;
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert()
<Cli as CommandFactory>::command().debug_assert()
}

#[test]
fn verify_cargo_wrapper() {
<CargoWrapper as CommandFactory>::command().debug_assert()
}

#[test]
Expand Down
11 changes: 2 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{build_target::BuildTarget, config::BuilderConfig};
use clap::Parser;
use cli::{CargoWrapper, Cli};
use cli::Cli;
use std::{
fs,
path::{Path, PathBuf},
Expand Down Expand Up @@ -54,13 +53,7 @@ fn determine_output_dir(
}

fn run() -> Result<(), Error> {
let mut args = std::env::args();
let args = if let Some("generate-rpm") = args.nth(1).as_deref() {
let CargoWrapper::GenerateRpm(args) = CargoWrapper::parse();
args
} else {
Cli::parse()
};
let (args, matcher) = Cli::get_matches_and_try_parse().unwrap_or_else(|e| e.exit());

let build_target = BuildTarget::new(&args);
let extra_metadata = collect_metadata(&args);
Expand Down

0 comments on commit 1a226c9

Please sign in to comment.