#[cfg(feature = "yaml")]
#[macro_export]
macro_rules! load_yaml {
($yaml:expr) => {
&$crate::YamlLoader::load_from_str(include_str!($yaml)).expect("failed to load YAML file")
[0]
};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! crate_license {
() => {{
let mut license = env!("CARGO_PKG_LICENSE");
if license.is_empty() {
license = env!("CARGO_PKG_LICENSE_FILE");
}
if license.is_empty() {
license = "";
}
license
}};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! crate_version {
() => {
env!("CARGO_PKG_VERSION")
};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! crate_authors {
($sep:expr) => {{
clap::lazy_static::lazy_static! {
static ref CACHED: String = env!("CARGO_PKG_AUTHORS").replace(':', $sep);
}
let s: &'static str = &*CACHED;
s
}};
() => {
env!("CARGO_PKG_AUTHORS")
};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! crate_description {
() => {
env!("CARGO_PKG_DESCRIPTION")
};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! crate_name {
() => {
env!("CARGO_PKG_NAME")
};
}
#[cfg(feature = "cargo")]
#[macro_export]
macro_rules! app_from_crate {
() => {
$crate::App::new($crate::crate_name!())
.version($crate::crate_version!())
.author($crate::crate_authors!())
.about($crate::crate_description!())
.license($crate::crate_license!())
};
($sep:expr) => {
$crate::App::new($crate::crate_name!())
.version($crate::crate_version!())
.author($crate::crate_authors!($sep))
.about($crate::crate_description!())
.license($crate::crate_license!())
};
}
#[macro_export]
macro_rules! clap_app {
(@app ($builder:expr)) => { $builder };
(@app ($builder:expr) (@arg ($name:expr): $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.arg(
$crate::clap_app!{ @arg ($crate::Arg::new($name)) (-) $($tail)* }))
$($tt)*
}
};
(@app ($builder:expr) (@arg $name:literal: $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.arg(
$crate::clap_app!{ @arg ($crate::Arg::new(stringify!($name).trim_matches('"'))) (-) $($tail)* }))
$($tt)*
}
};
(@app ($builder:expr) (@arg $name:ident: $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.arg(
$crate::clap_app!{ @arg ($crate::Arg::new(stringify!($name))) (-) $($tail)* }))
$($tt)*
}
};
(@app ($builder:expr) (@global_setting $setting:ident) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.global_setting($crate::AppSettings::$setting))
$($tt)*
}
};
(@app ($builder:expr) (@setting $setting:ident) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.setting($crate::AppSettings::$setting))
$($tt)*
}
};
(@app ($builder:expr) (@attributes $($attr:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app ($crate::clap_app!{ @arg ($builder) $($attr)* }) $($tt)* }
};
(@app ($builder:expr) (@group $name:ident: $($attrs:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($crate::clap_app!{ @group ($builder, $crate::ArgGroup::new(stringify!($name))) $($attrs)* })
$($tt)*
}
};
(@app ($builder:expr) (@subcommand ($name:expr) => $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.subcommand(
$crate::clap_app!{ @app ($crate::App::new($name)) $($tail)* }
))
$($tt)*
}
};
(@app ($builder:expr) (@subcommand $name:literal => $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.subcommand(
$crate::clap_app!{ @app ($crate::App::new(stringify!($name).trim_matches('"'))) $($tail)* }
))
$($tt)*
}
};
(@app ($builder:expr) (@subcommand $name:ident => $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.subcommand(
$crate::clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)* }
))
$($tt)*
}
};
(@app ($builder:expr) ($ident:ident: $($v:expr),*) $($tt:tt)*) => {
$crate::clap_app!{ @app
($builder.$ident($($v),*))
$($tt)*
}
};
(@group ($builder:expr, $group:expr)) => { $builder.group($group) };
(@group ($builder:expr, $group:expr) (@attributes $($attr:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @group ($builder, $group) $($attr)* $($tt)* }
};
(@group ($builder:expr, $group:expr) (@arg ($name:expr): $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @group
($crate::clap_app!{ @app ($builder) (@arg ($name): $($tail)*) },
$group.arg($name))
$($tt)*
}
};
(@group ($builder:expr, $group:expr) (@arg $name:literal: $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @group
($crate::clap_app!{ @app ($builder) (@arg $name: $($tail)*) },
$group.arg(stringify!($name).trim_matches('"')))
$($tt)*
}
};
(@group ($builder:expr, $group:expr) (@arg $name:ident: $($tail:tt)*) $($tt:tt)*) => {
$crate::clap_app!{ @group
($crate::clap_app!{ @app ($builder) (@arg $name: $($tail)*) },
$group.arg(stringify!($name)))
$($tt)*
}
};
(@group ($builder:expr, $group:expr) !$ident:ident $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group.$ident(false)) $($tail)* }
};
(@group ($builder:expr, $group:expr) +$ident:ident $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group.$ident(true)) $($tail)* }
};
(@group ($builder:expr, $group:expr) * $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group) +required $($tail)* }
};
(@group ($builder:expr, $group:expr) ... $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group) +multiple $($tail)* }
};
(@group ($builder:expr, $group:expr) $ident:ident[$($target:literal)*] $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group $( .$ident(stringify!($target).trim_matches('"')) )*) $($tail)* }
};
(@group ($builder:expr, $group:expr) $ident:ident[$($target:ident)*] $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group $( .$ident(stringify!($target)) )*) $($tail)* }
};
(@group ($builder:expr, $group:expr) $ident:ident($($expr:expr),*) $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group.$ident($($expr),*)) $($tail)* }
};
(@group ($builder:expr, $group:expr) $ident:ident($($expr:expr,)*) $($tail:tt)*) => {
$crate::clap_app!{ @group ($builder, $group.$ident($($expr),*)) $($tail)* }
};
(@arg ($arg:expr) $modes:tt) => { $arg };
(@arg ($arg:expr) $modes:tt --($long:expr) $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.long($long)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt --$long:literal $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.long(stringify!($long).trim_matches('"'))) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt --$long:ident $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.long(stringify!($long))) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt -($short:expr) $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.short($short)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt -$short:literal $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.short($short.to_string().chars().next().expect(r#""" is not allowed here"#))) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt -$short:ident $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.short(stringify!($short).chars().next().unwrap())) $modes $($tail)* }
};
(@arg ($arg:expr) (-) <$var:ident> $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) +takes_value +required $($tail)* }
};
(@arg ($arg:expr) (+) <$var:ident> $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) $($tail)* }
};
(@arg ($arg:expr) (-) [$var:ident] $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) +takes_value $($tail)* }
};
(@arg ($arg:expr) (+) [$var:ident] $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) $($tail)* }
};
(@arg ($arg:expr) $modes:tt ... $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg) $modes +multiple $($tail)* }
};
(@arg ($arg:expr) $modes:tt #{$n:expr, $m:expr} $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg) $modes min_values($n) max_values($m) $($tail)* }
};
(@arg ($arg:expr) $modes:tt * $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg) $modes +required $($tail)* }
};
(@arg ($arg:expr) $modes:tt !$ident:ident $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.$ident(false)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt +$ident:ident $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.$ident(true)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt {$fn_:expr} $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.validator($fn_)) $modes $($tail)* }
};
(@as_expr $expr:expr) => { $expr };
(@arg ($arg:expr) $modes:tt $desc:tt) => { $arg.about(clap_app!{ @as_expr $desc }) };
(@arg ($arg:expr) $modes:tt $ident:ident[$($target:literal)*] $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg $( .$ident(stringify!($target).trim_matches('"')) )*) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt $ident:ident[$($target:ident)*] $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg $( .$ident(stringify!($target)) )*) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr),*) $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr,)*) $($tail:tt)*) => {
$crate::clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
};
(@subcommand ($name:expr) => $($tail:tt)*) => {
$crate::clap_app!{ @app ($crate::App::new($name)) $($tail)* }
};
(@subcommand $name:literal => $($tail:tt)*) => {
$crate::clap_app!{ @app ($crate::App::new(stringify!($name).trim_matches('"'))) $($tail)* }
};
(@subcommand $name:ident => $($tail:tt)*) => {
$crate::clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)* }
};
(($name:expr) => $($tail:tt)*) => {{
$crate::clap_app!{ @app ($crate::App::new($name)) $($tail)*}
}};
($name:literal => $($tail:tt)*) => {{
$crate::clap_app!{ @app ($crate::App::new(stringify!($name).trim_matches('"'))) $($tail)*}
}};
($name:ident => $($tail:tt)*) => {{
$crate::clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)*}
}};
}
macro_rules! impl_settings {
($settings:ident, $flags:ident,
$( $setting:ident($str:expr) => $flag:path ),+
) => {
impl $flags {
pub(crate) fn set(&mut self, s: $settings) {
match s {
$($settings::$setting => self.0.insert($flag)),*
}
}
pub(crate) fn unset(&mut self, s: $settings) {
match s {
$($settings::$setting => self.0.remove($flag)),*
}
}
pub(crate) fn is_set(&self, s: $settings) -> bool {
match s {
$($settings::$setting => self.0.contains($flag)),*
}
}
}
impl FromStr for $settings {
type Err = String;
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
match &*s.to_ascii_lowercase() {
$( $str => Ok($settings::$setting), )*
_ => Err(format!("unknown AppSetting: `{}`", s)),
}
}
}
}
}
macro_rules! wlnerr {
($($arg:tt)*) => ({
use std::io::{Write, stderr};
writeln!(&mut stderr(), $($arg)*).ok();
})
}
#[cfg(feature = "debug")]
macro_rules! debug {
($($arg:tt)*) => {
print!("[{:>w$}] \t", module_path!(), w = 28);
println!($($arg)*)
}
}
#[cfg(not(feature = "debug"))]
macro_rules! debug {
($($arg:tt)*) => {};
}