summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-08 11:35:19 +0000
committerDaniel Mueller <deso@posteo.net>2020-01-08 11:35:19 +0000
commit3433f378b1cd783aafcc24e8bcc5bac5e8497246 (patch)
tree0037f6f2fcdf62ce3d7c93fc78d4d420fb3ddcac
parent7b9272c21af98c24a590ae9de1f122e03edc79ef (diff)
downloadnitrocli-3433f378b1cd783aafcc24e8bcc5bac5e8497246.tar.gz
nitrocli-3433f378b1cd783aafcc24e8bcc5bac5e8497246.tar.bz2
Handle doc comments and empty variants in the Command! macro
This patch introduces two changes to the Command! macro: - We allow variants without fields so that we no longer have to define empty *Args structs just for the Command! macro. - We allow doc comments so that we can document commands without a separate *Args struct.
-rw-r--r--src/arg_util.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/arg_util.rs b/src/arg_util.rs
index 8cd8fe5..c72d89b 100644
--- a/src/arg_util.rs
+++ b/src/arg_util.rs
@@ -24,12 +24,21 @@ macro_rules! count {
}
}
+/// Translate an optional source into an optional destination.
+macro_rules! tr {
+ ($dst:tt, $src:tt) => {
+ $dst
+ };
+ ($dst:tt) => {};
+}
+
macro_rules! Command {
- ( $name:ident, [ $( $var:ident($inner:ident) => $exec:expr, ) *] ) => {
+ ( $name:ident, [ $( $(#[$doc:meta])* $var:ident$(($inner:ty))? => $exec:expr, ) *] ) => {
#[derive(Debug, PartialEq, structopt::StructOpt)]
pub enum $name {
$(
- $var($inner),
+ $(#[$doc])*
+ $var$(($inner))?,
)*
}
@@ -41,7 +50,7 @@ macro_rules! Command {
) -> crate::Result<()> {
match self {
$(
- $name::$var(args) => $exec(ctx, args),
+ $name::$var$((tr!(args, $inner)))? => $exec(ctx $(,tr!(args, $inner))?),
)*
}
}