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

Add is_env_present to ArgMatches #1833

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/parse/arg_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ impl ArgMatcher {
self.insert(arg);
}

pub(crate) fn set_env_set(&mut self, arg: &Id) {
let ma = self.entry(arg).or_insert(MatchedArg::new());
ma.env_set = true;
}

pub(crate) fn add_val_to(&mut self, arg: &Id, val: &OsStr) {
let ma = self.entry(arg).or_insert(MatchedArg {
env_set: false,
occurs: 0, // @TODO @question Shouldn't this be 1 if we're already adding a value to this arg?
indices: Vec::with_capacity(1),
vals: Vec::with_capacity(1),
Expand All @@ -147,6 +153,7 @@ impl ArgMatcher {

pub(crate) fn add_index_to(&mut self, arg: &Id, idx: usize) {
let ma = self.entry(arg).or_insert(MatchedArg {
env_set: false,
occurs: 0,
indices: Vec::with_capacity(1),
vals: Vec::new(),
Expand Down
39 changes: 39 additions & 0 deletions src/parse/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,45 @@ impl ArgMatches {
})
}

/// Returns true if this arguments environment variable was set.
///
/// This is useful for determining if the argument's value came from the default value or the
/// environment variable.
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg};
/// # use std::env;
/// let app = App::new("myapp")
/// .arg(Arg::with_name("arg")
/// .long("arg")
/// .takes_value(true)
/// .env("ENV_ARG"));
/// let m = app.clone().get_matches_from(vec!["myapp", "--arg=val"]);
/// assert!(!m.is_env_present("arg"));
/// ```
///
/// ```rust
/// # use clap::{App, Arg};
/// # use std::env;
/// env::set_var("ENV_ARG", "from_env");
/// let app = App::new("myapp")
/// .arg(Arg::with_name("arg")
/// .long("arg")
/// .takes_value(true)
/// .env("ENV_ARG"));
/// let m = app.clone().get_matches_from(vec!["myapp", "--arg=val"]);
/// assert!(m.is_env_present("arg"));
/// ```
pub fn is_env_present<T: Key>(&self, id: T) -> bool {
if let Some(arg) = self.args.get(&Id::from(id)) {
arg.env_set
} else {
false
}
}

/// Because [`Subcommand`]s are essentially "sub-[`App`]s" they have their own [`ArgMatches`]
/// as well. This method returns the [`ArgMatches`] for a particular subcommand or `None` if
/// the subcommand wasn't present at runtime.
Expand Down
2 changes: 2 additions & 0 deletions src/parse/matches/matched_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) struct MatchedArg {
pub(crate) occurs: u64,
pub(crate) indices: Vec<usize>,
pub(crate) vals: Vec<OsString>,
pub(crate) env_set: bool,
}

impl Default for MatchedArg {
Expand All @@ -14,6 +15,7 @@ impl Default for MatchedArg {
occurs: 1,
indices: Vec::new(),
vals: Vec::new(),
env_set: false,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,9 @@ where

matcher.add_val_to(&arg.id, v);
matcher.add_index_to(&arg.id, self.cur_idx.get());
if let Some((_, Some(_))) = arg.env {
matcher.set_env_set(&arg.id);
}

// Increment or create the group "args"
for grp in groups_for_arg!(self.app, arg.id) {
Expand Down