feat: use rust_util use_clap

This commit is contained in:
2021-01-03 10:58:29 +08:00
parent 8b70dfa145
commit 740d5baf65
11 changed files with 44 additions and 71 deletions

View File

@@ -7,8 +7,6 @@ mod credit_util;
mod engine;
mod engine_plugin_credit;
mod cmd;
mod cmd_default;
mod cmd_genkey;
mod cmd_credit_create;
mod cmd_credit_issue;
@@ -16,30 +14,32 @@ mod cmd_credit_transfer;
mod cmd_credit_query;
mod cmd_credit_queryall;
use clap::App;
use cmd::{Command, CommandError};
use clap::{App, Arg, ArgMatches};
use rust_util::{XResult, util_clap::{CommandError, CommandExecutor, DefaultCommand}};
fn main() -> CommandError {
let commands: Vec<Box<dyn Command>> = vec![
Box::new(cmd_genkey::CommandImpl),
Box::new(cmd_credit_create::CommandImpl),
Box::new(cmd_credit_issue::CommandImpl),
Box::new(cmd_credit_transfer::CommandImpl),
Box::new(cmd_credit_query::CommandImpl),
Box::new(cmd_credit_queryall::CommandImpl),
];
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"));
app = cmd_default::CommandImpl::process_command(app);
for command in &commands {
app = app.subcommand(command.subcommand());
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"))
}
let matches = app.get_matches();
for command in &commands {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
return command.run(&matches, sub_cmd_matches);
}
fn run(&self, arg_matches: &ArgMatches) -> CommandError {
let verbose_count = arg_matches.occurrences_of("verbose");
information!("Verbose count: {}", verbose_count);
information!("Try use help (--help) print usage");
Ok(None)
}
cmd_default::CommandImpl::run(&matches)
}
fn main() -> XResult<()> {
let mut c = CommandExecutor::new(Some(Box::new(DefaultCommandImpl)));
c.add(Box::new(cmd_genkey::CommandImpl));
c.add(Box::new(cmd_credit_create::CommandImpl));
c.add(Box::new(cmd_credit_issue::CommandImpl));
c.add(Box::new(cmd_credit_transfer::CommandImpl));
c.add(Box::new(cmd_credit_query::CommandImpl));
c.add(Box::new(cmd_credit_queryall::CommandImpl));
c.run()?;
Ok(())
}