2018-12-10 12:36:52 +00:00
|
|
|
use failure::*;
|
2019-07-16 09:27:45 +00:00
|
|
|
use lazy_static::lazy_static;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use std::collections::{HashMap, HashSet};
|
2019-01-03 13:36:31 +00:00
|
|
|
|
2019-02-17 09:16:33 +00:00
|
|
|
use crate::api_schema::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
use crate::api_schema::router::*;
|
2019-06-04 06:24:50 +00:00
|
|
|
use crate::api_schema::format::*;
|
2019-02-17 08:59:20 +00:00
|
|
|
//use crate::api_schema::config::*;
|
2019-01-26 14:08:02 +00:00
|
|
|
use super::environment::CliEnvironment;
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-02-21 08:07:25 +00:00
|
|
|
use super::getopts;
|
2019-01-26 13:50:37 +00:00
|
|
|
|
2019-07-16 09:27:45 +00:00
|
|
|
lazy_static!{
|
|
|
|
|
|
|
|
pub static ref OUTPUT_FORMAT: Arc<Schema> =
|
|
|
|
StringSchema::new("Output format.")
|
|
|
|
.format(Arc::new(ApiStringFormat::Enum(&["text", "json", "json-pretty"])))
|
|
|
|
.into();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to format and print result
|
|
|
|
///
|
|
|
|
/// This is implemented for machine generatable formats 'json' and
|
|
|
|
/// 'json-pretty'. The 'text' format needs to be handled somewhere
|
|
|
|
/// else.
|
2019-07-18 07:52:11 +00:00
|
|
|
pub fn format_and_print_result(result: &Value, output_format: &str) {
|
2019-07-16 09:27:45 +00:00
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result).unwrap());
|
|
|
|
} else if output_format == "json" {
|
|
|
|
println!("{}", serde_json::to_string(&result).unwrap());
|
|
|
|
} else {
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 10:14:01 +00:00
|
|
|
|
2019-02-22 16:40:37 +00:00
|
|
|
fn generate_usage_str(
|
|
|
|
prefix: &str,
|
|
|
|
cli_cmd: &CliCommand,
|
|
|
|
format: DocumentationFormat,
|
|
|
|
indent: &str) -> String {
|
2019-02-22 10:14:01 +00:00
|
|
|
|
|
|
|
let arg_param = &cli_cmd.arg_param;
|
|
|
|
let fixed_param = &cli_cmd.fixed_param;
|
|
|
|
let properties = &cli_cmd.info.parameters.properties;
|
2019-02-22 16:40:37 +00:00
|
|
|
let description = &cli_cmd.info.parameters.description;
|
2019-02-22 10:14:01 +00:00
|
|
|
|
|
|
|
let mut done_hash = HashSet::<&str>::new();
|
|
|
|
let mut args = String::new();
|
|
|
|
|
|
|
|
for positional_arg in arg_param {
|
2019-02-26 07:46:36 +00:00
|
|
|
match properties.get(positional_arg) {
|
2019-02-26 11:40:51 +00:00
|
|
|
Some((optional, schema)) => {
|
2019-02-26 07:46:36 +00:00
|
|
|
args.push(' ');
|
2019-02-26 11:40:51 +00:00
|
|
|
|
|
|
|
let is_array = if let Schema::Array(_) = schema.as_ref() { true } else { false };
|
2019-02-26 07:46:36 +00:00
|
|
|
if *optional { args.push('['); }
|
2019-02-26 11:40:51 +00:00
|
|
|
if is_array { args.push('{'); }
|
2019-02-26 07:46:36 +00:00
|
|
|
args.push('<'); args.push_str(positional_arg); args.push('>');
|
2019-02-26 11:40:51 +00:00
|
|
|
if is_array { args.push('}'); }
|
2019-02-26 07:46:36 +00:00
|
|
|
if *optional { args.push(']'); }
|
|
|
|
|
|
|
|
done_hash.insert(positional_arg);
|
|
|
|
}
|
|
|
|
None => panic!("no such property '{}' in schema", positional_arg),
|
|
|
|
}
|
2019-02-22 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 16:40:37 +00:00
|
|
|
let mut arg_descr = String::new();
|
|
|
|
for positional_arg in arg_param {
|
|
|
|
let (_optional, schema) = properties.get(positional_arg).unwrap();
|
|
|
|
let param_descr = get_property_description(
|
|
|
|
positional_arg, &schema, ParameterDisplayStyle::Fixed, format);
|
|
|
|
arg_descr.push_str(¶m_descr);
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:14:01 +00:00
|
|
|
let mut options = String::new();
|
|
|
|
|
|
|
|
let mut prop_names: Vec<&str> = properties.keys().map(|v| *v).collect();
|
|
|
|
prop_names.sort();
|
|
|
|
|
|
|
|
for prop in prop_names {
|
|
|
|
let (optional, schema) = properties.get(prop).unwrap();
|
|
|
|
if done_hash.contains(prop) { continue; }
|
2019-03-12 11:54:16 +00:00
|
|
|
if fixed_param.contains_key(&prop) { continue; }
|
2018-12-10 12:36:52 +00:00
|
|
|
|
2019-02-22 16:40:37 +00:00
|
|
|
let type_text = get_schema_type_text(&schema, ParameterDisplayStyle::Arg);
|
2019-02-22 10:14:01 +00:00
|
|
|
|
|
|
|
if *optional {
|
|
|
|
|
2019-02-28 15:20:00 +00:00
|
|
|
if options.len() > 0 { options.push('\n'); }
|
2019-02-22 16:40:37 +00:00
|
|
|
options.push_str(&get_property_description(prop, &schema, ParameterDisplayStyle::Arg, format));
|
2019-02-22 10:14:01 +00:00
|
|
|
|
|
|
|
} else {
|
2019-06-25 04:19:51 +00:00
|
|
|
args.push_str(" --"); args.push_str(prop);
|
2019-02-22 10:14:01 +00:00
|
|
|
args.push(' ');
|
|
|
|
args.push_str(&type_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
done_hash.insert(prop);
|
|
|
|
}
|
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
let option_indicator = if options.len() > 0 { " [OPTIONS]" } else { "" };
|
|
|
|
|
2019-02-23 10:29:18 +00:00
|
|
|
let mut text = match format {
|
2019-02-22 16:40:37 +00:00
|
|
|
DocumentationFormat::Short => {
|
2019-02-23 14:10:48 +00:00
|
|
|
return format!("{}{}{}{}\n\n", indent, prefix, args, option_indicator);
|
2019-02-22 16:40:37 +00:00
|
|
|
}
|
|
|
|
DocumentationFormat::Long => {
|
2019-02-23 14:10:48 +00:00
|
|
|
format!("{}{}{}{}\n\n", indent, prefix, args, option_indicator)
|
2019-02-22 16:40:37 +00:00
|
|
|
}
|
|
|
|
DocumentationFormat::Full => {
|
2019-02-23 14:10:48 +00:00
|
|
|
format!("{}{}{}{}\n\n{}\n\n", indent, prefix, args, option_indicator, description)
|
2019-02-22 16:40:37 +00:00
|
|
|
}
|
2019-02-23 10:29:18 +00:00
|
|
|
DocumentationFormat::ReST => {
|
2019-06-25 04:19:51 +00:00
|
|
|
format!("``{}{}{}``\n\n{}\n\n", prefix, args, option_indicator, description)
|
2019-02-22 16:40:37 +00:00
|
|
|
}
|
2019-02-23 10:29:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if arg_descr.len() > 0 {
|
|
|
|
text.push_str(&arg_descr);
|
2019-02-23 14:10:48 +00:00
|
|
|
text.push('\n');
|
2019-02-23 10:29:18 +00:00
|
|
|
}
|
|
|
|
if options.len() > 0 {
|
|
|
|
text.push_str(&options);
|
2019-02-23 14:10:48 +00:00
|
|
|
text.push('\n');
|
2019-02-22 16:40:37 +00:00
|
|
|
}
|
2019-02-23 10:29:18 +00:00
|
|
|
text
|
2019-02-22 10:14:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn print_simple_usage_error(prefix: &str, cli_cmd: &CliCommand, err: Error) {
|
|
|
|
|
2019-02-22 16:40:37 +00:00
|
|
|
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "");
|
2019-02-23 10:29:18 +00:00
|
|
|
eprint!("Error: {}\nUsage: {}", err, usage);
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
fn print_help(
|
|
|
|
top_def: &CommandLineInterface,
|
|
|
|
mut prefix: String,
|
|
|
|
args: &Vec<String>,
|
|
|
|
verbose: Option<bool>,
|
|
|
|
) {
|
|
|
|
let mut iface = top_def;
|
|
|
|
|
|
|
|
for cmd in args {
|
|
|
|
if let CommandLineInterface::Nested(map) = iface {
|
|
|
|
if let Some(subcmd) = find_command(map, cmd) {
|
|
|
|
iface = subcmd;
|
|
|
|
prefix.push(' ');
|
|
|
|
prefix.push_str(cmd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eprintln!("no such command '{}'", cmd);
|
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let format = match verbose.unwrap_or(false) {
|
|
|
|
true => DocumentationFormat::Full,
|
|
|
|
false => DocumentationFormat::Short,
|
|
|
|
};
|
|
|
|
|
|
|
|
match iface {
|
|
|
|
CommandLineInterface::Nested(map) => {
|
|
|
|
println!("Usage:\n\n{}", generate_nested_usage(&prefix, map, format));
|
|
|
|
}
|
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
|
|
|
println!("Usage: {}", generate_usage_str(&prefix, cli_cmd, format, ""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_simple_command(
|
|
|
|
top_def: &CommandLineInterface,
|
|
|
|
prefix: &str,
|
|
|
|
cli_cmd: &CliCommand,
|
|
|
|
args: Vec<String>,
|
|
|
|
) {
|
2019-01-03 13:36:31 +00:00
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
let (params, rest) = match getopts::parse_arguments(
|
|
|
|
&args, &cli_cmd.arg_param, &cli_cmd.info.parameters) {
|
|
|
|
Ok((p, r)) => (p, r),
|
|
|
|
Err(err) => {
|
2019-02-22 10:14:01 +00:00
|
|
|
print_simple_usage_error(prefix, cli_cmd, err.into());
|
2019-02-21 10:08:59 +00:00
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
|
|
|
};
|
2018-12-10 12:36:52 +00:00
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
if cli_cmd.info.handler.is_none() {
|
2019-02-23 14:10:48 +00:00
|
|
|
let prefix = prefix.split(' ').next().unwrap().to_string();
|
|
|
|
print_help(top_def, prefix, &rest, params["verbose"].as_bool());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:36:52 +00:00
|
|
|
if !rest.is_empty() {
|
2019-02-21 10:08:59 +00:00
|
|
|
let err = format_err!("got additional arguments: {:?}", rest);
|
2019-02-22 10:14:01 +00:00
|
|
|
print_simple_usage_error(prefix, cli_cmd, err);
|
2019-02-21 10:08:59 +00:00
|
|
|
std::process::exit(-1);
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
let mut rpcenv = CliEnvironment::new();
|
|
|
|
|
2019-04-16 08:36:04 +00:00
|
|
|
match (cli_cmd.info.handler.as_ref().unwrap())(params, &cli_cmd.info, &mut rpcenv) {
|
2019-02-21 10:08:59 +00:00
|
|
|
Ok(value) => {
|
2019-07-16 09:27:45 +00:00
|
|
|
if value != Value::Null {
|
|
|
|
println!("Result: {}", serde_json::to_string_pretty(&value).unwrap());
|
|
|
|
}
|
2019-02-21 10:08:59 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
eprintln!("Error: {}", err);
|
2019-07-17 11:40:10 +00:00
|
|
|
std::process::exit(-1);
|
2019-02-21 10:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLineInterface> {
|
2018-12-10 17:13:55 +00:00
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
if let Some(sub_cmd) = def.commands.get(name) {
|
2018-12-10 17:13:55 +00:00
|
|
|
return Some(sub_cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut matches: Vec<&str> = vec![];
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
for cmd in def.commands.keys() {
|
2018-12-10 17:13:55 +00:00
|
|
|
if cmd.starts_with(name) {
|
|
|
|
matches.push(cmd); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches.len() != 1 { return None; }
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
if let Some(sub_cmd) = def.commands.get(matches[0]) {
|
2018-12-10 17:13:55 +00:00
|
|
|
return Some(sub_cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:14:01 +00:00
|
|
|
fn print_nested_usage_error(prefix: &str, def: &CliCommandMap, err: Error) {
|
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
let usage = generate_nested_usage(prefix, def, DocumentationFormat::Short);
|
2019-02-22 10:14:01 +00:00
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
eprintln!("Error: {}\n\nUsage:\n\n{}", err, usage);
|
2019-02-22 10:14:01 +00:00
|
|
|
}
|
2019-02-21 10:08:59 +00:00
|
|
|
|
2019-02-23 10:29:18 +00:00
|
|
|
fn generate_nested_usage(prefix: &str, def: &CliCommandMap, format: DocumentationFormat) -> String {
|
2019-02-22 10:14:01 +00:00
|
|
|
|
|
|
|
let mut cmds: Vec<&String> = def.commands.keys().collect();
|
|
|
|
cmds.sort();
|
|
|
|
|
2019-02-23 10:29:18 +00:00
|
|
|
let mut usage = String::new();
|
|
|
|
|
2019-02-22 10:14:01 +00:00
|
|
|
for cmd in cmds {
|
|
|
|
let new_prefix = format!("{} {}", prefix, cmd);
|
|
|
|
|
|
|
|
match def.commands.get(cmd).unwrap() {
|
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
2019-02-23 10:29:18 +00:00
|
|
|
if usage.len() > 0 && format == DocumentationFormat::ReST {
|
|
|
|
usage.push_str("----\n\n");
|
|
|
|
}
|
|
|
|
usage.push_str(&generate_usage_str(&new_prefix, cli_cmd, format, ""));
|
2019-02-22 10:14:01 +00:00
|
|
|
}
|
|
|
|
CommandLineInterface::Nested(map) => {
|
2019-02-23 10:29:18 +00:00
|
|
|
usage.push_str(&generate_nested_usage(&new_prefix, map, format));
|
2019-02-22 10:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 10:08:59 +00:00
|
|
|
|
2019-02-23 10:29:18 +00:00
|
|
|
usage
|
2019-02-21 10:08:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 14:10:48 +00:00
|
|
|
fn handle_nested_command(
|
|
|
|
top_def: &CommandLineInterface,
|
|
|
|
prefix: &str,
|
|
|
|
def: &CliCommandMap,
|
|
|
|
mut args: Vec<String>,
|
|
|
|
) {
|
2018-12-10 12:36:52 +00:00
|
|
|
|
|
|
|
if args.len() < 1 {
|
2018-12-11 10:31:36 +00:00
|
|
|
let mut cmds: Vec<&String> = def.commands.keys().collect();
|
2018-12-10 12:36:52 +00:00
|
|
|
cmds.sort();
|
|
|
|
|
|
|
|
let list = cmds.iter().fold(String::new(),|mut s,item| {
|
|
|
|
if !s.is_empty() { s+= ", "; }
|
|
|
|
s += item;
|
|
|
|
s
|
|
|
|
});
|
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
let err = format_err!("no command specified.\nPossible commands: {}", list);
|
2019-02-22 10:14:01 +00:00
|
|
|
print_nested_usage_error(prefix, def, err);
|
2019-02-21 10:08:59 +00:00
|
|
|
std::process::exit(-1);
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let command = args.remove(0);
|
|
|
|
|
2018-12-10 17:13:55 +00:00
|
|
|
let sub_cmd = match find_command(def, &command) {
|
2018-12-10 12:36:52 +00:00
|
|
|
Some(cmd) => cmd,
|
2019-02-21 10:08:59 +00:00
|
|
|
None => {
|
|
|
|
let err = format_err!("no such command '{}'", command);
|
2019-02-22 10:14:01 +00:00
|
|
|
print_nested_usage_error(prefix, def, err);
|
2019-02-21 10:08:59 +00:00
|
|
|
std::process::exit(-1);
|
|
|
|
}
|
2018-12-10 12:36:52 +00:00
|
|
|
};
|
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
let new_prefix = format!("{} {}", prefix, command);
|
|
|
|
|
2018-12-10 12:36:52 +00:00
|
|
|
match sub_cmd {
|
2018-12-10 12:40:10 +00:00
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
2019-02-23 14:10:48 +00:00
|
|
|
handle_simple_command(top_def, &new_prefix, cli_cmd, args);
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
2018-12-10 12:40:10 +00:00
|
|
|
CommandLineInterface::Nested(map) => {
|
2019-02-23 14:10:48 +00:00
|
|
|
handle_nested_command(top_def, &new_prefix, map, args);
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 11:18:28 +00:00
|
|
|
fn print_property_completion(
|
|
|
|
schema: &Schema,
|
|
|
|
name: &str,
|
|
|
|
completion_functions: &HashMap<String, CompletionFunction>,
|
2019-03-12 13:39:51 +00:00
|
|
|
arg: &str,
|
|
|
|
param: &HashMap<String, String>,
|
|
|
|
) {
|
2018-12-12 11:18:28 +00:00
|
|
|
if let Some(callback) = completion_functions.get(name) {
|
2019-03-12 13:39:51 +00:00
|
|
|
let list = (callback)(arg, param);
|
2018-12-12 11:18:28 +00:00
|
|
|
for value in list {
|
|
|
|
if value.starts_with(arg) {
|
|
|
|
println!("{}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-12 07:33:50 +00:00
|
|
|
if let Schema::String(StringSchema { format: Some(format), ..} ) = schema {
|
2019-05-23 09:27:45 +00:00
|
|
|
if let ApiStringFormat::Enum(list) = *format.as_ref() {
|
2018-12-12 07:33:50 +00:00
|
|
|
for value in list {
|
|
|
|
if value.starts_with(arg) {
|
|
|
|
println!("{}", value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 06:51:43 +00:00
|
|
|
println!("");
|
|
|
|
}
|
2018-12-11 13:21:05 +00:00
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
fn record_done_argument(done: &mut HashMap<String, String>, parameters: &ObjectSchema, key: &str, value: &str) {
|
2018-12-12 06:51:43 +00:00
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
if let Some((_, schema)) = parameters.properties.get::<str>(key) {
|
|
|
|
match schema.as_ref() {
|
|
|
|
Schema::Array(_) => { /* do nothing ?? */ }
|
|
|
|
_ => { done.insert(key.to_owned(), value.to_owned()); }
|
2018-12-12 06:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_simple_completion(
|
|
|
|
cli_cmd: &CliCommand,
|
2019-03-12 13:39:51 +00:00
|
|
|
done: &mut HashMap<String, String>,
|
2019-03-12 13:53:41 +00:00
|
|
|
all_arg_param: &[&str], // this is always the full list
|
|
|
|
arg_param: &[&str], // we remove done arguments
|
2019-02-23 16:50:33 +00:00
|
|
|
args: &[String],
|
2018-12-12 06:51:43 +00:00
|
|
|
) {
|
|
|
|
// fixme: arg_param, fixed_param
|
|
|
|
//eprintln!("COMPL: {:?} {:?} {}", arg_param, args, args.len());
|
|
|
|
|
|
|
|
if !arg_param.is_empty() {
|
|
|
|
let prop_name = arg_param[0];
|
|
|
|
if args.len() > 1 {
|
2019-03-12 13:39:51 +00:00
|
|
|
record_done_argument(done, &cli_cmd.info.parameters, prop_name, &args[0]);
|
2019-03-12 13:53:41 +00:00
|
|
|
print_simple_completion(cli_cmd, done, arg_param, &arg_param[1..], &args[1..]);
|
2018-12-12 06:51:43 +00:00
|
|
|
return;
|
2018-12-12 12:00:58 +00:00
|
|
|
} else if args.len() == 1 {
|
2019-03-12 13:39:51 +00:00
|
|
|
record_done_argument(done, &cli_cmd.info.parameters, prop_name, &args[0]);
|
2019-01-19 11:53:07 +00:00
|
|
|
if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
|
2019-03-12 13:39:51 +00:00
|
|
|
print_property_completion(schema, prop_name, &cli_cmd.completion_functions, &args[0], done);
|
2018-12-11 13:21:05 +00:00
|
|
|
}
|
2018-12-12 06:51:43 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if args.is_empty() { return; }
|
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
// Try to parse all argumnets but last, record args already done
|
|
|
|
if args.len() > 1 {
|
|
|
|
let mut errors = ParameterError::new(); // we simply ignore any parsing errors here
|
2019-03-15 06:12:40 +00:00
|
|
|
let (data, _rest) = getopts::parse_argument_list(&args[0..args.len()-1], &cli_cmd.info.parameters, &mut errors);
|
2019-03-12 13:39:51 +00:00
|
|
|
for (key, value) in &data {
|
|
|
|
record_done_argument(done, &cli_cmd.info.parameters, key, value);
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 07:18:38 +00:00
|
|
|
|
2019-02-23 16:50:33 +00:00
|
|
|
let prefix = &args[args.len()-1]; // match on last arg
|
2018-12-12 06:51:43 +00:00
|
|
|
|
2018-12-12 07:18:38 +00:00
|
|
|
// complete option-name or option-value ?
|
2019-02-23 16:50:33 +00:00
|
|
|
if !prefix.starts_with("-") && args.len() > 1 {
|
|
|
|
let last = &args[args.len()-2];
|
2018-12-12 07:18:38 +00:00
|
|
|
if last.starts_with("--") && last.len() > 2 {
|
2018-12-12 09:37:03 +00:00
|
|
|
let prop_name = &last[2..];
|
2019-01-19 11:53:07 +00:00
|
|
|
if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
|
2019-03-12 13:39:51 +00:00
|
|
|
print_property_completion(schema, prop_name, &cli_cmd.completion_functions, &prefix, done);
|
2018-12-12 07:18:38 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 06:51:43 +00:00
|
|
|
|
2019-01-19 11:53:07 +00:00
|
|
|
for (name, (_optional, _schema)) in &cli_cmd.info.parameters.properties {
|
2019-03-12 13:39:51 +00:00
|
|
|
if done.contains_key(*name) { continue; }
|
2019-03-12 13:53:41 +00:00
|
|
|
if all_arg_param.contains(name) { continue; }
|
2018-12-12 07:18:38 +00:00
|
|
|
let option = String::from("--") + name;
|
2019-02-23 16:50:33 +00:00
|
|
|
if option.starts_with(prefix) {
|
2018-12-12 07:18:38 +00:00
|
|
|
println!("{}", option);
|
2018-12-12 06:51:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
fn print_help_completion(def: &CommandLineInterface, help_cmd: &CliCommand, args: &[String]) {
|
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
let mut done = HashMap::new();
|
2019-02-23 17:07:32 +00:00
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
match def {
|
|
|
|
CommandLineInterface::Simple(_) => {
|
2019-03-12 13:53:41 +00:00
|
|
|
print_simple_completion(help_cmd, &mut done, &help_cmd.arg_param, &help_cmd.arg_param, args);
|
2019-02-23 16:38:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CommandLineInterface::Nested(map) => {
|
|
|
|
if args.is_empty() {
|
|
|
|
for cmd in map.commands.keys() {
|
|
|
|
println!("{}", cmd);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-02-23 17:07:32 +00:00
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
let first = &args[0];
|
2019-02-23 17:07:32 +00:00
|
|
|
|
|
|
|
if first.starts_with("-") {
|
2019-03-12 13:53:41 +00:00
|
|
|
print_simple_completion(help_cmd, &mut done, &help_cmd.arg_param, &help_cmd.arg_param, args);
|
2019-02-23 17:07:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
if let Some(sub_cmd) = map.commands.get(first) {
|
|
|
|
print_help_completion(sub_cmd, help_cmd, &args[1..]);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-23 17:07:32 +00:00
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
for cmd in map.commands.keys() {
|
|
|
|
if cmd.starts_with(first) {
|
|
|
|
println!("{}", cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:50:33 +00:00
|
|
|
fn print_nested_completion(def: &CommandLineInterface, args: &[String]) {
|
2018-12-12 06:51:43 +00:00
|
|
|
|
|
|
|
match def {
|
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
2019-03-12 13:39:51 +00:00
|
|
|
let mut done: HashMap<String, String> = HashMap::new();
|
2019-03-15 06:12:40 +00:00
|
|
|
cli_cmd.fixed_param.iter().for_each(|(key, value)| {
|
2019-03-12 13:39:51 +00:00
|
|
|
record_done_argument(&mut done, &cli_cmd.info.parameters, &key, &value);
|
|
|
|
});
|
2019-03-12 13:53:41 +00:00
|
|
|
print_simple_completion(cli_cmd, &mut done, &cli_cmd.arg_param, &cli_cmd.arg_param, args);
|
2018-12-11 13:21:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
CommandLineInterface::Nested(map) => {
|
|
|
|
if args.is_empty() {
|
|
|
|
for cmd in map.commands.keys() {
|
|
|
|
println!("{}", cmd);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-02-23 16:50:33 +00:00
|
|
|
let first = &args[0];
|
2019-03-12 11:07:45 +00:00
|
|
|
if args.len() > 1 {
|
|
|
|
if let Some(sub_cmd) = map.commands.get(first) {
|
|
|
|
print_nested_completion(sub_cmd, &args[1..]);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-11 13:21:05 +00:00
|
|
|
}
|
|
|
|
for cmd in map.commands.keys() {
|
2019-02-23 16:50:33 +00:00
|
|
|
if cmd.starts_with(first) {
|
2018-12-11 13:21:05 +00:00
|
|
|
println!("{}", cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_bash_completion(def: &CommandLineInterface) {
|
|
|
|
|
|
|
|
let comp_point: usize = match std::env::var("COMP_POINT") {
|
|
|
|
Ok(val) => {
|
|
|
|
match usize::from_str_radix(&val, 10) {
|
|
|
|
Ok(i) => i,
|
2018-12-16 12:57:59 +00:00
|
|
|
Err(_) => return,
|
2018-12-11 13:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-16 12:57:59 +00:00
|
|
|
Err(_) => return,
|
2018-12-11 13:21:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let cmdline = match std::env::var("COMP_LINE") {
|
|
|
|
Ok(val) => val[0..comp_point].to_owned(),
|
2018-12-16 12:57:59 +00:00
|
|
|
Err(_) => return,
|
2018-12-11 13:21:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut args = match shellwords::split(&cmdline) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
2019-03-07 11:14:26 +00:00
|
|
|
if args.len() == 0 { return; }
|
|
|
|
|
2018-12-11 13:21:05 +00:00
|
|
|
args.remove(0); //no need for program name
|
|
|
|
|
2018-12-12 06:51:43 +00:00
|
|
|
if cmdline.ends_with(char::is_whitespace) {
|
|
|
|
//eprintln!("CMDLINE {:?}", cmdline);
|
|
|
|
args.push("".into());
|
|
|
|
}
|
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
if !args.is_empty() && args[0] == "help" {
|
|
|
|
print_help_completion(def, &help_command_def(), &args[1..]);
|
|
|
|
} else {
|
2019-02-23 16:50:33 +00:00
|
|
|
print_nested_completion(def, &args);
|
2019-02-23 16:38:10 +00:00
|
|
|
}
|
2018-12-11 13:21:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 16:38:10 +00:00
|
|
|
fn help_command_def() -> CliCommand {
|
|
|
|
CliCommand::new(
|
2019-04-16 08:36:04 +00:00
|
|
|
ApiMethod::new_dummy(
|
2019-02-23 14:10:48 +00:00
|
|
|
ObjectSchema::new("Get help about specified command.")
|
|
|
|
.optional("verbose", BooleanSchema::new("Verbose help."))
|
|
|
|
)
|
2019-02-23 16:38:10 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_cli_command(def: CommandLineInterface) {
|
2019-02-23 14:10:48 +00:00
|
|
|
|
|
|
|
let def = match def {
|
|
|
|
CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd),
|
2019-02-23 16:38:10 +00:00
|
|
|
CommandLineInterface::Nested(map) =>
|
|
|
|
CommandLineInterface::Nested(map.insert("help", help_command_def().into())),
|
2019-02-23 14:10:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let top_def = &def; // we pass this to the help function ...
|
2019-02-21 10:08:59 +00:00
|
|
|
|
|
|
|
let mut args = std::env::args();
|
2018-12-10 12:36:52 +00:00
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
let prefix = args.next().unwrap();
|
|
|
|
let prefix = prefix.rsplit('/').next().unwrap(); // without path
|
|
|
|
|
|
|
|
let args: Vec<String> = args.collect();
|
2018-12-10 12:36:52 +00:00
|
|
|
|
2019-02-23 10:29:18 +00:00
|
|
|
if !args.is_empty() {
|
|
|
|
if args[0] == "bashcomplete" {
|
2019-02-23 14:10:48 +00:00
|
|
|
print_bash_completion(&def);
|
2019-02-23 10:29:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0] == "printdoc" {
|
|
|
|
let usage = match def {
|
|
|
|
CommandLineInterface::Simple(cli_cmd) => {
|
2019-02-23 14:10:48 +00:00
|
|
|
generate_usage_str(&prefix, &cli_cmd, DocumentationFormat::ReST, "")
|
2019-02-23 10:29:18 +00:00
|
|
|
}
|
|
|
|
CommandLineInterface::Nested(map) => {
|
2019-02-23 14:10:48 +00:00
|
|
|
generate_nested_usage(&prefix, &map, DocumentationFormat::ReST)
|
2019-02-23 10:29:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
println!("{}", usage);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-11 11:54:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 10:08:59 +00:00
|
|
|
match def {
|
2019-02-23 14:10:48 +00:00
|
|
|
CommandLineInterface::Simple(ref cli_cmd) => handle_simple_command(top_def, &prefix, &cli_cmd, args),
|
|
|
|
CommandLineInterface::Nested(ref map) => handle_nested_command(top_def, &prefix, &map, args),
|
2019-01-03 13:36:31 +00:00
|
|
|
};
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 13:39:51 +00:00
|
|
|
pub type CompletionFunction = fn(&str, &HashMap<String, String>) -> Vec<String>;
|
2018-12-12 11:18:28 +00:00
|
|
|
|
2018-12-10 12:36:52 +00:00
|
|
|
pub struct CliCommand {
|
|
|
|
pub info: ApiMethod,
|
|
|
|
pub arg_param: Vec<&'static str>,
|
2019-03-12 11:54:16 +00:00
|
|
|
pub fixed_param: HashMap<&'static str, String>,
|
2018-12-12 11:18:28 +00:00
|
|
|
pub completion_functions: HashMap<String, CompletionFunction>,
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 10:12:13 +00:00
|
|
|
impl CliCommand {
|
|
|
|
|
|
|
|
pub fn new(info: ApiMethod) -> Self {
|
2018-12-12 11:18:28 +00:00
|
|
|
Self {
|
|
|
|
info, arg_param: vec![],
|
2019-03-12 11:54:16 +00:00
|
|
|
fixed_param: HashMap::new(),
|
2018-12-12 11:18:28 +00:00
|
|
|
completion_functions: HashMap::new(),
|
|
|
|
}
|
2018-12-11 10:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn arg_param(mut self, names: Vec<&'static str>) -> Self {
|
|
|
|
self.arg_param = names;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-12 11:54:16 +00:00
|
|
|
pub fn fixed_param(mut self, key: &'static str, value: String) -> Self {
|
|
|
|
self.fixed_param.insert(key, value);
|
2018-12-11 10:12:13 +00:00
|
|
|
self
|
|
|
|
}
|
2018-12-12 11:18:28 +00:00
|
|
|
|
|
|
|
pub fn completion_cb(mut self, param_name: &str, cb: CompletionFunction) -> Self {
|
|
|
|
self.completion_functions.insert(param_name.into(), cb);
|
|
|
|
self
|
|
|
|
}
|
2018-12-11 10:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
pub struct CliCommandMap {
|
|
|
|
pub commands: HashMap<String, CommandLineInterface>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CliCommandMap {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { commands: HashMap:: new() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert<S: Into<String>>(mut self, name: S, cli: CommandLineInterface) -> Self {
|
|
|
|
self.commands.insert(name.into(), cli);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:40:10 +00:00
|
|
|
pub enum CommandLineInterface {
|
2018-12-10 12:36:52 +00:00
|
|
|
Simple(CliCommand),
|
2018-12-11 10:31:36 +00:00
|
|
|
Nested(CliCommandMap),
|
2018-12-10 12:36:52 +00:00
|
|
|
}
|
2018-12-10 12:51:10 +00:00
|
|
|
|
|
|
|
impl From<CliCommand> for CommandLineInterface {
|
|
|
|
fn from(cli_cmd: CliCommand) -> Self {
|
|
|
|
CommandLineInterface::Simple(cli_cmd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 10:31:36 +00:00
|
|
|
impl From<CliCommandMap> for CommandLineInterface {
|
|
|
|
fn from(list: CliCommandMap) -> Self {
|
|
|
|
CommandLineInterface::Nested(list)
|
2018-12-10 12:51:10 +00:00
|
|
|
}
|
|
|
|
}
|