use docopt::Docopt;
use std::vec;
use kinetic::KineticResult;
use std::ascii::OwnedAsciiExt;
use cli::{CliDispatcher, CliCommand};
#[derive(RustcDecodable, Debug)]
pub struct Args {
arg_command: Option<Command>,
arg_args: Vec<String>,
flag_list: bool,
flag_help: bool,
flag_version: bool,
flag_verbose: bool,
}
#[derive(RustcDecodable, Debug)]
pub enum Command {
Help,
Write,
Info,
Bench,
Log,
}
impl CliDispatcher for Command {
fn dispatch(&self, mut argv: vec::Vec<String>, shell: &mut ::shell::MultiShell) -> KineticResult<()> {
argv.insert(0, format!("{:?}", self).into_ascii_lowercase());
argv.insert(0, "kinetic-rust".to_string());
let result =
match *self {
Command::Write => {
let x: ::write::WriteArgs = CliCommand::from_argv(argv); try!(x.execute(shell))
},
Command::Info => {
let x: ::info::InfoArgs = CliCommand::from_argv(argv); try!(x.execute(shell))
},
Command::Bench => {
let x: ::bench::BenchArgs = CliCommand::from_argv(argv); try!(x.execute(shell))
},
Command::Log => {
let x: ::get_log::LogArgs = CliCommand::from_argv(argv); try!(x.execute(shell))
}
Command::Help => {
let x: ::help::HelpArgs = CliCommand::from_argv(argv); try!(x.execute(shell))
}
};
Ok(result) }
}
fn version() -> String {
format!("kinetic-rust {}\nkinetic-protocol {}", ::kinetic::version(), ::kinetic::protocol_version())
}
static USAGE: &'static str = "
Kinetic from Rust!
Usage: kinetic-rust <command> [<args>...]
kinetic-rust [options]
Options:
-h, --help Show this message.
--version Show the version of kinetic-rust.
--list List installed commands
-v, --verbose Use verbose output
Some common kinetic-rust commands are:
write Write objects to a kinetic device
info Show information for a kinetic device
See 'kinetic-rust help <command>' for more information on a specific command.
";
pub fn main_with_args(args : &[String], shell: &mut ::shell::MultiShell) -> KineticResult<()> {
let docopt = Docopt::new(USAGE).unwrap()
.options_first(true)
.argv(args.iter().map(|s| &s[..]))
.help(true)
.version(Some(version()));
let args: Args = docopt.decode().unwrap_or_else(|e| e.exit());
shell.set_verbose(args.flag_verbose);
if args.flag_list {
println!("Installed Commands:");
println!(" write");
println!(" info");
println!(" bench");
println!(" log");
println!(" help");
return Ok(());
}
match args.arg_command {
Some(cmd) => cmd.dispatch(args.arg_args, shell),
None => {
println!("{}", USAGE);
Ok(())
},
} }