Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

im(arg_enum): allows ascii case insensitivity for enum variants #105

Merged
merged 1 commit into from
May 5, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ macro_rules! value_t_or_exit {
match v.parse::<$t>() {
Ok(val) => val,
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",
v,
stringify!($t),
Expand All @@ -254,7 +254,7 @@ macro_rules! value_t_or_exit {
match pv.parse::<$t>() {
Ok(rv) => tmp.push(rv),
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",
pv,
stringify!($t),
Expand Down Expand Up @@ -330,6 +330,8 @@ macro_rules! simple_enum {
/// Convenience macro to generate more complete enums with variants to be used as a type when
/// parsing arguments.
///
/// **NOTE:** Case insensitivity is supported for ASCII characters
///
/// These enums support pub (or not) and use of the #[derive()] traits
///
///
Expand Down Expand Up @@ -369,13 +371,15 @@ macro_rules! arg_enum {
type Err = String;

fn from_str(s: &str) -> Result<Self,Self::Err> {
use ::std::ascii::AsciiExt;
match s {
$(stringify!($v) => Ok($e::$v),)+
$(stringify!($v) |
_ if s.eq_ignore_ascii_case(stringify!($v)) => Ok($e::$v),)+
_ => Err({
let v = vec![
$(stringify!($v),)+
];
format!("valid:{}",
format!("valid values:{}",
v.iter().fold(String::new(), |a, i| {
a + &format!(" {}", i)[..]
}))
Expand Down