From 9c4b8a1a6b12949222f17d1074578ad7676b9c0d Mon Sep 17 00:00:00 2001 From: Kevin K Date: Sun, 31 Jan 2016 08:03:09 -0500 Subject: [PATCH] imp(arg_enum): enum declared with arg_enum returns [&'static str; #] instead of Vec --- src/lib.rs | 5 +++++ src/macros.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1fc002ef78b..266eeb6d7b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,8 @@ +// Copyright ⓒ 2015-2016 Kevin B. Knapp and clap-rs contributors. +// Licensed under the MIT license +// (see LICENSE or ) 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 diff --git a/src/macros.rs b/src/macros.rs index c26bbba3dbe..1c12f18b14f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 ) or the Apache License, Version 2.0 (see LICENSE of +// ), 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),)+ ] }