cli::command: wrap usage errors in a UsageError
So we can distinguish them and show usage output conditionally. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
c7fa487250
commit
4968bc3ac0
|
@ -146,7 +146,9 @@ fn main() {
|
||||||
|
|
||||||
if let Err(err) = run_cli_command(&cmd_def.into()) {
|
if let Err(err) = run_cli_command(&cmd_def.into()) {
|
||||||
eprintln!("Error: {}", err);
|
eprintln!("Error: {}", err);
|
||||||
print_cli_usage();
|
if err.downcast::<UsageError>().is_ok() {
|
||||||
|
print_cli_usage();
|
||||||
|
}
|
||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ use failure::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::api::schema::*;
|
use crate::api::schema::*;
|
||||||
use crate::api::router::*;
|
use crate::api::router::*;
|
||||||
//use crate::api::config::*;
|
//use crate::api::config::*;
|
||||||
|
@ -12,7 +14,13 @@ pub fn print_cli_usage() {
|
||||||
eprintln!("Usage: TODO");
|
eprintln!("Usage: TODO");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<(), Error> {
|
#[derive(Debug, Fail)]
|
||||||
|
#[fail(display = "Usage error: {}", _0)]
|
||||||
|
pub struct UsageError(Error);
|
||||||
|
|
||||||
|
pub struct Invocation<'a>(&'a CliCommand, Value);
|
||||||
|
|
||||||
|
fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<Invocation, Error> {
|
||||||
|
|
||||||
let (params, rest) = getopts::parse_arguments(
|
let (params, rest) = getopts::parse_arguments(
|
||||||
&args, &cli_cmd.arg_param, &cli_cmd.info.parameters)?;
|
&args, &cli_cmd.arg_param, &cli_cmd.info.parameters)?;
|
||||||
|
@ -21,11 +29,7 @@ fn handle_simple_command(cli_cmd: &CliCommand, args: Vec<String>) -> Result<(),
|
||||||
bail!("got additional arguments: {:?}", rest);
|
bail!("got additional arguments: {:?}", rest);
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = (cli_cmd.info.handler)(params, &cli_cmd.info)?;
|
Ok(Invocation(cli_cmd, params))
|
||||||
|
|
||||||
println!("Result: {}", serde_json::to_string_pretty(&res).unwrap());
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLineInterface> {
|
fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLineInterface> {
|
||||||
|
@ -50,7 +54,7 @@ fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLin
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_nested_command(def: &CliCommandMap, mut args: Vec<String>) -> Result<(), Error> {
|
fn handle_nested_command(def: &CliCommandMap, mut args: Vec<String>) -> Result<Invocation, Error> {
|
||||||
|
|
||||||
if args.len() < 1 {
|
if args.len() < 1 {
|
||||||
let mut cmds: Vec<&String> = def.commands.keys().collect();
|
let mut cmds: Vec<&String> = def.commands.keys().collect();
|
||||||
|
@ -74,14 +78,12 @@ fn handle_nested_command(def: &CliCommandMap, mut args: Vec<String>) -> Result<(
|
||||||
|
|
||||||
match sub_cmd {
|
match sub_cmd {
|
||||||
CommandLineInterface::Simple(cli_cmd) => {
|
CommandLineInterface::Simple(cli_cmd) => {
|
||||||
handle_simple_command(cli_cmd, args)?;
|
handle_simple_command(cli_cmd, args)
|
||||||
}
|
}
|
||||||
CommandLineInterface::Nested(map) => {
|
CommandLineInterface::Nested(map) => {
|
||||||
handle_nested_command(map, args)?;
|
handle_nested_command(map, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_property_completion(
|
fn print_property_completion(
|
||||||
|
@ -253,10 +255,20 @@ pub fn run_cli_command(def: &CommandLineInterface) -> Result<(), Error> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
match def {
|
let invocation = match def {
|
||||||
CommandLineInterface::Simple(cli_cmd) => handle_simple_command(cli_cmd, args),
|
CommandLineInterface::Simple(cli_cmd) => handle_simple_command(cli_cmd, args),
|
||||||
CommandLineInterface::Nested(map) => handle_nested_command(map, args),
|
CommandLineInterface::Nested(map) => handle_nested_command(map, args),
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
|
let res = match invocation {
|
||||||
|
Err(e) => return Err(UsageError(e).into()),
|
||||||
|
Ok(invocation) => (invocation.0.info.handler)(invocation.1, &invocation.0.info)?,
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Result: {}", serde_json::to_string_pretty(&res).unwrap());
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CompletionFunction = fn() -> Vec<String>;
|
pub type CompletionFunction = fn() -> Vec<String>;
|
||||||
|
|
Loading…
Reference in New Issue