Skip to content

Commit 9c4b8a1

Browse files
committed
imp(arg_enum): enum declared with arg_enum returns [&'static str; #] instead of Vec
1 parent 4735f4d commit 9c4b8a1

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/lib.rs

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

src/macros.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,35 @@ macro_rules! values_t_or_exit {
212212
};
213213
}
214214

215+
// _clap_count_exprs! is derived from https://github.com/DanielKeep/rust-grabbag
216+
// commit: 82a35ca5d9a04c3b920622d542104e3310ee5b07
217+
// License: MIT
218+
// Copyright ⓒ 2015 grabbag contributors.
219+
// Licensed under the MIT license (see LICENSE or <http://opensource.org
220+
// /licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
221+
// <http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
222+
// files in the project carrying such notice may not be copied, modified,
223+
// or distributed except according to those terms.
224+
//
225+
/// Counts the number of comma-delimited expressions passed to it. The result is a compile-time
226+
/// evaluable expression, suitable for use as a static array size, or the value of a `const`.
227+
///
228+
/// # Examples
229+
///
230+
/// ```
231+
/// # #[macro_use] extern crate clap;
232+
/// # fn main() {
233+
/// const COUNT: usize = _clap_count_exprs!(a, 5+1, "hi there!".into_string());
234+
/// assert_eq!(COUNT, 3);
235+
/// # }
236+
/// ```
237+
#[macro_export]
238+
macro_rules! _clap_count_exprs {
239+
() => { 0 };
240+
($e:expr) => { 1 };
241+
($e:expr, $($es:expr),+) => { 1 + _clap_count_exprs!($($es),*) };
242+
}
243+
215244
/// Convenience macro to generate more complete enums with variants to be used as a type when
216245
/// parsing arguments. This enum also provides a `variants()` function which can be used to
217246
/// retrieve a `Vec<&'static str>` of the variant names, as well as implementing `FromStr` and
@@ -284,8 +313,8 @@ macro_rules! arg_enum {
284313
}
285314
impl $e {
286315
#[allow(dead_code)]
287-
pub fn variants() -> Vec<&'static str> {
288-
vec![
316+
pub fn variants() -> [&'static str; _clap_count_exprs!($(stringify!($v)),+)] {
317+
[
289318
$(stringify!($v),)+
290319
]
291320
}

0 commit comments

Comments
 (0)