formatting fixup

please run `make` to check formatting before committing

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-12-05 11:33:12 +01:00
parent 7c48247c58
commit 92ffe4c240
8 changed files with 274 additions and 326 deletions

View File

@ -63,11 +63,11 @@ pub struct CliCommand {
}
impl CliCommand {
/// Create a new instance.
pub fn new(info: &'static ApiMethod) -> Self {
Self {
info, arg_param: &[],
info,
arg_param: &[],
fixed_param: HashMap::new(),
completion_functions: HashMap::new(),
}
@ -86,7 +86,7 @@ impl CliCommand {
}
/// Set completion functions.
pub fn completion_cb(mut self, param_name: &str, cb: CompletionFunction) -> Self {
pub fn completion_cb(mut self, param_name: &str, cb: CompletionFunction) -> Self {
self.completion_functions.insert(param_name.into(), cb);
self
}
@ -100,10 +100,11 @@ pub struct CliCommandMap {
}
impl CliCommandMap {
/// Create a new instance.
pub fn new() -> Self {
Self { commands: HashMap:: new() }
Self {
commands: HashMap::new(),
}
}
/// Insert another command.
@ -114,12 +115,12 @@ impl CliCommandMap {
/// Insert the help command.
pub fn insert_help(mut self) -> Self {
self.commands.insert(String::from("help"), help_command_def().into());
self.commands
.insert(String::from("help"), help_command_def().into());
self
}
fn find_command(&self, name: &str) -> Option<(String, &CommandLineInterface)> {
if let Some(sub_cmd) = self.commands.get(name) {
return Some((name.to_string(), sub_cmd));
};
@ -128,10 +129,13 @@ impl CliCommandMap {
for cmd in self.commands.keys() {
if cmd.starts_with(name) {
matches.push(cmd); }
matches.push(cmd);
}
}
if matches.len() != 1 { return None; }
if matches.len() != 1 {
return None;
}
if let Some(sub_cmd) = self.commands.get(matches[0]) {
return Some((matches[0].to_string(), sub_cmd));
@ -149,7 +153,7 @@ pub enum CommandLineInterface {
impl From<CliCommand> for CommandLineInterface {
fn from(cli_cmd: CliCommand) -> Self {
CommandLineInterface::Simple(cli_cmd)
CommandLineInterface::Simple(cli_cmd)
}
}

View File

@ -1,17 +1,17 @@
use failure::*;
use serde_json::Value;
use std::sync::Arc;
use std::cell::RefCell;
use std::sync::Arc;
use crate::*;
use crate::format::*;
use crate::schema::*;
use crate::*;
use super::environment::CliEnvironment;
use super::getopts;
use super::{CommandLineInterface, CliCommand, CliCommandMap, completion::*};
use super::format::*;
use super::getopts;
use super::{completion::*, CliCommand, CliCommandMap, CommandLineInterface};
/// Schema definition for ``--output-format`` parameter.
///
@ -19,8 +19,7 @@ use super::format::*;
/// - ``json``: JSON, single line.
/// - ``json-pretty``: JSON, human readable.
///
pub const OUTPUT_FORMAT: Schema =
StringSchema::new("Output format.")
pub const OUTPUT_FORMAT: Schema = StringSchema::new("Output format.")
.format(&ApiStringFormat::Enum(&["text", "json", "json-pretty"]))
.schema();
@ -29,16 +28,15 @@ fn handle_simple_command(
cli_cmd: &CliCommand,
args: Vec<String>,
) -> Result<(), Error> {
let (params, rest) = match getopts::parse_arguments(
&args, cli_cmd.arg_param, &cli_cmd.info.parameters) {
Ok((p, r)) => (p, r),
Err(err) => {
let err_msg = err.to_string();
print_simple_usage_error(prefix, cli_cmd, &err_msg);
return Err(format_err!("{}", err_msg));
}
};
let (params, rest) =
match getopts::parse_arguments(&args, cli_cmd.arg_param, &cli_cmd.info.parameters) {
Ok((p, r)) => (p, r),
Err(err) => {
let err_msg = err.to_string();
print_simple_usage_error(prefix, cli_cmd, &err_msg);
return Err(format_err!("{}", err_msg));
}
};
if !rest.is_empty() {
let err_msg = format!("got additional arguments: {:?}", rest);
@ -49,22 +47,19 @@ fn handle_simple_command(
let mut rpcenv = CliEnvironment::new();
match cli_cmd.info.handler {
ApiHandler::Sync(handler) => {
match (handler)(params, &cli_cmd.info, &mut rpcenv) {
Ok(value) => {
if value != Value::Null {
println!("Result: {}", serde_json::to_string_pretty(&value).unwrap());
}
}
Err(err) => {
eprintln!("Error: {}", err);
return Err(err);
ApiHandler::Sync(handler) => match (handler)(params, &cli_cmd.info, &mut rpcenv) {
Ok(value) => {
if value != Value::Null {
println!("Result: {}", serde_json::to_string_pretty(&value).unwrap());
}
}
}
Err(err) => {
eprintln!("Error: {}", err);
return Err(err);
}
},
ApiHandler::AsyncHttp(_) => {
let err_msg =
"CliHandler does not support ApiHandler::AsyncHttp - internal error";
let err_msg = "CliHandler does not support ApiHandler::AsyncHttp - internal error";
print_simple_usage_error(prefix, cli_cmd, err_msg);
return Err(format_err!("{}", err_msg));
}
@ -78,13 +73,14 @@ fn handle_nested_command(
def: &CliCommandMap,
mut args: Vec<String>,
) -> Result<(), Error> {
if args.len() < 1 {
let mut cmds: Vec<&String> = def.commands.keys().collect();
cmds.sort();
let list = cmds.iter().fold(String::new(),|mut s,item| {
if !s.is_empty() { s+= ", "; }
let list = cmds.iter().fold(String::new(), |mut s, item| {
if !s.is_empty() {
s += ", ";
}
s += item;
s
});
@ -124,19 +120,22 @@ const API_METHOD_COMMAND_HELP: ApiMethod = ApiMethod::new(
&ObjectSchema::new(
"Get help about specified command (or sub-command).",
&[
( "command",
true,
&ArraySchema::new(
"Command. This may be a list in order to spefify nested sub-commands.",
&StringSchema::new("Name.").schema()
).schema()
(
"command",
true,
&ArraySchema::new(
"Command. This may be a list in order to spefify nested sub-commands.",
&StringSchema::new("Name.").schema(),
)
.schema(),
),
( "verbose",
true,
&BooleanSchema::new("Verbose help.").schema()
(
"verbose",
true,
&BooleanSchema::new("Verbose help.").schema(),
),
],
)
),
);
std::thread_local! {
@ -148,23 +147,21 @@ fn help_command(
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let command: Vec<String> = param["command"].as_array().unwrap_or(&Vec::new())
let command: Vec<String> = param["command"]
.as_array()
.unwrap_or(&Vec::new())
.iter()
.map(|v| v.as_str().unwrap().to_string())
.collect();
let verbose = param["verbose"].as_bool();
HELP_CONTEXT.with(|ctx| {
match &*ctx.borrow() {
Some(def) => {
print_help(def, String::from(""), &command, verbose);
}
None => {
eprintln!("Sorry, help context not set - internal error.");
}
HELP_CONTEXT.with(|ctx| match &*ctx.borrow() {
Some(def) => {
print_help(def, String::from(""), &command, verbose);
}
None => {
eprintln!("Sorry, help context not set - internal error.");
}
});
@ -172,12 +169,13 @@ fn help_command(
}
fn set_help_context(def: Option<Arc<CommandLineInterface>>) {
HELP_CONTEXT.with(|ctx| { *ctx.borrow_mut() = def; });
HELP_CONTEXT.with(|ctx| {
*ctx.borrow_mut() = def;
});
}
pub(crate) fn help_command_def() -> CliCommand {
CliCommand::new(&API_METHOD_COMMAND_HELP)
.arg_param(&["command"])
pub(crate) fn help_command_def() -> CliCommand {
CliCommand::new(&API_METHOD_COMMAND_HELP).arg_param(&["command"])
}
/// Handle command invocation.
@ -189,16 +187,11 @@ pub fn handle_command(
prefix: &str,
args: Vec<String>,
) -> Result<(), Error> {
set_help_context(Some(def.clone()));
let result = match &*def {
CommandLineInterface::Simple(ref cli_cmd) => {
handle_simple_command(&prefix, &cli_cmd, args)
}
CommandLineInterface::Nested(ref map) => {
handle_nested_command(&prefix, &map, args)
}
CommandLineInterface::Simple(ref cli_cmd) => handle_simple_command(&prefix, &cli_cmd, args),
CommandLineInterface::Nested(ref map) => handle_nested_command(&prefix, &map, args),
};
set_help_context(None);
@ -219,11 +212,9 @@ pub fn handle_command(
/// - ``printdoc``: Output ReST documentation.
///
pub fn run_cli_command(def: CommandLineInterface) {
let def = match def {
CommandLineInterface::Simple(cli_cmd) => CommandLineInterface::Simple(cli_cmd),
CommandLineInterface::Nested(map) =>
CommandLineInterface::Nested(map.insert_help().into()),
CommandLineInterface::Nested(map) => CommandLineInterface::Nested(map.insert_help().into()),
};
let mut args = std::env::args();
@ -242,7 +233,7 @@ pub fn run_cli_command(def: CommandLineInterface) {
if args[0] == "printdoc" {
let usage = match def {
CommandLineInterface::Simple(cli_cmd) => {
generate_usage_str(&prefix, &cli_cmd, DocumentationFormat::ReST, "")
generate_usage_str(&prefix, &cli_cmd, DocumentationFormat::ReST, "")
}
CommandLineInterface::Nested(map) => {
generate_nested_usage(&prefix, &map, DocumentationFormat::ReST)

View File

@ -6,13 +6,14 @@ fn record_done_argument(
done: &mut HashMap<String, String>,
parameters: &ObjectSchema,
key: &str,
value: &str
value: &str,
) {
if let Some((_, schema)) = parameters.lookup(key) {
match schema {
Schema::Array(_) => { /* do nothing ?? */ }
_ => { done.insert(key.to_owned(), value.to_owned()); }
_ => {
done.insert(key.to_owned(), value.to_owned());
}
}
}
}
@ -24,7 +25,6 @@ fn get_property_completion(
arg: &str,
param: &HashMap<String, String>,
) -> Vec<String> {
if let Some(callback) = completion_functions.get(name) {
let list = (callback)(arg, param);
let mut completions = Vec::new();
@ -36,7 +36,11 @@ fn get_property_completion(
return completions;
}
if let Schema::String(StringSchema { format: Some(format), ..} ) = schema {
if let Schema::String(StringSchema {
format: Some(format),
..
}) = schema
{
if let ApiStringFormat::Enum(list) = format {
let mut completions = Vec::new();
for value in list.iter() {
@ -80,31 +84,49 @@ fn get_simple_completion(
} else if args.len() == 1 {
record_done_argument(done, cli_cmd.info.parameters, prop_name, &args[0]);
if let Some((_, schema)) = cli_cmd.info.parameters.lookup(prop_name) {
return get_property_completion(schema, prop_name, &cli_cmd.completion_functions, &args[0], done);
return get_property_completion(
schema,
prop_name,
&cli_cmd.completion_functions,
&args[0],
done,
);
}
}
return Vec::new();
}
if args.is_empty() { return Vec::new(); }
if args.is_empty() {
return Vec::new();
}
// 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
let (data, _rest) = getopts::parse_argument_list(&args[0..args.len()-1], &cli_cmd.info.parameters, &mut errors);
let (data, _rest) = getopts::parse_argument_list(
&args[0..args.len() - 1],
&cli_cmd.info.parameters,
&mut errors,
);
for (key, value) in &data {
record_done_argument(done, &cli_cmd.info.parameters, key, value);
}
}
let prefix = &args[args.len()-1]; // match on last arg
let prefix = &args[args.len() - 1]; // match on last arg
// complete option-name or option-value ?
if !prefix.starts_with("-") && args.len() > 1 {
let last = &args[args.len()-2];
let last = &args[args.len() - 2];
if last.starts_with("--") && last.len() > 2 {
let prop_name = &last[2..];
if let Some((_, schema)) = cli_cmd.info.parameters.lookup(prop_name) {
return get_property_completion(schema, prop_name, &cli_cmd.completion_functions, &prefix, done);
return get_property_completion(
schema,
prop_name,
&cli_cmd.completion_functions,
&prefix,
done,
);
}
return Vec::new();
}
@ -112,8 +134,12 @@ fn get_simple_completion(
let mut completions = Vec::new();
for (name, _optional, _schema) in cli_cmd.info.parameters.properties {
if done.contains_key(*name) { continue; }
if cli_cmd.arg_param.contains(name) { continue; }
if done.contains_key(*name) {
continue;
}
if cli_cmd.arg_param.contains(name) {
continue;
}
let option = String::from("--") + name;
if option.starts_with(prefix) {
completions.push(option);
@ -127,7 +153,6 @@ fn get_help_completion(
help_cmd: &CliCommand,
args: &[String],
) -> Vec<String> {
let mut done = HashMap::new();
match def {
@ -145,7 +170,8 @@ fn get_help_completion(
let first = &args[0];
if args.len() > 1 {
if let Some(sub_cmd) = map.commands.get(first) { // do exact match here
if let Some(sub_cmd) = map.commands.get(first) {
// do exact match here
return get_help_completion(sub_cmd, help_cmd, &args[1..]);
}
return Vec::new();
@ -166,11 +192,7 @@ fn get_help_completion(
}
}
fn get_nested_completion(
def: &CommandLineInterface,
args: &[String],
) -> Vec<String> {
fn get_nested_completion(def: &CommandLineInterface, args: &[String]) -> Vec<String> {
match def {
CommandLineInterface::Simple(cli_cmd) => {
let mut done: HashMap<String, String> = HashMap::new();
@ -212,14 +234,11 @@ fn get_nested_completion(
/// passed to ``get_completions()``. Returned values are printed to
/// ``stdout``.
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,
Err(_) => return,
}
}
Ok(val) => match usize::from_str_radix(&val, 10) {
Ok(i) => i,
Err(_) => return,
},
Err(_) => return,
};
@ -241,21 +260,21 @@ pub fn get_completions(
line: &str,
skip_first: bool,
) -> (usize, Vec<String>) {
let (mut args, start ) = match shellword_split_unclosed(line, false) {
let (mut args, start) = match shellword_split_unclosed(line, false) {
(mut args, None) => {
args.push("".into());
(args, line.len())
}
(mut args, Some((start , arg, _quote))) => {
(mut args, Some((start, arg, _quote))) => {
args.push(arg);
(args, start)
}
};
if skip_first {
if args.len() == 0 { return (0, Vec::new()); }
if args.len() == 0 {
return (0, Vec::new());
}
args.remove(0); // no need for program name
}
@ -275,14 +294,13 @@ mod test {
use failure::*;
use serde_json::Value;
use crate::{*, schema::*, cli::*};
use crate::{cli::*, schema::*, *};
fn dummy_method(
_param: Value,
_info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
Ok(Value::Null)
}
@ -291,13 +309,20 @@ mod test {
&ObjectSchema::new(
"Simple API method with one required and one optionl argument.",
&[
( "optional-arg", true, &BooleanSchema::new("Optional boolean argument.")
.default(false)
.schema()
(
"optional-arg",
true,
&BooleanSchema::new("Optional boolean argument.")
.default(false)
.schema(),
),
( "required-arg", false, &StringSchema::new("Required string argument.").schema()),
]
)
(
"required-arg",
false,
&StringSchema::new("Required string argument.").schema(),
),
],
),
);
fn get_complex_test_cmddef() -> CommandLineInterface {
@ -309,23 +334,23 @@ mod test {
.insert_help()
.insert("l0sub", CommandLineInterface::Nested(sub_def))
.insert("l0c1", CliCommand::new(&API_METHOD_SIMPLE1).into())
.insert("l0c2", CliCommand::new(&API_METHOD_SIMPLE1)
.insert(
"l0c2",
CliCommand::new(&API_METHOD_SIMPLE1)
.arg_param(&["required-arg"])
.into())
.insert("l0c3", CliCommand::new(&API_METHOD_SIMPLE1)
.into(),
)
.insert(
"l0c3",
CliCommand::new(&API_METHOD_SIMPLE1)
.arg_param(&["required-arg", "optional-arg"])
.into());
.into(),
);
cmd_def.into()
}
fn test_completions(
cmd_def: &CommandLineInterface,
line: &str,
start: usize,
expect: &[&str],
) {
fn test_completions(cmd_def: &CommandLineInterface, line: &str, start: usize, expect: &[&str]) {
let mut expect: Vec<String> = expect.iter().map(|s| s.to_string()).collect();
expect.sort();
@ -337,29 +362,13 @@ mod test {
#[test]
fn test_nested_completion() {
let cmd_def = get_complex_test_cmddef();
test_completions(
&cmd_def,
"",
0,
&["help", "l0c1", "l0c2", "l0c3", "l0sub"],
);
test_completions(&cmd_def, "", 0, &["help", "l0c1", "l0c2", "l0c3", "l0sub"]);
test_completions(
&cmd_def,
"l0c1 ",
5,
&["--optional-arg", "--required-arg"],
);
test_completions(&cmd_def, "l0c1 ", 5, &["--optional-arg", "--required-arg"]);
test_completions(
&cmd_def,
"l0c1 -",
5,
&["--optional-arg", "--required-arg"],
);
test_completions(&cmd_def, "l0c1 -", 5, &["--optional-arg", "--required-arg"]);
test_completions(
&cmd_def,
@ -368,33 +377,13 @@ mod test {
&["--optional-arg", "--required-arg"],
);
test_completions(
&cmd_def,
"l0c1 ---",
5,
&[],
);
test_completions(&cmd_def, "l0c1 ---", 5, &[]);
test_completions(
&cmd_def,
"l0c1 x",
5,
&[],
);
test_completions(&cmd_def, "l0c1 x", 5, &[]);
test_completions(
&cmd_def,
"l0c1 --r",
5,
&["--required-arg"],
);
test_completions(&cmd_def, "l0c1 --r", 5, &["--required-arg"]);
test_completions(
&cmd_def,
"l0c1 --required-arg",
5,
&["--required-arg"],
);
test_completions(&cmd_def, "l0c1 --required-arg", 5, &["--required-arg"]);
test_completions(
&cmd_def,
@ -438,19 +427,13 @@ mod test {
40,
&["yes"],
);
}
}
#[test]
fn test_help_completion() {
let cmd_def = get_complex_test_cmddef();
test_completions(
&cmd_def,
"h",
0,
&["help"],
);
test_completions(&cmd_def, "h", 0, &["help"]);
test_completions(
&cmd_def,
@ -459,76 +442,24 @@ mod test {
&["help", "l0sub", "l0c1", "l0c3", "l0c2"],
);
test_completions(
&cmd_def,
"help l0",
5,
&["l0sub", "l0c1", "l0c3", "l0c2"],
);
test_completions(&cmd_def, "help l0", 5, &["l0sub", "l0c1", "l0c3", "l0c2"]);
test_completions(
&cmd_def,
"help -",
5,
&["--verbose"],
);
test_completions(&cmd_def, "help -", 5, &["--verbose"]);
test_completions(
&cmd_def,
"help l0c2",
5,
&["l0c2"],
);
test_completions(&cmd_def, "help l0c2", 5, &["l0c2"]);
test_completions(
&cmd_def,
"help l0c2 ",
10,
&["--verbose"],
);
test_completions(&cmd_def, "help l0c2 ", 10, &["--verbose"]);
test_completions(
&cmd_def,
"help l0c2 --verbose -",
20,
&[],
);
test_completions(&cmd_def, "help l0c2 --verbose -", 20, &[]);
test_completions(
&cmd_def,
"help l0s",
5,
&["l0sub"],
);
test_completions(&cmd_def, "help l0s", 5, &["l0sub"]);
test_completions(
&cmd_def,
"help l0sub ",
11,
&["l1c1", "l1c2"],
);
test_completions(&cmd_def, "help l0sub ", 11, &["l1c1", "l1c2"]);
test_completions(
&cmd_def,
"help l0sub l1c2 -",
16,
&["--verbose"],
);
test_completions(&cmd_def, "help l0sub l1c2 -", 16, &["--verbose"]);
test_completions(
&cmd_def,
"help l0sub l1c2 --verbose -",
26,
&[],
);
test_completions(
&cmd_def,
"help l0sub l1c3",
11,
&[],
);
test_completions(&cmd_def, "help l0sub l1c2 --verbose -", 26, &[]);
test_completions(&cmd_def, "help l0sub l1c3", 11, &[]);
}
}

View File

@ -1,5 +1,5 @@
use std::collections::HashMap;
use serde_json::Value;
use std::collections::HashMap;
use crate::{RpcEnvironment, RpcEnvironmentType};
@ -19,7 +19,6 @@ impl CliEnvironment {
}
impl RpcEnvironment for CliEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}

View File

@ -2,21 +2,17 @@ use serde_json::Value;
use std::collections::HashSet;
use crate::schema::*;
use crate::format::*;
use crate::schema::*;
use super::{CommandLineInterface, CliCommand, CliCommandMap};
use super::{CliCommand, CliCommandMap, CommandLineInterface};
/// 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.
pub fn format_and_print_result(
result: &Value,
output_format: &str,
) {
pub fn format_and_print_result(result: &Value, output_format: &str) {
if output_format == "json-pretty" {
println!("{}", serde_json::to_string_pretty(&result).unwrap());
} else if output_format == "json" {
@ -31,8 +27,8 @@ pub fn generate_usage_str(
prefix: &str,
cli_cmd: &CliCommand,
format: DocumentationFormat,
indent: &str) -> String {
indent: &str,
) -> String {
let arg_param = cli_cmd.arg_param;
let fixed_param = &cli_cmd.fixed_param;
let schema = cli_cmd.info.parameters;
@ -45,12 +41,26 @@ pub fn generate_usage_str(
Some((optional, param_schema)) => {
args.push(' ');
let is_array = if let Schema::Array(_) = param_schema { true } else { false };
if optional { args.push('['); }
if is_array { args.push('{'); }
args.push('<'); args.push_str(positional_arg); args.push('>');
if is_array { args.push('}'); }
if optional { args.push(']'); }
let is_array = if let Schema::Array(_) = param_schema {
true
} else {
false
};
if optional {
args.push('[');
}
if is_array {
args.push('{');
}
args.push('<');
args.push_str(positional_arg);
args.push('>');
if is_array {
args.push('}');
}
if optional {
args.push(']');
}
done_hash.insert(positional_arg);
}
@ -62,25 +72,39 @@ pub fn generate_usage_str(
for positional_arg in arg_param {
let (_optional, param_schema) = schema.lookup(positional_arg).unwrap();
let param_descr = get_property_description(
positional_arg, param_schema, ParameterDisplayStyle::Fixed, format);
positional_arg,
param_schema,
ParameterDisplayStyle::Fixed,
format,
);
arg_descr.push_str(&param_descr);
}
let mut options = String::new();
for (prop, optional, param_schema) in schema.properties {
if done_hash.contains(prop) { continue; }
if fixed_param.contains_key(prop) { continue; }
if done_hash.contains(prop) {
continue;
}
if fixed_param.contains_key(prop) {
continue;
}
let type_text = get_schema_type_text(param_schema, ParameterDisplayStyle::Arg);
if *optional {
if options.len() > 0 { options.push('\n'); }
options.push_str(&get_property_description(prop, param_schema, ParameterDisplayStyle::Arg, format));
if options.len() > 0 {
options.push('\n');
}
options.push_str(&get_property_description(
prop,
param_schema,
ParameterDisplayStyle::Arg,
format,
));
} else {
args.push_str(" --"); args.push_str(prop);
args.push_str(" --");
args.push_str(prop);
args.push(' ');
args.push_str(&type_text);
}
@ -97,12 +121,14 @@ pub fn generate_usage_str(
DocumentationFormat::Long => {
format!("{}{}{}{}\n\n", indent, prefix, args, option_indicator)
}
DocumentationFormat::Full => {
format!("{}{}{}{}\n\n{}\n\n", indent, prefix, args, option_indicator, schema.description)
}
DocumentationFormat::ReST => {
format!("``{}{}{}``\n\n{}\n\n", prefix, args, option_indicator, schema.description)
}
DocumentationFormat::Full => format!(
"{}{}{}{}\n\n{}\n\n",
indent, prefix, args, option_indicator, schema.description
),
DocumentationFormat::ReST => format!(
"``{}{}{}``\n\n{}\n\n",
prefix, args, option_indicator, schema.description
),
};
if arg_descr.len() > 0 {
@ -117,21 +143,13 @@ pub fn generate_usage_str(
}
/// Print command usage for simple commands to ``stderr``.
pub fn print_simple_usage_error(
prefix: &str,
cli_cmd: &CliCommand,
err_msg: &str,
) {
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "");
pub fn print_simple_usage_error(prefix: &str, cli_cmd: &CliCommand, err_msg: &str) {
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "");
eprint!("Error: {}\nUsage: {}", err_msg, usage);
}
/// Print command usage for nested commands to ``stderr``.
pub fn print_nested_usage_error(
prefix: &str,
def: &CliCommandMap,
err_msg: &str,
) {
pub fn print_nested_usage_error(prefix: &str, def: &CliCommandMap, err_msg: &str) {
let usage = generate_nested_usage(prefix, def, DocumentationFormat::Short);
eprintln!("Error: {}\n\nUsage:\n\n{}", err_msg, usage);
}
@ -140,9 +158,8 @@ pub fn print_nested_usage_error(
pub fn generate_nested_usage(
prefix: &str,
def: &CliCommandMap,
format: DocumentationFormat
format: DocumentationFormat,
) -> String {
let mut cmds: Vec<&String> = def.commands.keys().collect();
cmds.sort();
@ -180,7 +197,9 @@ pub fn print_help(
if let CommandLineInterface::Nested(map) = iface {
if let Some((full_name, subcmd)) = map.find_command(cmd) {
iface = subcmd;
if !prefix.is_empty() { prefix.push(' '); }
if !prefix.is_empty() {
prefix.push(' ');
}
prefix.push_str(&full_name);
continue;
}
@ -203,7 +222,10 @@ pub fn print_help(
println!("Usage:\n\n{}", generate_nested_usage(&prefix, map, format));
}
CommandLineInterface::Simple(cli_cmd) => {
println!("Usage: {}", generate_usage_str(&prefix, cli_cmd, format, ""));
println!(
"Usage: {}",
generate_usage_str(&prefix, cli_cmd, format, "")
);
}
}
}

View File

@ -54,12 +54,11 @@ fn parse_argument(arg: &str) -> RawArgument {
/// parse as many arguments as possible into a Vec<String, String>. This does not
/// verify the schema.
/// Returns parsed data and the rest as separate array
pub (crate) fn parse_argument_list<T: AsRef<str>>(
pub(crate) fn parse_argument_list<T: AsRef<str>>(
args: &[T],
schema: &ObjectSchema,
errors: &mut ParameterError,
) -> (Vec<(String, String)>, Vec<String>) {
let mut data: Vec<(String, String)> = vec![];
let mut rest: Vec<String> = vec![];
@ -107,16 +106,21 @@ pub (crate) fn parse_argument_list<T: AsRef<str>>(
} else if can_default {
data.push((name, "true".to_string()));
} else {
errors.push(format_err!("parameter '{}': {}", name,
"missing boolean value."));
errors.push(format_err!(
"parameter '{}': {}",
name,
"missing boolean value."
));
}
} else if next_is_argument {
pos += 1;
data.push((name, args[pos].as_ref().to_string()));
} else {
errors.push(format_err!("parameter '{}': {}", name,
"missing parameter value."));
errors.push(format_err!(
"parameter '{}': {}",
name,
"missing parameter value."
));
}
}
Some(v) => {
@ -158,7 +162,7 @@ pub fn parse_arguments<T: AsRef<str>>(
for i in 0..arg_param.len() {
let name = arg_param[i];
if let Some((optional, param_schema)) = schema.lookup(&name) {
if i == arg_param.len() -1 {
if i == arg_param.len() - 1 {
last_arg_param_is_optional = optional;
if let Schema::Array(_) = param_schema {
last_arg_param_is_array = true;
@ -174,7 +178,6 @@ pub fn parse_arguments<T: AsRef<str>>(
let (mut data, mut rest) = parse_argument_list(args, schema, &mut errors);
for i in 0..arg_param.len() {
let name = arg_param[i];
let is_last_arg_param = i == (arg_param.len() - 1);
@ -203,10 +206,9 @@ pub fn parse_arguments<T: AsRef<str>>(
#[test]
fn test_boolean_arg() {
const PARAMETERS: ObjectSchema = ObjectSchema::new(
"Parameters:",
&[ ("enable", false, &BooleanSchema::new("Enable").schema()) ],
&[("enable", false, &BooleanSchema::new("Enable").schema())],
);
let mut variants: Vec<(Vec<&str>, bool)> = vec![];
@ -235,7 +237,6 @@ fn test_boolean_arg() {
#[test]
fn test_argument_paramenter() {
const PARAMETERS: ObjectSchema = ObjectSchema::new(
"Parameters:",
&[

View File

@ -12,9 +12,10 @@ pub struct CliHelper {
}
impl CliHelper {
pub fn new(cmd_def: CommandLineInterface) -> Self {
Self { cmd_def: Arc::new(cmd_def) }
Self {
cmd_def: Arc::new(cmd_def),
}
}
pub fn cmd_def(&self) -> Arc<CommandLineInterface> {
@ -31,7 +32,6 @@ impl rustyline::completion::Completer for CliHelper {
pos: usize,
_ctx: &rustyline::Context<'_>,
) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
let line = &line[..pos];
let (start, completions) = super::get_completions(&*self.cmd_def, line, false);

View File

@ -3,7 +3,6 @@ use failure::*;
/// Shell quote type
pub use rustyline::completion::Quote;
#[derive(PartialEq)]
enum ParseMode {
Space,
@ -24,8 +23,10 @@ enum ParseMode {
/// removed). If there are unclosed quotes, the start of that
/// parameter, the parameter value (unescaped and quotes removed), and
/// the quote type are returned.
pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option<(usize, String, Quote)>) {
pub fn shellword_split_unclosed(
s: &str,
finalize: bool,
) -> (Vec<String>, Option<(usize, String, Quote)>) {
let char_indices = s.char_indices();
let mut args: Vec<String> = Vec::new();
let mut field_start = None;
@ -55,7 +56,7 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
field_start = Some((index, Quote::None));
field.push(c);
}
}
},
ParseMode::EscapeNormal => {
mode = ParseMode::Normal;
field.push(c);
@ -81,17 +82,17 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
args.push(field.split_off(0));
}
c => field.push(c), // continue
}
},
ParseMode::DoubleQuote => match c {
'"' => mode = ParseMode::Normal,
'\\' => mode = ParseMode::EscapeInDoubleQuote,
c => field.push(c), // continue
}
},
ParseMode::SingleQuote => match c {
// Note: no escape in single quotes
'\'' => mode = ParseMode::Normal,
c => field.push(c), // continue
}
},
}
}
@ -101,12 +102,8 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
}
match field_start {
Some ((start, quote)) => {
(args, Some((start, field, quote)))
}
None => {
(args, None)
}
Some((start, quote)) => (args, Some((start, field, quote))),
None => (args, None),
}
}
@ -114,7 +111,6 @@ pub fn shellword_split_unclosed(s: &str, finalize: bool) -> (Vec<String>, Option
///
/// Return words unescaped and without quotes.
pub fn shellword_split(s: &str) -> Result<Vec<String>, Error> {
let (args, unclosed_field) = shellword_split_unclosed(s, true);
if !unclosed_field.is_none() {
bail!("shellword split failed - found unclosed quote.");
@ -124,8 +120,7 @@ pub fn shellword_split(s: &str) -> Result<Vec<String>, Error> {
#[test]
fn test_shellword_split() {
let expect = [ "ls", "/etc" ];
let expect = ["ls", "/etc"];
let expect: Vec<String> = expect.iter().map(|v| v.to_string()).collect();
assert_eq!(expect, shellword_split("ls /etc").unwrap());
@ -137,27 +132,32 @@ fn test_shellword_split() {
assert_eq!(expect, shellword_split("ls /e'tc'").unwrap());
assert_eq!(expect, shellword_split("ls /e't''c'").unwrap());
let expect = [ "ls", "/etc 08x" ];
let expect = ["ls", "/etc 08x"];
let expect: Vec<String> = expect.iter().map(|v| v.to_string()).collect();
assert_eq!(expect, shellword_split("ls /etc\\ \\08x").unwrap());
let expect = [ "ls", "/etc \\08x" ];
let expect = ["ls", "/etc \\08x"];
let expect: Vec<String> = expect.iter().map(|v| v.to_string()).collect();
assert_eq!(expect, shellword_split("ls \"/etc \\08x\"").unwrap());
}
#[test]
fn test_shellword_split_unclosed() {
let expect = [ "ls".to_string() ].to_vec();
let expect = ["ls".to_string()].to_vec();
assert_eq!(
(expect, Some((3, "./File1 name with spaces".to_string(), Quote::Single))),
(
expect,
Some((3, "./File1 name with spaces".to_string(), Quote::Single))
),
shellword_split_unclosed("ls './File1 name with spaces", false)
);
let expect = [ "ls".to_string() ].to_vec();
let expect = ["ls".to_string()].to_vec();
assert_eq!(
(expect, Some((3, "./File2 name with spaces".to_string(), Quote::Double))),
(
expect,
Some((3, "./File2 name with spaces".to_string(), Quote::Double))
),
shellword_split_unclosed("ls \"./File2 \"name\" with spaces", false)
);
}