feat: init commit

This commit is contained in:
2020-11-28 10:18:30 +08:00
parent 88262e0132
commit 935dfa8a8e
5 changed files with 98 additions and 0 deletions

13
src/cmd.rs Normal file
View File

@@ -0,0 +1,13 @@
use clap::{ArgMatches, App};
use rust_util::XResult;
pub type CommandError = XResult<()>;
pub trait Command {
fn name(&self) -> &str;
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError;
}

19
src/cmd_default.rs Normal file
View File

@@ -0,0 +1,19 @@
use clap::{App, Arg, ArgMatches};
use crate::cmd::CommandError;
pub struct CommandImpl;
impl CommandImpl {
pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app.arg(Arg::with_name("verbose").long("verbose").short("v").multiple(true).help("Show verbose info"))
}
pub fn run(arg_matches: &ArgMatches) -> CommandError {
let verbose_count = arg_matches.occurrences_of("verbose");
information!("Verbose count: {}", verbose_count);
information!(r#"Print using command line:
commit-msg usage"#);
Ok(())
}
}

17
src/cmd_usage.rs Normal file
View File

@@ -0,0 +1,17 @@
use clap::{ArgMatches, SubCommand, App};
use crate::cmd::{Command, CommandError};
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str { "usage" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Sample subcommand")
}
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {
Ok(())
}
}

30
src/main.rs Normal file
View File

@@ -0,0 +1,30 @@
#[macro_use] extern crate rust_util;
mod cmd;
mod cmd_default;
mod cmd_usage;
use clap::App;
use cmd::{Command, CommandError};
fn main() -> CommandError {
let commands: Vec<Box<dyn Command>> = vec![
Box::new(cmd_usage::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());
}
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);
}
}
cmd_default::CommandImpl::run(&matches)
}