mirror of
https://github.com/clap-rs/clap
synced 2024-11-15 00:57:15 +00:00
fix!: Remove deprecated macros
This commit is contained in:
parent
57bfb8c425
commit
819c1e061d
4 changed files with 0 additions and 711 deletions
388
src/macros.rs
388
src/macros.rs
|
@ -1,189 +1,3 @@
|
||||||
/// Deprecated, replaced with [`ArgMatches::value_of_t`][crate::ArgMatches::value_of_t]
|
|
||||||
#[macro_export]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ArgMatches::get_one`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! value_t {
|
|
||||||
($m:ident, $v:expr, $t:ty) => {
|
|
||||||
$crate::value_t!($m.value_of($v), $t)
|
|
||||||
};
|
|
||||||
($m:ident.value_of($v:expr), $t:ty) => {
|
|
||||||
$m.value_of_t::<$t>($v)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deprecated, replaced with [`ArgMatches::value_of_t_or_exit`][crate::ArgMatches::value_of_t_or_exit]
|
|
||||||
#[macro_export]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ArgMatches::get_one`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! value_t_or_exit {
|
|
||||||
($m:ident, $v:expr, $t:ty) => {
|
|
||||||
value_t_or_exit!($m.value_of($v), $t)
|
|
||||||
};
|
|
||||||
($m:ident.value_of($v:expr), $t:ty) => {
|
|
||||||
$m.value_of_t_or_exit::<$t>($v)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deprecated, replaced with [`ArgMatches::values_of_t`][crate::ArgMatches::value_of_t]
|
|
||||||
#[macro_export]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ArgMatches::get_many`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! values_t {
|
|
||||||
($m:ident, $v:expr, $t:ty) => {
|
|
||||||
values_t!($m.values_of($v), $t)
|
|
||||||
};
|
|
||||||
($m:ident.values_of($v:expr), $t:ty) => {
|
|
||||||
$m.values_of_t::<$t>($v)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deprecated, replaced with [`ArgMatches::values_of_t_or_exit`][crate::ArgMatches::value_of_t_or_exit]
|
|
||||||
#[macro_export]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ArgMatches::get_many`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
macro_rules! values_t_or_exit {
|
|
||||||
($m:ident, $v:expr, $t:ty) => {
|
|
||||||
values_t_or_exit!($m.values_of($v), $t)
|
|
||||||
};
|
|
||||||
($m:ident.values_of($v:expr), $t:ty) => {
|
|
||||||
$m.values_of_t_or_exit::<$t>($v)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ValueEnum`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! _clap_count_exprs {
|
|
||||||
() => { 0 };
|
|
||||||
($e:expr) => { 1 };
|
|
||||||
($e:expr, $($es:expr),+) => { 1 + $crate::_clap_count_exprs!($($es),*) };
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Deprecated, replaced with [`ArgEnum`][crate::ArgEnum]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.0.0", note = "Replaced with `ValueEnum`")
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! arg_enum {
|
|
||||||
(@as_item $($i:item)*) => ($($i)*);
|
|
||||||
(@impls ( $($tts:tt)* ) -> ($e:ident, $($v:ident),+)) => {
|
|
||||||
$crate::arg_enum!(@as_item
|
|
||||||
$($tts)*
|
|
||||||
|
|
||||||
impl ::std::str::FromStr for $e {
|
|
||||||
type Err = String;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> ::std::result::Result<Self,Self::Err> {
|
|
||||||
#[allow(deprecated, unused_imports)]
|
|
||||||
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.join(", "))
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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() -> [&'static str; $crate::_clap_count_exprs!($(stringify!($v)),+)] {
|
|
||||||
[
|
|
||||||
$(stringify!($v),)+
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
($(#[$($m),+])+
|
|
||||||
pub enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
($(#[$($m),+])+
|
|
||||||
pub enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
($(#[$($m),+])+
|
|
||||||
enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
($(#[$($m),+])+
|
|
||||||
enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
(pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
(pub enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
(pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
(pub enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
(enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
(enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
(enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => {
|
|
||||||
$crate::arg_enum!(@impls
|
|
||||||
(enum $e {
|
|
||||||
$($v$(=$val)*),+
|
|
||||||
}) -> ($e, $($v),+)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Allows you to pull the version from your Cargo.toml at compile time as
|
/// Allows you to pull the version from your Cargo.toml at compile time as
|
||||||
/// `MAJOR.MINOR.PATCH_PKGVERSION_PRE`
|
/// `MAJOR.MINOR.PATCH_PKGVERSION_PRE`
|
||||||
///
|
///
|
||||||
|
@ -341,46 +155,6 @@ macro_rules! command {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deprecated, replaced with [`clap::command!`][crate::command]
|
|
||||||
#[cfg(feature = "cargo")]
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(since = "3.1.0", note = "Replaced with `clap::command!")
|
|
||||||
)]
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! app_from_crate {
|
|
||||||
() => {{
|
|
||||||
let mut cmd = $crate::Command::new($crate::crate_name!()).version($crate::crate_version!());
|
|
||||||
|
|
||||||
let author = $crate::crate_authors!(", ");
|
|
||||||
if !author.is_empty() {
|
|
||||||
cmd = cmd.author(author)
|
|
||||||
}
|
|
||||||
|
|
||||||
let about = $crate::crate_description!();
|
|
||||||
if !about.is_empty() {
|
|
||||||
cmd = cmd.about(about)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd
|
|
||||||
}};
|
|
||||||
($sep:expr) => {{
|
|
||||||
let mut cmd = $crate::Command::new($crate::crate_name!()).version($crate::crate_version!());
|
|
||||||
|
|
||||||
let author = $crate::crate_authors!($sep);
|
|
||||||
if !author.is_empty() {
|
|
||||||
cmd = cmd.author(author)
|
|
||||||
}
|
|
||||||
|
|
||||||
let about = $crate::crate_description!();
|
|
||||||
if !about.is_empty() {
|
|
||||||
cmd = cmd.about(about)
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! arg_impl {
|
macro_rules! arg_impl {
|
||||||
|
@ -755,168 +529,6 @@ macro_rules! arg {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deprecated, replaced with [`clap::Parser`][crate::Parser] and [`clap::arg!`][crate::arg] (Issue clap-rs/clap#2835)
|
|
||||||
#[cfg_attr(
|
|
||||||
feature = "deprecated",
|
|
||||||
deprecated(
|
|
||||||
since = "3.0.0",
|
|
||||||
note = "Replaced with `clap::Parser` for a declarative API (Issue clap-rs/clap#2835)"
|
|
||||||
)
|
|
||||||
)]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[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: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) (@setting $setting:ident) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($builder.setting($crate::AppSettings::$setting))
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Treat the application builder as an argument to set its attributes
|
|
||||||
(@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 => $($tail:tt)*) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($crate::clap_app!{ @group ($builder, $crate::ArgGroup::new(stringify!($name))) $($tail)* })
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
(@app ($builder:expr) (@group $name:ident !$ident:ident => $($tail:tt)*) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($crate::clap_app!{ @group ($builder, $crate::ArgGroup::new(stringify!($name)).$ident(false)) $($tail)* })
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
(@app ($builder:expr) (@group $name:ident +$ident:ident => $($tail:tt)*) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($crate::clap_app!{ @group ($builder, $crate::ArgGroup::new(stringify!($name)).$ident(true)) $($tail)* })
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Handle subcommand creation
|
|
||||||
(@app ($builder:expr) (@subcommand $name:ident => $($tail:tt)*) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($builder.subcommand(
|
|
||||||
$crate::clap_app!{ @app ($crate::Command::new(stringify!($name))) $($tail)* }
|
|
||||||
))
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Yaml like function calls - used for setting various meta directly against the app
|
|
||||||
(@app ($builder:expr) ($ident:ident: $($v:expr),*) $($tt:tt)*) => {
|
|
||||||
// $crate::clap_app!{ @app ($builder.$ident($($v),*)) $($tt)* }
|
|
||||||
$crate::clap_app!{ @app
|
|
||||||
($builder.$ident($($v),*))
|
|
||||||
$($tt)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add members to group and continue argument handling with the parent builder
|
|
||||||
(@group ($builder:expr, $group:expr)) => { $builder.group($group) };
|
|
||||||
// Treat the group builder as an argument to set its attributes
|
|
||||||
(@group ($builder:expr, $group:expr) (@attributes $($attr:tt)*) $($tt:tt)*) => {
|
|
||||||
$crate::clap_app!{ @group ($builder, $crate::clap_app!{ @arg ($group) (-) $($attr)* }) $($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)*
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// No more tokens to munch
|
|
||||||
(@arg ($arg:expr) $modes:tt) => { $arg };
|
|
||||||
// Shorthand tokens influenced by the usage_string
|
|
||||||
(@arg ($arg:expr) $modes:tt --($long:expr) $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.long($long)) $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: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 +takes_value $($tail)* }
|
|
||||||
};
|
|
||||||
// Shorthand magic
|
|
||||||
(@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)* }
|
|
||||||
};
|
|
||||||
// !foo -> .foo(false)
|
|
||||||
(@arg ($arg:expr) $modes:tt !$ident:ident $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.$ident(false)) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
// +foo -> .foo(true)
|
|
||||||
(@arg ($arg:expr) $modes:tt +$ident:ident $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.$ident(true)) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
// Validator
|
|
||||||
(@arg ($arg:expr) $modes:tt {$fn_:expr} $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.validator($fn_)) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
(@as_expr $expr:expr) => { $expr };
|
|
||||||
// Help
|
|
||||||
(@arg ($arg:expr) $modes:tt $desc:tt) => { $arg.help($crate::clap_app!{ @as_expr $desc }) };
|
|
||||||
// Handle functions that need to be called multiple times for each argument
|
|
||||||
(@arg ($arg:expr) $modes:tt $ident:ident[$($target:ident)*] $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg $( .$ident(stringify!($target)) )*) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
// Inherit builder's functions, e.g. `index(2)`, `requires_if("val", "arg")`
|
|
||||||
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr),*) $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
// Inherit builder's functions with trailing comma, e.g. `index(2,)`, `requires_if("val", "arg",)`
|
|
||||||
(@arg ($arg:expr) $modes:tt $ident:ident($($expr:expr,)*) $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @arg ($arg.$ident($($expr),*)) $modes $($tail)* }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build a subcommand outside of an app.
|
|
||||||
(@subcommand $name:ident => $($tail:tt)*) => {
|
|
||||||
$crate::clap_app!{ @app ($crate::Command::new(stringify!($name))) $($tail)* }
|
|
||||||
};
|
|
||||||
// Start the magic
|
|
||||||
(($name:expr) => $($tail:tt)*) => {{
|
|
||||||
$crate::clap_app!{ @app ($crate::Command::new($name)) $($tail)*}
|
|
||||||
}};
|
|
||||||
|
|
||||||
($name:ident => $($tail:tt)*) => {{
|
|
||||||
$crate::clap_app!{ @app ($crate::Command::new(stringify!($name))) $($tail)*}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! impl_settings {
|
macro_rules! impl_settings {
|
||||||
($settings:ident, $flags:ident,
|
($settings:ident, $flags:ident,
|
||||||
$(
|
$(
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
#![cfg(feature = "cargo")]
|
|
||||||
#![allow(deprecated)]
|
|
||||||
|
|
||||||
use clap::{app_from_crate, error::ErrorKind};
|
|
||||||
|
|
||||||
static EVERYTHING: &str = "clap {{version}}
|
|
||||||
A simple to use, efficient, and full-featured Command Line Argument Parser
|
|
||||||
|
|
||||||
USAGE:
|
|
||||||
clap
|
|
||||||
|
|
||||||
OPTIONS:
|
|
||||||
-h, --help Print help information
|
|
||||||
-V, --version Print version information
|
|
||||||
";
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn app_from_crate() {
|
|
||||||
let res = app_from_crate!().try_get_matches_from(vec!["clap", "--help"]);
|
|
||||||
|
|
||||||
assert!(res.is_err());
|
|
||||||
let err = res.unwrap_err();
|
|
||||||
assert_eq!(err.kind(), ErrorKind::DisplayHelp);
|
|
||||||
assert_eq!(
|
|
||||||
err.to_string(),
|
|
||||||
EVERYTHING.replace("{{version}}", env!("CARGO_PKG_VERSION"))
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
#![allow(clippy::bool_assert_comparison)]
|
#![allow(clippy::bool_assert_comparison)]
|
||||||
|
|
||||||
mod action;
|
mod action;
|
||||||
mod app_from_crate;
|
|
||||||
mod app_settings;
|
mod app_settings;
|
||||||
mod arg_aliases;
|
mod arg_aliases;
|
||||||
mod arg_aliases_short;
|
mod arg_aliases_short;
|
||||||
|
|
294
tests/macros.rs
294
tests/macros.rs
|
@ -1,297 +1,3 @@
|
||||||
// We intentionally don't import `clap_app!` here; not having it in scope protects against the
|
|
||||||
// class of errors where the macro refers to itself as `clap_app!` instead of `$crate::clap_app!`
|
|
||||||
use clap::error::ErrorKind;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn basic() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg opt: -o --option +takes_value ... "tests options")
|
|
||||||
(@arg positional: index(1) "tests positionals")
|
|
||||||
(@arg flag: -f --flag ... +global "tests flags")
|
|
||||||
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
||||||
"tests flags with exclusions")
|
|
||||||
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
||||||
"tests long options with exclusions")
|
|
||||||
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
||||||
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
||||||
"tests options with specific value sets")
|
|
||||||
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
||||||
"tests positionals with specific values")
|
|
||||||
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
||||||
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
||||||
(@subcommand subcmd =>
|
|
||||||
(about: "tests subcommands")
|
|
||||||
(version: "0.1")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
||||||
(@arg scpositional: index(1) "tests positionals"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quoted_app_name() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let mut cmd = clap::clap_app!(("cmd name with spaces-and-hyphens") =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg option: -o --option +takes_value ... "tests options")
|
|
||||||
(@arg positional: index(1) "tests positionals")
|
|
||||||
(@arg flag: -f --flag ... +global "tests flags")
|
|
||||||
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
||||||
"tests flags with exclusions")
|
|
||||||
(@arg option2: --long_option_2 conflicts_with[option] requires[positional2]
|
|
||||||
"tests long options with exclusions")
|
|
||||||
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
||||||
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
||||||
"tests options with specific value sets")
|
|
||||||
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
||||||
"tests positionals with specific values")
|
|
||||||
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
||||||
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
||||||
(@subcommand subcmd =>
|
|
||||||
(about: "tests subcommands")
|
|
||||||
(version: "0.1")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
||||||
(@arg scpositional: index(1) "tests positionals"))
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(cmd.get_name(), "cmd name with spaces-and-hyphens");
|
|
||||||
|
|
||||||
let mut help_text = vec![];
|
|
||||||
cmd.write_help(&mut help_text)
|
|
||||||
.expect("Could not write help text.");
|
|
||||||
let help_text = String::from_utf8(help_text).expect("Help text is not valid utf-8");
|
|
||||||
assert!(help_text.starts_with("cmd name with spaces-and-hyphens 0.1\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quoted_arg_long_name() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg option: -o --option +takes_value ... "tests options")
|
|
||||||
(@arg positional: index(1) "tests positionals")
|
|
||||||
(@arg flag: -f --flag ... +global "tests flags")
|
|
||||||
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
||||||
"tests flags with exclusions")
|
|
||||||
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
|
|
||||||
"tests long options with exclusions")
|
|
||||||
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
||||||
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
||||||
"tests options with specific value sets")
|
|
||||||
(@arg positional3: index(3) ... possible_value[vi emacs]
|
|
||||||
"tests positionals with specific values")
|
|
||||||
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
||||||
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
||||||
(@subcommand subcmd =>
|
|
||||||
(about: "tests subcommands")
|
|
||||||
(version: "0.1")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
||||||
(@arg scpositional: index(1) "tests positionals"))
|
|
||||||
);
|
|
||||||
|
|
||||||
let matches = cmd
|
|
||||||
.try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
|
|
||||||
.expect("Expected to successfully match the given args.");
|
|
||||||
#[allow(deprecated)]
|
|
||||||
{
|
|
||||||
assert!(matches.is_present("option2"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quoted_arg_name() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg option: -o --option +takes_value ... "tests options")
|
|
||||||
(@arg ("positional-arg"): index(1) "tests positionals")
|
|
||||||
(@arg flag: -f --flag ... +global "tests flags")
|
|
||||||
(@arg flag2: -F conflicts_with[flag] requires[option2]
|
|
||||||
"tests flags with exclusions")
|
|
||||||
(@arg option2: --("long-option-2") conflicts_with[option] requires[positional2]
|
|
||||||
"tests long options with exclusions")
|
|
||||||
(@arg positional2: index(2) "tests positionals with exclusions")
|
|
||||||
(@arg option3: -O --Option +takes_value possible_value[fast slow]
|
|
||||||
"tests options with specific value sets")
|
|
||||||
(@arg ("positional-3"): index(3) ... possible_value[vi emacs]
|
|
||||||
"tests positionals with specific values")
|
|
||||||
(@arg multvals: --multvals +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg multvalsmo: --multvalsmo ... +takes_value value_name[one two]
|
|
||||||
"Tests multiple values, not mult occs")
|
|
||||||
(@arg minvals: --minvals2 min_values(1) ... +takes_value "Tests 2 min vals")
|
|
||||||
(@arg maxvals: --maxvals3 ... +takes_value max_values(3) "Tests 3 max vals")
|
|
||||||
(@subcommand subcmd =>
|
|
||||||
(about: "tests subcommands")
|
|
||||||
(version: "0.1")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@arg scoption: -o --option ... +takes_value "tests options")
|
|
||||||
(@arg scpositional: index(1) "tests positionals"))
|
|
||||||
);
|
|
||||||
|
|
||||||
let matches = cmd
|
|
||||||
.try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
|
|
||||||
.expect("Expected to successfully match the given args.");
|
|
||||||
assert!(matches.is_present("option2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn group_macro() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@group difficulty =>
|
|
||||||
(@arg hard: -h --hard "Sets hard mode")
|
|
||||||
(@arg normal: -n --normal "Sets normal mode")
|
|
||||||
(@arg easy: -e --easy "Sets easy mode")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = cmd.try_get_matches_from(vec!["bin_name", "--hard"]);
|
|
||||||
assert!(result.is_ok());
|
|
||||||
let matches = result.expect("Expected to successfully match the given args.");
|
|
||||||
assert!(matches.is_present("difficulty"));
|
|
||||||
assert!(matches.is_present("hard"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn group_macro_set_multiple() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@group difficulty +multiple =>
|
|
||||||
(@arg hard: -h --hard "Sets hard mode")
|
|
||||||
(@arg normal: -n --normal "Sets normal mode")
|
|
||||||
(@arg easy: -e --easy "Sets easy mode")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = cmd.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
|
|
||||||
assert!(result.is_ok());
|
|
||||||
let matches = result.expect("Expected to successfully match the given args.");
|
|
||||||
assert!(matches.is_present("difficulty"));
|
|
||||||
assert!(matches.is_present("hard"));
|
|
||||||
assert!(matches.is_present("easy"));
|
|
||||||
assert!(!matches.is_present("normal"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn group_macro_set_not_multiple() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@group difficulty !multiple =>
|
|
||||||
(@arg hard: -h --hard "Sets hard mode")
|
|
||||||
(@arg normal: -n --normal "Sets normal mode")
|
|
||||||
(@arg easy: -e --easy "Sets easy mode")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = cmd.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
|
|
||||||
assert!(result.is_err());
|
|
||||||
let err = result.unwrap_err();
|
|
||||||
assert_eq!(err.kind(), ErrorKind::ArgumentConflict);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn group_macro_set_required() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@group difficulty +required =>
|
|
||||||
(@arg hard: -h --hard "Sets hard mode")
|
|
||||||
(@arg normal: -n --normal "Sets normal mode")
|
|
||||||
(@arg easy: -e --easy "Sets easy mode")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = cmd.try_get_matches_from(vec!["bin_name"]);
|
|
||||||
assert!(result.is_err());
|
|
||||||
let err = result.unwrap_err();
|
|
||||||
assert_eq!(err.kind(), ErrorKind::MissingRequiredArgument);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn group_macro_set_not_required() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(version: "0.1")
|
|
||||||
(about: "tests clap library")
|
|
||||||
(author: "Kevin K. <kbknapp@gmail.com>")
|
|
||||||
(@group difficulty !required =>
|
|
||||||
(@arg hard: -h --hard "Sets hard mode")
|
|
||||||
(@arg normal: -n --normal "Sets normal mode")
|
|
||||||
(@arg easy: -e --easy "Sets easy mode")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let result = cmd.try_get_matches_from(vec!["bin_name"]);
|
|
||||||
assert!(result.is_ok());
|
|
||||||
let matches = result.expect("Expected to successfully match the given args.");
|
|
||||||
assert!(!matches.is_present("difficulty"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn multiarg() {
|
|
||||||
#![allow(deprecated)]
|
|
||||||
let cmd = clap::clap_app!(claptests =>
|
|
||||||
(@arg flag: --flag "value")
|
|
||||||
(@arg multiarg: --multiarg
|
|
||||||
default_value("flag-unset") default_value_if("flag", None, Some("flag-set"))
|
|
||||||
"multiarg")
|
|
||||||
(@arg multiarg2: --multiarg2
|
|
||||||
default_value("flag-unset") default_value_if("flag", None, Some("flag-set"))
|
|
||||||
"multiarg2")
|
|
||||||
);
|
|
||||||
|
|
||||||
let matches = cmd
|
|
||||||
.clone()
|
|
||||||
.try_get_matches_from(vec!["bin_name"])
|
|
||||||
.expect("match failed");
|
|
||||||
|
|
||||||
assert_eq!(matches.value_of("multiarg"), Some("flag-unset"));
|
|
||||||
assert_eq!(matches.value_of("multiarg2"), Some("flag-unset"));
|
|
||||||
|
|
||||||
let matches = cmd
|
|
||||||
.try_get_matches_from(vec!["bin_name", "--flag"])
|
|
||||||
.expect("match failed");
|
|
||||||
|
|
||||||
assert_eq!(matches.value_of("multiarg"), Some("flag-set"));
|
|
||||||
assert_eq!(matches.value_of("multiarg2"), Some("flag-set"));
|
|
||||||
}
|
|
||||||
|
|
||||||
mod arg {
|
mod arg {
|
||||||
#[test]
|
#[test]
|
||||||
fn name_explicit() {
|
fn name_explicit() {
|
||||||
|
|
Loading…
Reference in a new issue