use std::ffi::OsString;
use clap::{Arg, Command};
use qli_ext::{Extension, Group};
pub fn build_group_command(group: &Group) -> Command {
let mut cmd = Command::new(leak_str(&group.name))
.about(group.manifest.description.clone())
.arg_required_else_help(true);
for ext in group.extensions.values() {
cmd = cmd.subcommand(build_extension_command(ext));
}
cmd
}
fn build_extension_command(ext: &Extension) -> Command {
Command::new(leak_str(&ext.name))
.about(describe(ext))
.disable_help_flag(true)
.disable_version_flag(true)
.arg(
Arg::new("args")
.num_args(0..)
.trailing_var_arg(true)
.allow_hyphen_values(true)
.value_parser(clap::value_parser!(OsString))
.help("Arguments forwarded to the extension"),
)
}
fn leak_str(s: &str) -> &'static str {
Box::leak(s.to_owned().into_boxed_str())
}
fn describe(ext: &Extension) -> String {
format!("{}: {}", ext.origin.as_str(), ext.path.display())
}