[][src]Macro clap::arg_enum

macro_rules! arg_enum {
    (@as_item $($i:item)*) => { ... };
    (@impls ( $($tts:tt)* ) -> ($e:ident, $($v:ident),+)) => { ... };
    ($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => { ... };
    ($(#[$($m:meta),+])+ pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ... };
    ($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => { ... };
    ($(#[$($m:meta),+])+ enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ... };
    (pub enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => { ... };
    (pub enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ... };
    (enum $e:ident { $($v:ident $(=$val:expr)*,)+ } ) => { ... };
    (enum $e:ident { $($v:ident $(=$val:expr)*),+ } ) => { ... };
}

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, as well as implementing FromStr and Display automatically.

NOTE: Case insensitivity is supported for ASCII characters only. It's highly recommended to use Arg::case_insensitive(true) for args that will be used with these enums

NOTE: This macro automatically implements std::str::FromStr and std::fmt::Display

NOTE: These enums support pub (or not) and uses of the #[derive()] traits

Examples

arg_enum!{
    #[derive(PartialEq, 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(Arg::from_usage("<foo> 'the foo'")
                    .possible_values(&Foo::variants())
                    .case_insensitive(true))
                .get_matches_from(vec![
                    "app", "baz"
                ]);
    let f = value_t!(m, "foo", Foo).unwrap_or_else(|e| e.exit());

    assert_eq!(f, Foo::Baz);
}