rename CmdDef to CommandLineInterface
This commit is contained in:
parent
b7329c8a1a
commit
9f6ab1fc58
|
@ -5,29 +5,29 @@ use std::collections::HashMap;
|
||||||
use apitest::api3;
|
use apitest::api3;
|
||||||
use apitest::cli::command::*;
|
use apitest::cli::command::*;
|
||||||
|
|
||||||
fn datastore_commands() -> CmdDef {
|
fn datastore_commands() -> CommandLineInterface {
|
||||||
|
|
||||||
let mut cmd_def = HashMap::new();
|
let mut cmd_def = HashMap::new();
|
||||||
|
|
||||||
cmd_def.insert("list".to_owned(), CmdDef::Simple(CliCommand {
|
cmd_def.insert("list".to_owned(), CommandLineInterface::Simple(CliCommand {
|
||||||
info: api3::config::datastore::get(),
|
info: api3::config::datastore::get(),
|
||||||
arg_param: vec![],
|
arg_param: vec![],
|
||||||
fixed_param: vec![],
|
fixed_param: vec![],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
cmd_def.insert("create".to_owned(), CmdDef::Simple(CliCommand {
|
cmd_def.insert("create".to_owned(), CommandLineInterface::Simple(CliCommand {
|
||||||
info: api3::config::datastore::post(),
|
info: api3::config::datastore::post(),
|
||||||
arg_param: vec!["name", "path"],
|
arg_param: vec!["name", "path"],
|
||||||
fixed_param: vec![],
|
fixed_param: vec![],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
cmd_def.insert("remove".to_owned(), CmdDef::Simple(CliCommand {
|
cmd_def.insert("remove".to_owned(), CommandLineInterface::Simple(CliCommand {
|
||||||
info: api3::config::datastore::delete(),
|
info: api3::config::datastore::delete(),
|
||||||
arg_param: vec!["name"],
|
arg_param: vec!["name"],
|
||||||
fixed_param: vec![],
|
fixed_param: vec![],
|
||||||
}));
|
}));
|
||||||
|
|
||||||
CmdDef::Nested(cmd_def)
|
CommandLineInterface::Nested(cmd_def)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -36,7 +36,7 @@ fn main() {
|
||||||
|
|
||||||
cmd_def.insert("datastore".to_owned(), datastore_commands());
|
cmd_def.insert("datastore".to_owned(), datastore_commands());
|
||||||
|
|
||||||
if let Err(err) = run_cli_command(&CmdDef::Nested(cmd_def)) {
|
if let Err(err) = run_cli_command(&CommandLineInterface::Nested(cmd_def)) {
|
||||||
eprintln!("Error: {}", err);
|
eprintln!("Error: {}", err);
|
||||||
print_cli_usage();
|
print_cli_usage();
|
||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
|
|
|
@ -27,7 +27,7 @@ fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<(),
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_nested_command(def: &HashMap<String, CmdDef>, mut args: Vec<String>) -> Result<(), Error> {
|
fn handle_nested_command(def: &HashMap<String, CommandLineInterface>, mut args: Vec<String>) -> Result<(), Error> {
|
||||||
|
|
||||||
if args.len() < 1 {
|
if args.len() < 1 {
|
||||||
let mut cmds: Vec<&String> = def.keys().collect();
|
let mut cmds: Vec<&String> = def.keys().collect();
|
||||||
|
@ -52,10 +52,10 @@ fn handle_nested_command(def: &HashMap<String, CmdDef>, mut args: Vec<String>) -
|
||||||
};
|
};
|
||||||
|
|
||||||
match sub_cmd {
|
match sub_cmd {
|
||||||
CmdDef::Simple(cli_cmd) => {
|
CommandLineInterface::Simple(cli_cmd) => {
|
||||||
handle_simple_command(cli_cmd, args)?;
|
handle_simple_command(cli_cmd, args)?;
|
||||||
}
|
}
|
||||||
CmdDef::Nested(map) => {
|
CommandLineInterface::Nested(map) => {
|
||||||
handle_nested_command(map, args)?;
|
handle_nested_command(map, args)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,13 +63,13 @@ fn handle_nested_command(def: &HashMap<String, CmdDef>, mut args: Vec<String>) -
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_cli_command(def: &CmdDef) -> Result<(), Error> {
|
pub fn run_cli_command(def: &CommandLineInterface) -> Result<(), Error> {
|
||||||
|
|
||||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||||
|
|
||||||
match def {
|
match def {
|
||||||
CmdDef::Simple(cli_cmd) => handle_simple_command(cli_cmd, args),
|
CommandLineInterface::Simple(cli_cmd) => handle_simple_command(cli_cmd, args),
|
||||||
CmdDef::Nested(map) => handle_nested_command(map, args),
|
CommandLineInterface::Nested(map) => handle_nested_command(map, args),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ pub struct CliCommand {
|
||||||
pub fixed_param: Vec<&'static str>,
|
pub fixed_param: Vec<&'static str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum CmdDef {
|
pub enum CommandLineInterface {
|
||||||
Simple(CliCommand),
|
Simple(CliCommand),
|
||||||
Nested(HashMap<String, CmdDef>),
|
Nested(HashMap<String, CommandLineInterface>),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue