mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
im(arg_enum): allows ascii case insensitivity for enum variants
Allows creating an enum with CamelCase to follow Rust guidelines, but then will match ascii case insensitive. This means variant SomeEnum::Emacs would match string "emacs". Closes #104
This commit is contained in:
parent
632c89bf26
commit
b249f9657c
1 changed files with 8 additions and 4 deletions
|
@ -227,7 +227,7 @@ macro_rules! value_t_or_exit {
|
||||||
match v.parse::<$t>() {
|
match v.parse::<$t>() {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{} isn't a valid {}\n{}\n{}\nPlease re-run with --help for \
|
println!("{} isn't a valid {}\n\t{}\n{}\nPlease re-run with --help for \
|
||||||
more information",
|
more information",
|
||||||
v,
|
v,
|
||||||
stringify!($t),
|
stringify!($t),
|
||||||
|
@ -254,7 +254,7 @@ macro_rules! value_t_or_exit {
|
||||||
match pv.parse::<$t>() {
|
match pv.parse::<$t>() {
|
||||||
Ok(rv) => tmp.push(rv),
|
Ok(rv) => tmp.push(rv),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
println!("{} isn't a valid {}\n{}\nPlease re-run with --help for more \
|
println!("{} isn't a valid {}\n\t{}\nPlease re-run with --help for more \
|
||||||
information",
|
information",
|
||||||
pv,
|
pv,
|
||||||
stringify!($t),
|
stringify!($t),
|
||||||
|
@ -330,6 +330,8 @@ macro_rules! simple_enum {
|
||||||
/// Convenience macro to generate more complete enums with variants to be used as a type when
|
/// Convenience macro to generate more complete enums with variants to be used as a type when
|
||||||
/// parsing arguments.
|
/// parsing arguments.
|
||||||
///
|
///
|
||||||
|
/// **NOTE:** Case insensitivity is supported for ASCII characters
|
||||||
|
///
|
||||||
/// These enums support pub (or not) and use of the #[derive()] traits
|
/// These enums support pub (or not) and use of the #[derive()] traits
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
@ -369,13 +371,15 @@ macro_rules! arg_enum {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self,Self::Err> {
|
fn from_str(s: &str) -> Result<Self,Self::Err> {
|
||||||
|
use ::std::ascii::AsciiExt;
|
||||||
match s {
|
match s {
|
||||||
$(stringify!($v) => Ok($e::$v),)+
|
$(stringify!($v) |
|
||||||
|
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
|
||||||
_ => Err({
|
_ => Err({
|
||||||
let v = vec![
|
let v = vec![
|
||||||
$(stringify!($v),)+
|
$(stringify!($v),)+
|
||||||
];
|
];
|
||||||
format!("valid:{}",
|
format!("valid values:{}",
|
||||||
v.iter().fold(String::new(), |a, i| {
|
v.iter().fold(String::new(), |a, i| {
|
||||||
a + &format!(" {}", i)[..]
|
a + &format!(" {}", i)[..]
|
||||||
}))
|
}))
|
||||||
|
|
Loading…
Reference in a new issue