diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-14 16:34:27 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-14 16:34:27 -0800 |
commit | 74b9b3845846e0fbc2efd37d80b5527be1bee564 (patch) | |
tree | 5dbc875a78aa7434025c5cbda259c25a99b12704 /nitrocli/src/arg_util.rs | |
parent | 8e0badc9988775b1b0340db5b2cb975c3ecf407d (diff) | |
download | nitrocli-74b9b3845846e0fbc2efd37d80b5527be1bee564.tar.gz nitrocli-74b9b3845846e0fbc2efd37d80b5527be1bee564.tar.bz2 |
Auto-populate help text content
With the ability to fully generate the command enums we use for working
with the argparse crate, we can now take things one step further and
populate the contents of the help string we print for the user that
lists the available commands.
Doing so we also fix a bug where we forgot to mention the "storage
status" command in the help text.
Diffstat (limited to 'nitrocli/src/arg_util.rs')
-rw-r--r-- | nitrocli/src/arg_util.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/nitrocli/src/arg_util.rs b/nitrocli/src/arg_util.rs index b188085..b1ced25 100644 --- a/nitrocli/src/arg_util.rs +++ b/nitrocli/src/arg_util.rs @@ -17,6 +17,13 @@ // * along with this program. If not, see <http://www.gnu.org/licenses/>. * // ************************************************************************* +macro_rules! count { + ($head:ident) => { 1 }; + ($head:ident, $($tail:ident),*) => { + 1 + count!($($tail),*) + } +} + /// A macro for generating an enum with a set of simple (i.e., no /// parameters) variants and their textual representations. // TODO: Right now we hard code the derives we create. We may want to @@ -50,6 +57,21 @@ macro_rules! Enum { )* } + impl $name { + #[allow(unused)] + pub fn all(&self) -> [$name; count!($($var),*) ] { + $name::all_variants() + } + + pub fn all_variants() -> [$name; count!($($var),*) ] { + [ + $( + $name::$var, + )* + ] + } + } + impl AsRef<str> for $name { fn as_ref(&self) -> &'static str { match *self { @@ -81,6 +103,34 @@ macro_rules! Enum { }; } +/// A macro for formatting the variants of an enum (as created by the +/// Enum!{} macro) ready to be used in a help text. The supplied `fmt` +/// needs to contain the named parameter `{variants}`, which will be +/// replaced with a generated version of the enum's variants. +macro_rules! fmt_enum { + ( $enm:ident ) => {{ + $enm + .all() + .iter() + .map(::std::convert::AsRef::as_ref) + .collect::<::std::vec::Vec<_>>() + .join("|") + }}; +} + +/// A macro for generating the help text for a command/subcommand. The +/// argument is the variable representing the command (which in turn is +/// an enum). +/// Note that the name of this variable is embedded into the help text! +macro_rules! cmd_help { + ( $cmd:ident ) => { + format!( + concat!("The ", stringify!($cmd), " to execute ({})"), + fmt_enum!($cmd) + ) + }; +} + #[cfg(test)] mod tests { Enum! {Command, [ @@ -90,6 +140,14 @@ mod tests { ]} #[test] + fn all_variants() { + assert_eq!( + Command::all_variants(), + [Command::Var1, Command::Var2, Command::Var3] + ) + } + + #[test] fn text_representations() { assert_eq!(Command::Var1.as_ref(), "var1"); assert_eq!(Command::Var2.as_ref(), "2"); |