1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 07:30:05 +08:00

feat: opt use_clap

This commit is contained in:
2021-01-03 11:02:41 +08:00
parent 5df135fda9
commit 797d75bf71
2 changed files with 13 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "rust_util"
version = "0.6.26"
version = "0.6.27"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "Hatter's Rust Util"

View File

@@ -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<Box<dyn DefaultCommand>>) -> Self {
CommandExecutor{
default_cmd,
@@ -48,6 +52,13 @@ impl CommandExecutor {
self
}
pub fn add_commands(&mut self, cmds: Vec<Box<dyn Command>>) -> &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"))