From 797d75bf71d1e54509c5342715135a33e638b7ff Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 3 Jan 2021 11:02:41 +0800 Subject: [PATCH] feat: opt use_clap --- Cargo.toml | 2 +- src/util_clap.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7021b53..d62ab5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.26" +version = "0.6.27" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_clap.rs b/src/util_clap.rs index eb41bcb..c61b675 100644 --- a/src/util_clap.rs +++ b/src/util_clap.rs @@ -16,7 +16,7 @@ pub trait DefaultCommand { fn run(&self, arg_matches: &ArgMatches) -> CommandError; } -pub struct DefaultCommandImpl {} +pub struct DefaultCommandImpl; impl DefaultCommand for DefaultCommandImpl { fn process_command<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a> { app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info")) @@ -36,6 +36,10 @@ pub struct CommandExecutor { } impl CommandExecutor { + pub fn new_default() -> Self { + Self::new(None) + } + pub fn new(default_cmd: Option>) -> Self { CommandExecutor{ default_cmd, @@ -48,6 +52,13 @@ impl CommandExecutor { self } + pub fn add_commands(&mut self, cmds: Vec>) -> &mut Self { + for cmd in cmds.into_iter() { + self.add(cmd); + } + self + } + pub fn run(&self) -> XResult<()> { let mut app = App::new(env!("CARGO_PKG_NAME")) .version(env!("CARGO_PKG_VERSION"))