From 72a31d38d6e719b337d23e3a73a5050bc9221d1c Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 8 Jan 2020 11:35:19 +0000 Subject: 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. --- nitrocli/src/arg_util.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index cb75c52..bc1c02c 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/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))?), )* } } -- cgit v1.2.1