macro_rules! wlnerr(
($($arg:tt)*) => ({
use std::io::{Write, stderr};
writeln!(&mut stderr(), $($arg)*).ok();
})
);
macro_rules! werr(
($($arg:tt)*) => ({
use std::io::{Write, stderr};
write!(&mut stderr(), $($arg)*).ok();
})
);
#[cfg(feature = "debug")]
macro_rules! debugln {
($fmt:expr) => (println!(concat!("**DEBUG** ", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("**DEBUG** ",$fmt), $($arg)*));
}
#[cfg(feature = "debug")]
macro_rules! debug {
($fmt:expr) => (print!(concat!("**DEBUG** ", $fmt)));
($fmt:expr, $($arg:tt)*) => (println!(concat!("**DEBUG** ",$fmt), $($arg)*));
}
#[cfg(not(feature = "debug"))]
macro_rules! debugln {
($fmt:expr) => ();
($fmt:expr, $($arg:tt)*) => ();
}
#[cfg(not(feature = "debug"))]
macro_rules! debug {
($fmt:expr) => ();
($fmt:expr, $($arg:tt)*) => ();
}
#[cfg(feature = "yaml")]
#[macro_export]
macro_rules! load_yaml {
($yml:expr) => (
&::clap::YamlLoader::load_from_str(include_str!($yml)).ok().expect("failed to load YAML file")[0]
);
}
macro_rules! print_opt_help {
($opt:ident, $spc:expr, $w:ident) => {
if let Some(h) = $opt.help {
if h.contains("{n}") {
let mut hel = h.split("{n}");
if let Some(part) = hel.next() {
try!(write!($w, "{}", part));
}
for part in hel {
try!(write!($w, "\n"));
write_spaces!($spc, $w);
try!(write!($w, "{}", part));
}
} else {
try!(write!($w, "{}", h));
}
if let Some(ref pv) = $opt.possible_vals {
try!(write!($w, " [values:"));
for pv_s in pv.iter() {
try!(write!($w, " {}", pv_s));
}
try!(write!($w, "]"));
}
}
};
}
macro_rules! write_spaces {
($num:expr, $w:ident) => ({
for _ in 0..$num {
try!(write!($w, " "));
}
})
}
macro_rules! vec_remove {
($vec:expr, $to_rem:ident) => {
{
let mut ix = None;
$vec.dedup();
for (i, val) in $vec.iter().enumerate() {
if &val == &$to_rem {
ix = Some(i);
break;
}
}
if let Some(i) = ix {
$vec.remove(i);
}
}
}
}
macro_rules! for_match {
($it:ident, $($p:pat => $($e:expr);+),*) => {
for i in $it {
match i {
$(
$p => { $($e)+ }
)*
}
}
};
}
#[macro_export]
macro_rules! value_t {
($m:ident.value_of($v:expr), $t:ty) => {
match $m.value_of($v) {
Some(v) => {
match v.parse::<$t>() {
Ok(val) => Ok(val),
Err(_) => Err(format!("'{}' isn't a valid value", ::clap::Format::Warning(v))),
}
},
None => Err(format!("The argument '{}' not found", ::clap::Format::Warning($v)))
}
};
($m:ident.values_of($v:expr), $t:ty) => {
match $m.values_of($v) {
Some(ref v) => {
let mut tmp = Vec::with_capacity(v.len());
let mut err = None;
for pv in v {
match pv.parse::<$t>() {
Ok(rv) => tmp.push(rv),
Err(e) => {
err = Some(format!("'{}' isn't a valid value\n\t{}", ::clap::Format::Warning(pv),e));
break
}
}
}
match err {
Some(e) => Err(e),
None => Ok(tmp)
}
},
None => Err(format!("The argument '{}' was not found", ::clap::Format::Warning($v)))
}
};
}
#[macro_export]
macro_rules! value_t_or_exit {
($m:ident.value_of($v:expr), $t:ty) => {
match $m.value_of($v) {
Some(v) => {
match v.parse::<$t>() {
Ok(val) => val,
Err(..) => {
println!("{} '{}' isn't a valid value\n\n{}\n\nPlease re-run with {} for \
more information",
::clap::Format::Error("error:"),
::clap::Format::Warning(v.to_string()),
$m.usage(),
::clap::Format::Good("--help"));
::std::process::exit(1);
}
}
},
None => {
println!("{} The argument '{}' was not found or is not valid\n\n{}\n\nPlease re-run with \
{} for more information",
::clap::Format::Error("error:"),
::clap::Format::Warning($v.to_string()),
$m.usage(),
::clap::Format::Good("--help"));
::std::process::exit(1);
}
}
};
($m:ident.values_of($v:expr), $t:ty) => {
match $m.values_of($v) {
Some(ref v) => {
let mut tmp = Vec::with_capacity(v.len());
for pv in v {
match pv.parse::<$t>() {
Ok(rv) => tmp.push(rv),
Err(_) => {
println!("{} '{}' isn't a valid value\n\n{}\n\nPlease re-run with {} for more \
information",
::clap::Format::Error("error:"),
::clap::Format::Warning(pv),
$m.usage(),
::clap::Format::Good("--help"));
::std::process::exit(1);
}
}
}
tmp
},
None => {
println!("{} The argument '{}' not found or is not valid\n\n{}\n\nPlease re-run with \
{} for more information",
::clap::Format::Error("error:"),
::clap::Format::Warning($v.to_string()),
$m.usage(),
::clap::Format::Good("--help"));
::std::process::exit(1);
}
}
};
}
#[macro_export]
macro_rules! simple_enum {
($e:ident => $($v:ident),+) => {
enum $e {
$($v),+
}
impl ::std::str::FromStr for $e {
type Err = String;
fn from_str(s: &str) -> Result<Self,Self::Err> {
match s {
$(stringify!($v) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
})
}
}
}
impl ::std::fmt::Display for $e {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$($e::$v => write!(f, stringify!($v)),)+
}
}
}
impl $e {
#[allow(dead_code)]
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
}
#[macro_export]
macro_rules! arg_enum {
(enum $e:ident { $($v:ident),+ } ) => {
enum $e {
$($v),+
}
impl ::std::str::FromStr for $e {
type Err = String;
fn from_str(s: &str) -> Result<Self,Self::Err> {
use ::std::ascii::AsciiExt;
match s {
$(stringify!($v) |
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
})
}
}
}
impl ::std::fmt::Display for $e {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$($e::$v => write!(f, stringify!($v)),)+
}
}
}
impl $e {
#[allow(dead_code)]
fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(pub enum $e:ident { $($v:ident),+ } ) => {
pub enum $e {
$($v),+
}
impl ::std::str::FromStr for $e {
type Err = String;
fn from_str(s: &str) -> Result<Self,Self::Err> {
use ::std::ascii::AsciiExt;
match s {
$(stringify!($v) |
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
})
}
}
}
impl ::std::fmt::Display for $e {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$($e::$v => write!(f, stringify!($v)),)+
}
}
}
impl $e {
#[allow(dead_code)]
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)]
enum $e {
$($v),+
}
impl ::std::str::FromStr for $e {
type Err = String;
fn from_str(s: &str) -> Result<Self,Self::Err> {
use ::std::ascii::AsciiExt;
match s {
$(stringify!($v) |
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
})
}
}
}
impl ::std::fmt::Display for $e {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$($e::$v => write!(f, stringify!($v)),)+
}
}
}
impl $e {
#[allow(dead_code)]
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
(#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => {
#[derive($($d,)+)]
pub enum $e {
$($v),+
}
impl ::std::str::FromStr for $e {
type Err = String;
fn from_str(s: &str) -> Result<Self,Self::Err> {
use ::std::ascii::AsciiExt;
match s {
$(stringify!($v) |
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
})
}
}
}
impl ::std::fmt::Display for $e {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
$($e::$v => write!(f, stringify!($v)),)+
}
}
}
impl $e {
#[allow(dead_code)]
pub fn variants() -> Vec<&'static str> {
vec![
$(stringify!($v),)+
]
}
}
};
}
#[macro_export]
macro_rules! crate_version {
() => {
env!("CARGO_PKG_VERSION").to_owned()
}
}
#[macro_export]
macro_rules! clap_app {
(@app ($builder:expr)) => { $builder };
(@app ($builder:expr) (@arg $name:ident: $($tail:tt)*) $($tt:tt)*) => {
clap_app!{ @app
($builder.arg(clap_app!{ @arg ($crate::Arg::with_name(stringify!($name))) (-) $($tail)* }))
$($tt)*
}
};
(@app ($builder:expr) (@setting $setting:ident) $($tt:tt)*) => {
clap_app!{ @app
($builder.setting($crate::AppSettings::$setting))
$($tt)*
}
};
(@app ($builder:expr) (@attributes $($attr:tt)*) $($tt:tt)*) => {
clap_app!{ @app (clap_app!{ @arg ($builder) $($attr)* }) $($tt)* }
};
(@app ($builder:expr) (@group $name:ident => $($tail:tt)*) $($tt:tt)*) => {
clap_app!{ @app
(clap_app!{ @group ($builder, $crate::ArgGroup::with_name(stringify!($name))) $($tail)* })
$($tt)*
}
};
(@app ($builder:expr) (@subcommand $name:ident => $($tail:tt)*) $($tt:tt)*) => {
clap_app!{ @app
($builder.subcommand(
clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* }
))
$($tt)*
}
};
(@app ($builder:expr) ($ident:ident: $($v:expr),*) $($tt:tt)*) => {
clap_app!{ @app
($builder.$ident($($v),*))
$($tt)*
}
};
(@group ($builder:expr, $group:expr)) => { $builder.arg_group($group) };
(@group ($builder:expr, $group:expr) (@attributes $($attr:tt)*) $($tt:tt)*) => {
clap_app!{ @group ($builder, clap_app!{ @arg ($group) (-) $($attr)* }) $($tt)* }
};
(@group ($builder:expr, $group:expr) (@arg $name:ident: $($tail:tt)*) $($tt:tt)*) => {
clap_app!{ @group
(clap_app!{ @app ($builder) (@arg $name: $($tail)*) },
$group.add(stringify!($name)))
$($tt)*
}
};
(@arg ($arg:expr) $modes:tt) => { $arg };
(@arg ($arg:expr) $modes:tt --$long:ident $($tail:tt)*) => {
clap_app!{ @arg ($arg.long(stringify!($long))) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt -$short:ident $($tail:tt)*) => {
clap_app!{ @arg ($arg.short(stringify!($short))) $modes $($tail)* }
};
(@arg ($arg:expr) (-) <$var:ident> $($tail:tt)*) => {
clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) +takes_value +required $($tail)* }
};
(@arg ($arg:expr) (+) <$var:ident> $($tail:tt)*) => {
clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) $($tail)* }
};
(@arg ($arg:expr) (-) [$var:ident] $($tail:tt)*) => {
clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) +takes_value $($tail)* }
};
(@arg ($arg:expr) (+) [$var:ident] $($tail:tt)*) => {
clap_app!{ @arg ($arg.value_name(stringify!($var))) (+) $($tail)* }
};
(@arg ($arg:expr) $modes:tt ... $($tail:tt)*) => {
clap_app!{ @arg ($arg) $modes +multiple $($tail)* }
};
(@arg ($arg:expr) $modes:tt #{$n:expr, $m:expr} $($tail:tt)*) => {
clap_app!{ @arg ($arg) $modes min_values($n) max_values($m) $($tail)* }
};
(@arg ($arg:expr) $modes:tt * $($tail:tt)*) => {
clap_app!{ @arg ($arg) $modes +required $($tail)* }
};
(@arg ($arg:expr) $modes:tt !$ident $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident(false)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt +$ident:ident $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident(true)) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt {$fn_:expr} $($tail:tt)*) => {
clap_app!{ @arg ($arg.validator($fn_)) $modes $($tail)* }
};
(@as_expr $expr:expr) => { $expr };
(@arg ($arg:expr) $modes:tt $desc:tt) => { $arg.help(clap_app!{ @as_expr $desc }) };
(@arg ($arg:expr) $modes:tt $ident:ident[$($target:ident)*] $($tail:tt)*) => {
clap_app!{ @arg ($arg $( .$ident(stringify!($target)) )*) $modes $($tail)* }
};
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr)*) $($tail:tt)*) => {
clap_app!{ @arg ($arg.$ident($($expr)*)) $modes $($tail)* }
};
(@subcommand $name:ident => $($tail:tt)*) => {
clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* }
};
($name:ident => $($tail:tt)*) => {{
clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)*}
}};
}