src/cli/command.rs::allow to generate ReST docu

This commit is contained in:
Dietmar Maurer 2019-02-23 11:29:18 +01:00
parent 2f3f2bb77f
commit 8b6dd2240d
2 changed files with 66 additions and 48 deletions

View File

@ -202,7 +202,7 @@ fn main() {
create_backup, create_backup,
ObjectSchema::new("Create backup.") ObjectSchema::new("Create backup.")
.required("repository", repo_url_schema.clone()) .required("repository", repo_url_schema.clone())
.required("filename", StringSchema::new("Source name (file or directory name)")) .required("filename", StringSchema::new("Source name (file or directory name)."))
.required("target", StringSchema::new("Target name.")) .required("target", StringSchema::new("Target name."))
.optional( .optional(
"chunk-size", "chunk-size",

View File

@ -27,8 +27,8 @@ enum DocumentationFormat {
Long, Long,
/// text, include description /// text, include description
Full, Full,
/// like full, but in pandoc json format /// like full, but in reStructuredText format
Pandoc, ReST,
} }
fn get_schema_type_text(schema: &Schema, _style: ParameterDisplayStyle) -> String { fn get_schema_type_text(schema: &Schema, _style: ParameterDisplayStyle) -> String {
@ -63,8 +63,22 @@ fn get_property_description(
Schema::Array(ref schema) => (schema.description, None), Schema::Array(ref schema) => (schema.description, None),
}; };
if format == DocumentationFormat::Pandoc { if format == DocumentationFormat::ReST {
panic!("implement me");
let mut text = match style {
ParameterDisplayStyle::Arg => {
format!(":``--{} {}``: ", name, type_text)
}
ParameterDisplayStyle::Fixed => {
format!(":``<{}> {}``: ", name, type_text)
}
};
text.push_str(descr);
text.push('\n');
text.push('\n');
text
} else { } else {
@ -141,7 +155,7 @@ fn generate_usage_str(
if *optional { if *optional {
options.push(' '); if options.len() > 0 { options.push(' '); }
options.push_str(&get_property_description(prop, &schema, ParameterDisplayStyle::Arg, format)); options.push_str(&get_property_description(prop, &schema, ParameterDisplayStyle::Arg, format));
} else { } else {
@ -153,24 +167,21 @@ fn generate_usage_str(
done_hash.insert(prop); done_hash.insert(prop);
} }
match format { let mut text = match format {
DocumentationFormat::Short => { DocumentationFormat::Short => {
format!("{}{}{}", indent, prefix, args) return format!("{}{}{}", indent, prefix, args);
} }
DocumentationFormat::Long => { DocumentationFormat::Long => {
let mut text = format!("{}{}{}\n", indent, prefix, args); format!("{}{}{}\n", indent, prefix, args)
if arg_descr.len() > 0 {
text.push('\n');
text.push_str(&arg_descr);
}
if options.len() > 0 {
text.push('\n');
text.push_str(&options);
}
text
} }
DocumentationFormat::Full => { DocumentationFormat::Full => {
let mut text = format!("{}{}{}\n\n{}\n", indent, prefix, args, description); format!("{}{}{}\n\n{}\n", indent, prefix, args, description)
}
DocumentationFormat::ReST => {
format!("``{} {}``\n\n{}\n\n", prefix, args.trim(), description)
}
};
if arg_descr.len() > 0 { if arg_descr.len() > 0 {
text.push('\n'); text.push('\n');
text.push_str(&arg_descr); text.push_str(&arg_descr);
@ -180,24 +191,12 @@ fn generate_usage_str(
text.push_str(&options); text.push_str(&options);
} }
text text
}
DocumentationFormat::Pandoc => {
panic!("implement me");
}
}
} }
fn print_simple_usage_error(prefix: &str, cli_cmd: &CliCommand, err: Error) { fn print_simple_usage_error(prefix: &str, cli_cmd: &CliCommand, err: Error) {
eprint!("Error: {}\nUsage: ", err);
print_simple_usage(prefix, cli_cmd);
}
fn print_simple_usage(prefix: &str, cli_cmd: &CliCommand) {
let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, ""); let usage = generate_usage_str(prefix, cli_cmd, DocumentationFormat::Long, "");
eprintln!("{}", usage); eprint!("Error: {}\nUsage: {}", err, usage);
} }
fn handle_simple_command(prefix: &str, cli_cmd: &CliCommand, args: Vec<String>) { fn handle_simple_command(prefix: &str, cli_cmd: &CliCommand, args: Vec<String>) {
@ -253,31 +252,35 @@ fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLin
fn print_nested_usage_error(prefix: &str, def: &CliCommandMap, err: Error) { fn print_nested_usage_error(prefix: &str, def: &CliCommandMap, err: Error) {
eprintln!("Error: {}\n\nUsage:\n", err); let usage = generate_nested_usage(prefix, def, DocumentationFormat::Long);
// print_nested_usage(prefix, def, DocumentationFormat::Short); eprintln!("Error: {}\n\nUsage:\n{}", err, usage);
print_nested_usage(prefix, def, DocumentationFormat::Long);
} }
fn print_nested_usage(prefix: &str, def: &CliCommandMap, format: DocumentationFormat) { fn generate_nested_usage(prefix: &str, def: &CliCommandMap, format: DocumentationFormat) -> String {
let mut cmds: Vec<&String> = def.commands.keys().collect(); let mut cmds: Vec<&String> = def.commands.keys().collect();
cmds.sort(); cmds.sort();
let mut usage = String::new();
for cmd in cmds { for cmd in cmds {
let new_prefix = format!("{} {}", prefix, cmd); let new_prefix = format!("{} {}", prefix, cmd);
match def.commands.get(cmd).unwrap() { match def.commands.get(cmd).unwrap() {
CommandLineInterface::Simple(cli_cmd) => { CommandLineInterface::Simple(cli_cmd) => {
let usage = generate_usage_str(&new_prefix, cli_cmd, format, ""); if usage.len() > 0 && format == DocumentationFormat::ReST {
eprintln!("{}", usage); usage.push_str("----\n\n");
}
usage.push_str(&generate_usage_str(&new_prefix, cli_cmd, format, ""));
} }
CommandLineInterface::Nested(map) => { CommandLineInterface::Nested(map) => {
print_nested_usage(&new_prefix, map, format); usage.push_str(&generate_nested_usage(&new_prefix, map, format));
} }
} }
} }
usage
} }
fn handle_nested_command(prefix: &str, def: &CliCommandMap, mut args: Vec<String>) { fn handle_nested_command(prefix: &str, def: &CliCommandMap, mut args: Vec<String>) {
@ -489,11 +492,26 @@ pub fn run_cli_command(def: &CommandLineInterface) {
let args: Vec<String> = args.collect(); let args: Vec<String> = args.collect();
if !args.is_empty() && args[0] == "bashcomplete" { if !args.is_empty() {
if args[0] == "bashcomplete" {
print_bash_completion(def); print_bash_completion(def);
return; return;
} }
if args[0] == "printdoc" {
let usage = match def {
CommandLineInterface::Simple(cli_cmd) => {
generate_usage_str(&prefix, cli_cmd, DocumentationFormat::ReST, "")
}
CommandLineInterface::Nested(map) => {
generate_nested_usage(&prefix, map, DocumentationFormat::ReST)
}
};
println!("{}", usage);
return;
}
}
match def { match def {
CommandLineInterface::Simple(cli_cmd) => handle_simple_command(&prefix, cli_cmd, args), CommandLineInterface::Simple(cli_cmd) => handle_simple_command(&prefix, cli_cmd, args),
CommandLineInterface::Nested(map) => handle_nested_command(&prefix, map, args), CommandLineInterface::Nested(map) => handle_nested_command(&prefix, map, args),