1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-13 15:50:05 +08:00

Compare commits

..

2 Commits

Author SHA1 Message Date
da71d5c0c2 feat: add test 2021-01-03 11:05:22 +08:00
797d75bf71 feat: opt use_clap 2021-01-03 11:02:41 +08:00
3 changed files with 17 additions and 2 deletions

View File

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

View File

@@ -9,4 +9,8 @@ log:
publish: publish:
cargo publish cargo publish
# test all
test:
cargo test
cd demo/test_clap; cargo build; cargo test

View File

@@ -16,7 +16,7 @@ pub trait DefaultCommand {
fn run(&self, arg_matches: &ArgMatches) -> CommandError; fn run(&self, arg_matches: &ArgMatches) -> CommandError;
} }
pub struct DefaultCommandImpl {} pub struct DefaultCommandImpl;
impl DefaultCommand for DefaultCommandImpl { impl DefaultCommand for DefaultCommandImpl {
fn process_command<'a>(&self, app: App<'a, 'a>) -> App<'a, 'a> { 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")) 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 { impl CommandExecutor {
pub fn new_default() -> Self {
Self::new(None)
}
pub fn new(default_cmd: Option<Box<dyn DefaultCommand>>) -> Self { pub fn new(default_cmd: Option<Box<dyn DefaultCommand>>) -> Self {
CommandExecutor{ CommandExecutor{
default_cmd, default_cmd,
@@ -48,6 +52,13 @@ impl CommandExecutor {
self 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<()> { pub fn run(&self) -> XResult<()> {
let mut app = App::new(env!("CARGO_PKG_NAME")) let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))