use std::process::ExitCode;
use clap::Parser;
use lore::cli::Cli;
use lore::console;
const USAGE: u8 = 2;
fn main() -> ExitCode {
let cli = match Cli::try_parse() {
Ok(cli) => cli,
Err(error) if error.use_stderr() => {
console::report(&error.render().to_string());
return ExitCode::from(USAGE);
}
Err(help) => {
let _ = help.print();
return ExitCode::SUCCESS;
}
};
match cli.run() {
Ok(()) => ExitCode::SUCCESS,
Err(error) if is_broken_pipe(&error) => ExitCode::SUCCESS,
Err(error) => {
console::report(&format!("lore: {error:#}\n"));
ExitCode::FAILURE
}
}
}
fn is_broken_pipe(error: &anyhow::Error) -> bool {
error.chain().any(|cause| {
cause
.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() == std::io::ErrorKind::BrokenPipe)
})
}