Skip to content

Commit

Permalink
Handle doc comments and empty variants in the Command! macro
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Jan 8, 2020
1 parent 7b9272c commit 3433f37
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/arg_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?,
)*
}

Expand All @@ -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))?),
)*
}
}
Expand Down

0 comments on commit 3433f37

Please sign in to comment.