#[cfg(all(feature = "mimalloc", not(debug_assertions)))]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
fn main() {
let embedder = &aube_util::identity::AUBE;
let code = if is_usage_invocation() {
print_usage_spec(embedder)
} else {
aube::cli_main(embedder)
};
std::process::exit(code);
}
fn print_usage_spec(embedder: &'static aube_util::Embedder) -> i32 {
let mut cmd = aube::command();
let mut usage_spec = clap_usage::spec(&mut cmd, embedder.name);
aube::command_effects::apply(&mut usage_spec);
let mut spec = format!(
"// @generated by usage-cli from clap metadata\nmin_usage_version \"4.0\"\n{usage_spec}\n"
)
.into_bytes();
let extra = include_str!("../assets/extra.usage.kdl")
.replace("{run}", &format!("{} run", embedder.name))
.replace(
"{completers}",
&include_str!("../assets/completion.usage.kdl").replace("{bin}", embedder.name),
);
spec.extend_from_slice(extra.trim_end().as_bytes());
spec.push(b'\n');
use std::io::Write;
let mut out = std::io::stdout();
match out.write_all(&spec).and_then(|()| out.flush()) {
Ok(()) => 0,
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => 0,
Err(e) => {
let code = aube_codes::errors::ERR_AUBE_USAGE_SPEC_WRITE_FAILED;
eprintln!("{}: {code}: failed to write usage spec: {e}", embedder.name);
aube_codes::exit::exit_code_for(code).unwrap_or(aube_codes::exit::EXIT_GENERIC)
}
}
}
fn is_usage_invocation() -> bool {
let mut args = std::env::args_os();
let invoked_as_aube = args
.next()
.as_deref()
.map(std::path::Path::new)
.and_then(|p| p.file_stem())
.and_then(|s| s.to_str())
.map(|stem| {
!matches!(
stem.to_ascii_lowercase().as_str(),
"aubr" | "aubx" | "node" | "npm" | "npx" | "pnpm" | "pnpx" | "yarn" | "yarnpkg"
)
})
.unwrap_or(true);
invoked_as_aube && args.next().as_deref() == Some(std::ffi::OsStr::new("usage"))
}