diff --git a/src/macros.rs b/src/macros.rs index 5091638527b..8a26d97979e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -275,7 +275,8 @@ macro_rules! value_t_or_exit { } /// Convenience macro generated a simple enum with variants to be used as a type when parsing -/// arguments. +/// arguments. This enum also provides a `variants()` function which can be used to retrieve a +/// `Vec<&'static str>` of the variant names. /// /// **NOTE:** This macro automaically implements std::str::FromStr and std::fmt::Display /// @@ -332,11 +333,20 @@ macro_rules! simple_enum { } } } + + impl $e { + pub fn variants() -> Vec<&'static str> { + vec![ + $(stringify!($v),)+ + ] + } + } }; } /// Convenience macro to generate more complete enums with variants to be used as a type when -/// parsing arguments. +/// parsing arguments. This enum also provides a `variants()` function which can be used to retrieve a +/// `Vec<&'static str>` of the variant names. /// /// **NOTE:** Case insensitivity is supported for ASCII characters /// @@ -405,6 +415,14 @@ macro_rules! arg_enum { } } } + + impl $e { + fn variants() -> Vec<&'static str> { + vec![ + $(stringify!($v),)+ + ] + } + } }; (pub enum $e:ident { $($v:ident),+ } ) => { pub enum $e { @@ -439,6 +457,14 @@ macro_rules! arg_enum { } } } + + impl $e { + pub fn variants() -> Vec<&'static str> { + vec![ + $(stringify!($v),)+ + ] + } + } }; (#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] @@ -474,6 +500,14 @@ macro_rules! arg_enum { } } } + + impl $e { + pub fn variants() -> Vec<&'static str> { + vec![ + $(stringify!($v),)+ + ] + } + } }; (#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] @@ -509,6 +543,14 @@ macro_rules! arg_enum { } } } + + impl $e { + pub fn variants() -> Vec<&'static str> { + vec![ + $(stringify!($v),)+ + ] + } + } }; }