Skip to content

Commit

Permalink
Test of get_matches_with_env
Browse files Browse the repository at this point in the history
  • Loading branch information
purew committed Nov 6, 2016
1 parent 907ff78 commit df90d4a
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use errors::Result as ClapResult;
pub use self::settings::AppSettings;
use completions::Shell;

const DEFAULT_ARGS_ENV_VAR: &'static str = "VAR";

/// Used to create a representation of a command line program and all possible command line
/// arguments. Application settings are set using the "builder pattern" with the
/// [`App::get_matches`] family of methods being the terminal methods that starts the
Expand Down Expand Up @@ -1228,13 +1230,13 @@ impl<'a, 'b> App<'a, 'b> {
self.get_matches_from_safe(&mut env::args_os())
}

/// Similar to [`App::get_matches`] but also reads args from
/// Similar to [`App::get_matches`] but also reads args from
/// env-var `env_var_name`.
///
/// Defaults to read from "VAR" if `env_var_name` is `None`.
pub fn get_matches_with_env(self, env_var_name: Option<String>)
pub fn get_matches_with_env(self, env_var_name: Option<String>)
-> ArgMatches<'a> {
let mut env_var = "VAR".to_string();
let mut env_var = DEFAULT_ARGS_ENV_VAR.to_string();
if let Some(name) = env_var_name {
env_var = name;
}
Expand Down Expand Up @@ -1624,3 +1626,31 @@ impl<'n, 'e> fmt::Display for App<'n, 'e> {
write!(f, "{}", self.p.meta.name)
}
}

#[cfg(test)]
mod tests {

use std::env;

use ::{Arg, App};
use super::DEFAULT_ARGS_ENV_VAR;


#[test]
fn test_get_matches_with_env() {
env::set_var(DEFAULT_ARGS_ENV_VAR, "--arg1 val1");
let mut matches = App::new("testprog").arg(Arg::with_name("arg1")
.takes_value(true)
.long("arg1"))
.get_matches_with_env(None);
assert_eq!(matches.value_of("arg1").unwrap(), "val1");

env::set_var("ENV_VAR_WITH_PROGRAM_ARGS", "--arg2 val2");
matches = App::new("testprog")
.arg(Arg::with_name("arg2")
.takes_value(true)
.long("arg2"))
.get_matches_with_env(Some("ENV_VAR_WITH_PROGRAM_ARGS".to_string()));
assert_eq!(matches.value_of("arg2").unwrap(), "val2");
}
}

0 comments on commit df90d4a

Please sign in to comment.