clap::arg_enum!
[−]
[src]
macro_rules! arg_enum { (enum $e:ident { $($v:ident),+ } ) => { enum $e { $($v),+ } impl ::std::str::FromStr for $e { type Err = String; fn from_str(s: &str) -> Result<Self,Self::Err> { use ::std::ascii::AsciiExt; match s { $(stringify!($v) | _ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+ _ => Err({ let v = vec![ $(stringify!($v),)+ ]; format!("valid values:{}", v.iter().fold(String::new(), |a, i| { a + &format!(" {}", i)[..] })) }) } } } impl ::std::fmt::Display for $e { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match *self { $($e::$v => write!(f, stringify!($v)),)+ } } } impl $e { #[allow(dead_code)] fn variants() -> Vec<&'static str> { vec![ $(stringify!($v),)+ ] } } }; (pub enum $e:ident { $($v:ident),+ } ) => { pub enum $e { $($v),+ } impl ::std::str::FromStr for $e { type Err = String; fn from_str(s: &str) -> Result<Self,Self::Err> { use ::std::ascii::AsciiExt; match s { $(stringify!($v) | _ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+ _ => Err({ let v = vec![ $(stringify!($v),)+ ]; format!("valid values:{}", v.iter().fold(String::new(), |a, i| { a + &format!(" {}", i)[..] })) }) } } } impl ::std::fmt::Display for $e { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match *self { $($e::$v => write!(f, stringify!($v)),)+ } } } impl $e { #[allow(dead_code)] pub fn variants() -> Vec<&'static str> { vec![ $(stringify!($v),)+ ] } } }; (#[derive($($d:ident),+)] enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] enum $e { $($v),+ } impl ::std::str::FromStr for $e { type Err = String; fn from_str(s: &str) -> Result<Self,Self::Err> { use ::std::ascii::AsciiExt; match s { $(stringify!($v) | _ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+ _ => Err({ let v = vec![ $(stringify!($v),)+ ]; format!("valid values:{}", v.iter().fold(String::new(), |a, i| { a + &format!(" {}", i)[..] })) }) } } } impl ::std::fmt::Display for $e { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match *self { $($e::$v => write!(f, stringify!($v)),)+ } } } impl $e { #[allow(dead_code)] pub fn variants() -> Vec<&'static str> { vec![ $(stringify!($v),)+ ] } } }; (#[derive($($d:ident),+)] pub enum $e:ident { $($v:ident),+ } ) => { #[derive($($d,)+)] pub enum $e { $($v),+ } impl ::std::str::FromStr for $e { type Err = String; fn from_str(s: &str) -> Result<Self,Self::Err> { use ::std::ascii::AsciiExt; match s { $(stringify!($v) | _ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+ _ => Err({ let v = vec![ $(stringify!($v),)+ ]; format!("valid values:{}", v.iter().fold(String::new(), |a, i| { a + &format!(" {}", i)[..] })) }) } } } impl ::std::fmt::Display for $e { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match *self { $($e::$v => write!(f, stringify!($v)),)+ } } } impl $e { #[allow(dead_code)] 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. 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
NOTE: This macro automatically implements std::str::FromStr and std::fmt::Display
These enums support pub (or not) and use of the #[derive()] traits
Examples
arg_enum!{ #[derive(Debug)] pub enum Foo { Bar, Baz, Qux } } // Foo enum can now be used via Foo::Bar, or Foo::Baz, etc // and implements std::str::FromStr to use with the value_t! macros fn main() { let m = App::new("app") .arg_from_usage("<foo> 'the foo'") .get_matches(); let f = value_t_or_exit!(m.value_of("foo"), Foo); // Use f like any other Foo variant... }