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

New feature: 'iter_matches' #1240

Closed
wants to merge 11 commits into from
24 changes: 13 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ clippy = { version = "~0.0.166", optional = true }
atty = { version = "0.2.2", optional = true }
vec_map = { version = "0.8", optional = true }
term_size = { version = "0.3.0", optional = true }
indexmap = { version = "1.0.1", optional = true }

[target.'cfg(not(windows))'.dependencies]
ansi_term = { version = "0.11", optional = true }
Expand All @@ -43,17 +44,18 @@ lazy_static = "1"
version-sync = "0.5"

[features]
default = ["suggestions", "color", "vec_map"]
suggestions = ["strsim"]
color = ["ansi_term", "atty"]
wrap_help = ["term_size", "textwrap/term_size"]
yaml = ["yaml-rust"]
unstable = [] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
nightly = [] # for building with unstable Rust features (currently none)
lints = ["clippy"] # Requires nightly Rust
debug = [] # Enables debug messages
no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros
doc = ["yaml"] # All the features which add to documentation
default = ["suggestions", "color", "vec_map"]
suggestions = ["strsim"]
color = ["ansi_term", "atty"]
wrap_help = ["term_size", "textwrap/term_size"]
yaml = ["yaml-rust"]
iter_matches = ["indexmap"]
unstable = [] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
nightly = [] # for building with unstable Rust features (currently none)
lints = ["clippy"] # Requires nightly Rust
debug = [] # Enables debug messages
no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros
doc = ["yaml"] # All the features which add to documentation

[profile.release]
opt-level = 3
Expand Down
8 changes: 8 additions & 0 deletions src/args/arg_matcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Std
#[cfg(not(feature = "iter_matches"))]
use std::collections::hash_map::{Entry, Iter};
#[cfg(not(feature = "iter_matches"))]
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ops::Deref;
Expand All @@ -10,6 +12,12 @@ use args::{ArgMatches, MatchedArg, SubCommand};
use args::AnyArg;
use args::settings::ArgSettings;

// iter_matches specific
#[cfg(feature = "iter_matches")] extern crate indexmap;
#[cfg(feature = "iter_matches")] use self::indexmap::map::{Entry, Iter};
#[cfg(feature = "iter_matches")] use self::indexmap::IndexMap as HashMap;


#[doc(hidden)]
#[allow(missing_debug_implementations)]
pub struct ArgMatcher<'a>(pub ArgMatches<'a>);
Expand Down
7 changes: 6 additions & 1 deletion src/args/arg_matches.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// iter_matches specific
#[cfg(feature = "iter_matches")]
use indexmap::IndexMap as HashMap;

// Std
use std::borrow::Cow;
#[cfg(not(feature = "iter_matches"))]
use std::collections::HashMap;
use std::borrow::Cow;
use std::ffi::{OsStr, OsString};
use std::iter::Map;
use std::slice::Iter;
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ extern crate atty;
extern crate bitflags;
#[cfg(feature = "suggestions")]
extern crate strsim;
#[cfg(feature = "iter_matches")]
extern crate indexmap;
#[cfg(feature = "wrap_help")]
extern crate term_size;
extern crate textwrap;
Expand Down
15 changes: 15 additions & 0 deletions tests/iter_matches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate clap;

use clap::{App, Arg};

#![cfg(feature = "iter_matches")]
#[test]
fn iter_matches_empty() {
let res = App::new("prog")
.arg(Arg::with_name("cfg")
.takes_value(true)
.long("config"))
.get_matches_from_safe(vec!["prog"]);
assert!(res.is_ok());
assert_eq!(res.unwrap().args.iter().count(), 0);
}