ref cmd format

This commit is contained in:
2020-06-14 00:28:38 +08:00
parent 0b33a5cfeb
commit 02cb88ceb5
3 changed files with 6 additions and 10 deletions

View File

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

View File

@@ -14,16 +14,14 @@ pub struct CommandNewCliApp;
impl Command for CommandNewCliApp { impl Command for CommandNewCliApp {
fn name(&self) -> &str { "newcliapp" }
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("New cli app") SubCommand::with_name(self.name()).about("New cli app")
.arg(Arg::with_name("APP_NAME").required(true).index(1).help("App name")) .arg(Arg::with_name("APP_NAME").required(true).index(1).help("App name"))
.arg(Arg::with_name("verbose").long("verbose").short("V").help("Verbose")) .arg(Arg::with_name("verbose").long("verbose").short("V").help("Verbose"))
} }
fn name(&self) -> &str {
"newcliapp"
}
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let is_verbose = sub_arg_matches.is_present("verbose"); let is_verbose = sub_arg_matches.is_present("verbose");

View File

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