add newcliapp
This commit is contained in:
10
src/Carto.toml_template
Normal file
10
src/Carto.toml_template
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "{{NAME}}"
|
||||||
|
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"
|
||||||
@@ -16,7 +16,8 @@ impl CommandDefault {
|
|||||||
|
|
||||||
pub fn run(arg_matches: &ArgMatches) -> CommandError {
|
pub fn run(arg_matches: &ArgMatches) -> CommandError {
|
||||||
let verbose_count = arg_matches.occurrences_of("verbose");
|
let verbose_count = arg_matches.occurrences_of("verbose");
|
||||||
println!("Verbose count: {}", verbose_count);
|
println!("[INFO] Verbose count: {}", verbose_count);
|
||||||
|
println!("[INFO] This is default command cli ...");
|
||||||
// TODO ...
|
// TODO ...
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/cmd_newcliapp.rs
Normal file
49
src/cmd_newcliapp.rs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
use std::fs::{ self, File, };
|
||||||
|
use clap::{ Arg, ArgMatches, SubCommand, App, };
|
||||||
|
use crate::cmd::{ Command, CommandError, };
|
||||||
|
|
||||||
|
pub struct CommandNewCliApp;
|
||||||
|
|
||||||
|
const CARGO_TOML_FILE: &str = "Carto.toml";
|
||||||
|
|
||||||
|
const MAIN_RS: &str = include_str!("main.rs_template");
|
||||||
|
const CMD_RS: &str = include_str!("cmd.rs");
|
||||||
|
const CMD_DEFAULT_RS: &str = include_str!("cmd_default.rs");
|
||||||
|
const CMD_SAMPLE_RS: &str = include_str!("cmd_sample.rs");
|
||||||
|
const CARGO_TOML: &str = include_str!("Carto.toml_template");
|
||||||
|
|
||||||
|
impl Command for CommandNewCliApp {
|
||||||
|
|
||||||
|
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||||
|
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("verbose").long("verbose").short("V").help("Verbose"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"newcliapp"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
|
||||||
|
if is_file_exists(CARGO_TOML_FILE) || is_file_exists("src") {
|
||||||
|
println!("[ERROR] File exists: {}, or dir exists: src/", CARGO_TOML_FILE);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let app_name = sub_arg_matches.value_of("APP_NAME").expect("APP_NAME is required!");
|
||||||
|
let cargo_toml = CARGO_TOML.replace("{{NAME}}", app_name);
|
||||||
|
|
||||||
|
fs::create_dir("src/")?;
|
||||||
|
fs::write("src/main.rs", MAIN_RS.as_bytes())?;
|
||||||
|
fs::write("src/cmd.rs", CMD_RS.as_bytes())?;
|
||||||
|
fs::write("src/cmd_default.rs", CMD_DEFAULT_RS.as_bytes())?;
|
||||||
|
fs::write("src/cmd_sample.rs", CMD_SAMPLE_RS.as_bytes())?;
|
||||||
|
fs::write(CARGO_TOML_FILE, cargo_toml.as_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_file_exists(file_name: &str) -> bool {
|
||||||
|
File::open(file_name).is_ok()
|
||||||
|
}
|
||||||
@@ -3,14 +3,17 @@ use clap::App;
|
|||||||
mod cmd;
|
mod cmd;
|
||||||
mod cmd_sample;
|
mod cmd_sample;
|
||||||
mod cmd_default;
|
mod cmd_default;
|
||||||
|
mod cmd_newcliapp;
|
||||||
|
|
||||||
use cmd::{ Command, CommandError, };
|
use cmd::{ Command, CommandError, };
|
||||||
use cmd_sample::CommandSample;
|
use cmd_sample::CommandSample;
|
||||||
use cmd_default::CommandDefault;
|
use cmd_default::CommandDefault;
|
||||||
|
use cmd_newcliapp::CommandNewCliApp;
|
||||||
|
|
||||||
fn main() -> CommandError {
|
fn main() -> CommandError {
|
||||||
let commands = vec![
|
let commands: Vec<Box<dyn Command>> = vec![
|
||||||
CommandSample{},
|
Box::new(CommandSample{}),
|
||||||
|
Box::new(CommandNewCliApp{}),
|
||||||
];
|
];
|
||||||
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"))
|
||||||
|
|||||||
31
src/main.rs_template
Normal file
31
src/main.rs_template
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user