init commit

This commit is contained in:
2020-06-13 23:49:42 +08:00
parent 9358c53d28
commit e7503834e7
5 changed files with 96 additions and 0 deletions

10
Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "buildrs"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.33.1"

12
src/cmd.rs Normal file
View File

@@ -0,0 +1,12 @@
use clap::{ ArgMatches, App, };
pub type CommandError = Result<(), Box<dyn std::error::Error>>;
pub trait Command {
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn name(&self) -> &str;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError;
}

23
src/cmd_default.rs Normal file
View File

@@ -0,0 +1,23 @@
use clap::{ App, Arg, ArgMatches, };
use crate::cmd::CommandError;
pub struct CommandDefault;
impl CommandDefault {
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");
println!("Verbose count: {}", verbose_count);
// TODO ...
Ok(())
}
}

20
src/cmd_sample.rs Normal file
View File

@@ -0,0 +1,20 @@
use clap::{ ArgMatches, SubCommand, App, };
use crate::cmd::{ Command, CommandError, };
pub struct CommandSample;
impl Command for CommandSample {
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Sample subcommand")
}
fn name(&self) -> &str {
"sample"
}
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {
println!("This is test command!");
Ok(())
}
}

31
src/main.rs Normal file
View File

@@ -0,0 +1,31 @@
use clap::App;
mod cmd;
mod cmd_sample;
mod cmd_default;
use cmd::{ Command, CommandError, };
use cmd_sample::CommandSample;
use cmd_default::CommandDefault;
fn main() -> CommandError {
let commands = vec![
CommandSample{},
];
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"));
app = CommandDefault::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);
}
}
CommandDefault::run(&matches)
}