imp(arg_enum): enum declared with arg_enum returns [&'static str; #] instead of Vec

This commit is contained in:
Kevin K 2016-01-31 08:03:09 -05:00
parent 4735f4d5cb
commit ab41be700b
3 changed files with 37 additions and 3 deletions

View file

@ -42,7 +42,7 @@ fn main() {
let m = App::new("myapp")
// Use a single positional argument that is required
.arg(Arg::from_usage("<foo> 'The Foo to use'")
.possible_values(&*Foo::variants()))
.possible_values(&Foo::variants()))
.arg(Arg::from_usage("<speed> 'The speed to use'")
// You can define a list of possible values if you want the values to be
// displayed in the help information. Whether you use possible_values() or

View file

@ -1,3 +1,8 @@
// Copyright ⓒ 2015-2016 Kevin B. Knapp and clap-rs contributors.
// Licensed under the MIT license
// (see LICENSE or <http://opensource.org/licenses/MIT>) All files in the project carrying such
// notice may not be copied, modified, or distributed except according to those terms.
//! A simple to use, efficient, and full featured library for parsing command line arguments and subcommands when writing console, or terminal applications.
//!
//! ## About

View file

@ -212,6 +212,35 @@ macro_rules! values_t_or_exit {
};
}
// _clap_count_exprs! is derived from https://github.com/DanielKeep/rust-grabbag
// commit: 82a35ca5d9a04c3b920622d542104e3310ee5b07
// License: MIT
// Copyright ⓒ 2015 grabbag contributors.
// Licensed under the MIT license (see LICENSE or <http://opensource.org
// /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
// <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
// files in the project carrying such notice may not be copied, modified,
// or distributed except according to those terms.
//
/// Counts the number of comma-delimited expressions passed to it. The result is a compile-time
/// evaluable expression, suitable for use as a static array size, or the value of a `const`.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate clap;
/// # fn main() {
/// const COUNT: usize = _clap_count_exprs!(a, 5+1, "hi there!".into_string());
/// assert_eq!(COUNT, 3);
/// # }
/// ```
#[macro_export]
macro_rules! _clap_count_exprs {
() => { 0 };
($e:expr) => { 1 };
($e:expr, $($es:expr),+) => { 1 + _clap_count_exprs!($($es),*) };
}
/// 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
@ -284,8 +313,8 @@ macro_rules! arg_enum {
}
impl $e {
#[allow(dead_code)]
pub fn variants() -> Vec<&'static str> {
vec![
pub fn variants() -> [&'static str; _clap_count_exprs!($(stringify!($v)),+)] {
[
$(stringify!($v),)+
]
}