Switch to a command-line API closer to TaskWarrior
* Use a parser (rather than clap) to process the command line * Outline some generic support for filtering, reporting, modifying, etc. * Break argument parsing strictly from invocation, to allow independent testing
This commit is contained in:
@@ -1,44 +1,8 @@
|
||||
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() {
|
||||
env_logger::init();
|
||||
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)
|
||||
pub fn main() {
|
||||
if let Err(err) = taskchampion_cli::main() {
|
||||
eprintln!("{}", err);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user