mod cli;
mod commands;
mod fetch;
mod output;
mod render;
mod source;
use clap::{error::ErrorKind, Parser};
use cli::{Cli, Command, Format};
use commands::{Ctx, EXIT_OK, EXIT_USAGE};
fn main() {
let cli = match Cli::try_parse() {
Ok(cli) => cli,
Err(e) => {
let code = match e.kind() {
ErrorKind::DisplayHelp
| ErrorKind::DisplayVersion
| ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => EXIT_OK,
_ => EXIT_USAGE,
};
let _ = e.print();
std::process::exit(code);
}
};
let ctx = Ctx {
format: Format::resolve(cli.format),
quiet: cli.quiet,
};
let result = match cli.command {
Command::Mic(cmd) => commands::run(cmd, &ctx),
Command::Cal(_) => commands::cal(ctx.quiet),
};
match result {
Ok(code) => std::process::exit(code),
Err(e) => {
if let Some(io) = e.downcast_ref::<std::io::Error>() {
if io.kind() == std::io::ErrorKind::BrokenPipe {
std::process::exit(EXIT_OK);
}
}
eprintln!("error: {e:#}");
std::process::exit(EXIT_USAGE);
}
}
}