You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.8 KiB

//! Handles arg parsing
use super::*;
use std::fmt;
/// The name of the process that was invoked
#[inline] pub fn prog_name() -> &'static str
{
lazy_static! {
static ref NAME: String = std::env::args().next().unwrap();
}
&NAME[..]
}
#[inline] fn options() -> impl Iterator<Item = impl fmt::Display>
{
struct Opt(&'static [&'static str], Option<&'static str>, &'static str, usize, Option<&'static str>, Option<&'static [&'static str]>);
impl Opt
{
const fn with_def(self, vl: &'static str) -> Self
{
Self(self.0, self.1, self.2, self.3, Some(vl), self.5)
}
const fn with_slice(self, vl: &'static [&'static str]) -> Self
{
Self(self.0, self.1, self.2, self.3, self.4, Some(vl))
}
}
/// Macro for printing the options
///
/// # Usage
/// Any combination of the following:
/// ```
/// opt!("--long", "--other"; "Description"); // --long, --other: Description
/// opt!("--long", "--other"; * 2 "Description"); // --long, --other: Description
/// opt!("--long", "--other" => "value name"; "Description"); // --long, --other <value name>: Description
/// opt!("--long"; "Description", ""); // --long, --other: Description (default)
/// opt!("--long"; "Description", "Value"); // --long, --other: Description (default: `Value`)
/// opt!("--long"; "Description", ["OTHER", "OTHER 2"], "value"); // --long, --other: Description (see `OTHER`, `OTHER 2`) (default: `value`)
/// ```
macro_rules! opt {
($($name:literal),* => $num:literal; $desc:literal $(, [$($see:literal),*])? $(, $def:literal)?) => {
{
let o = Opt(&[$($name),*], Some($num), $desc, 1, None, None);
$(let o = o.with_def($def);)?
$(let o = o.with_slice(&[$($see),*]);)?
o
}
};
($($name:literal),* => $num:literal; * $tabs:literal $desc:literal $(, [$($see:literal),*])? $(, $def:literal)?) => {
{
let o = Opt(&[$($name),*], Some($num), $desc, $tabs, None, None);
$(let o = o.with_def($def);)?
$(let o = o.with_slice(&[$($see),*]);)?
o
}
};
($($name:literal),*; $desc:literal $(, [$($see:literal),*])? $(, $def:literal)?) => {
{
let o = Opt(&[$($name),*], None, $desc, 1, None, None);
$(let o = o.with_def($def);)?
$(let o = o.with_slice(&[$($see),*]);)?
o
}
};
($($name:literal),*; * $tabs:literal $desc:literal $(, [$($see:literal),*])? $(, $def:literal)?) => {
{
let o = Opt(&[$($name),*], None, $desc, $tabs, None, None);
$(let o = o.with_def($def);)?
$(let o = o.with_slice(&[$($see),*]);)?
o
}
};
}
impl fmt::Display for Opt
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, " {}", self.0.iter().join(", "))?;
if let Some(mx) = self.1 {
use recolored::Colorize;
write!(f, " <{}>", mx.italic())?;
}
write!(f, ":")?;
for ch in std::iter::repeat('\t').take(self.3)
{
use std::fmt::Write;
f.write_char(ch)?;
}
write!(f,"{}", self.2)?;
{
use recolored::Colorize;
match self.5 {
Some(&[]) => (),
Some(strings) => {
write!(f, " (see {})", strings.iter().map(|x| iter!["`".clear(), x.underline(), "`".clear()]).flat_join(", "))?;
}
_ => (),
}
match self.4 {
Some("") => {
write!(f, " ({})", "default".bold().bright_red())?;
},
Some(string) => {
write!(f, " ({}: `{}`)", "default".bold().bright_red(), string.bright_blue())?;
},
None => (),
}
}
Ok(())
}
}
iter![
opt!("--limit", "-l" => "max children"; "Limit the number of child processes running at once."),
opt!("--auto-limit", "-m"; * 2 "Limit to max number of CPUs + 1", ""),
opt!("--unlimit", "-U"; * 3 "No limit to number of processes spawned at once"),
opt!("--completion", "-c" => "action"; "Set the default action on completion", ["ACTIONS", "COMPLETION"], "ignore"),
opt!("--silent", "-s"; * 3 "Output no messages other than that of child processes"),
opt!("--stdout" => "where"; * 2 "Set default redirect option for childrens' `stdout`", ["ACTIONS"] ,"inherit"),
opt!("--stderr" => "where"; * 2 "Set default redirect option for childrens' `stderr`", ["ACTIONS"], "inherit"),
opt!("--stdin" => "where"; * 2 "Set default redirect option for childrens' `stdin`", ["ACTIONS"], "close")
]
}
/// Print usage
pub fn print_usage()
{
use recolored::Colorize;
println!(r#"batchtask v{version} - advanced process batching
by {author} with <3 (licensed GPL3+)"#,
version = env!("CARGO_PKG_VERSION"),
author = env!("CARGO_PKG_AUTHORS"));
println!(r#"
Usage: {prog} [OPTIONS]...
Usage: {prog} --help
[{OPTIONS}]:
{opts}
"#,
prog = prog_name(),
OPTIONS = "OPTIONS".bright_blue().bold(),
opts = options().map(|x| lazy_format!("{}", x)).join("\n"));
}
/// Print usage then exit
#[inline(always)] pub fn usage() -> !
{
print_usage();
std::process::exit(1)
}