Refactor command-line handling into modules per subcommands

This commit is contained in:
Dustin J. Mitchell
2020-11-23 19:33:04 -05:00
parent e0b69a62b1
commit fe4183c3ca
12 changed files with 560 additions and 59 deletions

43
cli/src/bin/task.rs Normal file
View File

@@ -0,0 +1,43 @@
use clap::{Error as ClapError, ErrorKind};
use std::process::exit;
use taskchampion_cli::parse_command_line;
enum Output {
Stdout,
Stderr,
}
use Output::*;
fn bail<E: std::fmt::Display>(err: E, output: Output, code: i32) -> ! {
match output {
Stdout => println!("{}", err),
Stderr => eprintln!("{}", err),
}
exit(code)
}
fn main() {
let command = match parse_command_line(std::env::args_os()) {
Ok(command) => command,
Err(err) => {
match err.downcast::<ClapError>() {
Ok(err) => {
if err.kind == ErrorKind::HelpDisplayed
|| err.kind == ErrorKind::VersionDisplayed
{
// --help and --version go to stdout and succeed
bail(err, Stdout, 0)
} else {
// other clap errors exit with failure
bail(err, Stderr, 1)
}
}
Err(err) => bail(err, Stderr, 1),
}
}
};
if let Err(err) = command.run() {
bail(err, Stderr, 1)
}
}