-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rm): use clap builder instead of derive
- Loading branch information
1 parent
cdf1214
commit 4372325
Showing
5 changed files
with
150 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,34 @@ | ||
use cargo::util::command_prelude::*; | ||
use cargo::CargoResult; | ||
use clap::{Parser, Subcommand}; | ||
use clap::Command; | ||
|
||
pub fn main(config: &mut Config) -> CliResult { | ||
let args = Cli::try_parse()?; | ||
execute_subcommand(config, &args)?; | ||
let args = cli().try_get_matches()?; | ||
let (cmd, subcommand_args) = args.subcommand().expect("subcommand_required(true)"); | ||
execute_subcommand(config, cmd, subcommand_args)?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
#[clap(bin_name = "cargo")] | ||
pub struct Cli { | ||
/// Unstable (nightly-only) flags | ||
#[clap(short = 'Z', value_name = "FLAG", global = true, arg_enum)] | ||
unstable_features: Vec<UnstableOptions>, | ||
|
||
#[clap(subcommand)] | ||
subcommand: Command, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum Command { | ||
Rm(crate::commands::rm::RmArgs), | ||
fn cli() -> Command<'static> { | ||
Command::new("cargo") | ||
.bin_name("cargo") | ||
.arg( | ||
Arg::new("unstable-features") | ||
.help("Unstable (nightly-only) flags") | ||
.short('Z') | ||
.value_name("FLAG") | ||
.action(ArgAction::Append) | ||
.global(true), | ||
) | ||
.subcommands(crate::commands::builtin()) | ||
.subcommand_required(true) | ||
} | ||
|
||
impl Command { | ||
pub fn exec(&self) -> CargoResult<()> { | ||
match self { | ||
Self::Rm(rm) => rm.exec(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ArgEnum)] | ||
enum UnstableOptions {} | ||
|
||
fn execute_subcommand(_config: &mut Config, args: &Cli) -> CliResult { | ||
args.subcommand.exec()?; | ||
Ok(()) | ||
fn execute_subcommand(config: &mut Config, cmd: &str, subcommand_args: &ArgMatches) -> CliResult { | ||
let exec = crate::commands::builtin_exec(cmd).expect("all of `builtin` supported"); | ||
exec(config, subcommand_args) | ||
} | ||
|
||
#[test] | ||
fn verify_app() { | ||
use clap::CommandFactory; | ||
Cli::command().debug_assert() | ||
cli().debug_assert() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
use cargo::CliResult; | ||
|
||
pub mod rm; | ||
|
||
pub fn builtin() -> [clap::Command<'static>; 1] { | ||
[rm::cli()] | ||
} | ||
|
||
pub fn builtin_exec(cmd: &str) -> Option<fn(&mut cargo::Config, &clap::ArgMatches) -> CliResult> { | ||
let f = match cmd { | ||
"rm" => rm::exec, | ||
_ => return None, | ||
}; | ||
Some(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters