use anyhow::Result;
use std::path::PathBuf;
use structopt::{clap, StructOpt};
mod cmd;
#[derive(StructOpt)]
#[structopt(name = "MDoc", author = "Knut Magnus Aasrud")]
struct App {
#[structopt(subcommand)]
command: Command,
#[structopt(short = "q", long = "quiet")]
quiet: bool,
#[structopt(short = "d", long = "debug")]
debug: bool,
}
#[derive(Debug, StructOpt)]
enum Command {
Build {
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
#[structopt(short = "o", long = "output")]
output: Option<String>,
},
Clean {
#[structopt(long = "data")]
data: bool,
},
Init {
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},
List,
}
fn run() -> Result<()> {
let app = App::from_args_safe()?;
match (app.debug, app.quiet) {
(false, false) => mdoc::log::set_max_level(3),
(true, _) => mdoc::log::set_max_level(4),
(false, true) => mdoc::log::set_max_level(1),
}
match app.command {
Command::Build { path, output } => cmd::build(path, output)?,
Command::Clean { data } => cmd::clean(data)?,
Command::Init { path } => cmd::init(path)?,
Command::List => mdoc::info!("Listing"),
}
Ok(())
}
fn main() {
if let Err(e) = run() {
match e.downcast_ref::<clap::Error>() {
Some(e) if e.kind == clap::ErrorKind::HelpDisplayed => {
println!("{}", e)
}
Some(e) => {
mdoc::error!(
"{}",
e.to_string()
.trim_start_matches("\u{1b}[1;31merror:\u{1b}[0m ")
);
std::process::exit(1);
}
_ => {
mdoc::error!("{}{}", e, mdoc::log::format_chain(e.chain()));
std::process::exit(1);
}
}
}
}