Skip to content

Commit

Permalink
api(Arg): adds Arg::hide_env_values(bool) which allows one to hide an…
Browse files Browse the repository at this point in the history
…y current env values and display only the key in help messages
  • Loading branch information
kbknapp committed Dec 2, 2017
1 parent a652db0 commit fb41d06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/app/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,17 @@ impl<'a> Help<'a> {
env.0,
env.1
);
spec_vals.push(format!(
" [env: {}={}]",
let env_val = if !a.is_set(ArgSettings::HideEnvValues) {
format!("={}", env.1.map_or(Cow::Borrowed(""), |val| val.to_string_lossy()))
} else {
String::new()
};
let env_info = format!(
" [env: {}{}]",
env.0.to_string_lossy(),
env.1.map_or(Cow::Borrowed(""), |val| val.to_string_lossy())
));
env_val
);
spec_vals.push(env_info);
}
if !a.is_set(ArgSettings::HideDefaultValue) {
if let Some(pv) = a.default_val() {
Expand Down
9 changes: 9 additions & 0 deletions src/args/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3515,6 +3515,15 @@ impl<'a, 'b> Arg<'a, 'b> {
self
}

/// @TODO @p2 @docs @release: write docs
pub fn hide_env_values(self, hide: bool) -> Self {
if hide {
self.set(ArgSettings::HideEnvValues)
} else {
self.unset(ArgSettings::HideEnvValues)
}
}

/// When set to `true` the help string will be displayed on the line after the argument and
/// indented once. This can be helpful for arguments with very long or complex help messages.
/// This can also be helpful for arguments with very long flag names, or many/long value names.
Expand Down
9 changes: 9 additions & 0 deletions src/args/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bitflags! {
const LAST = 1 << 14;
const HIDE_DEFAULT_VAL = 1 << 15;
const CASE_INSENSITIVE = 1 << 16;
const HIDE_ENV_VALS = 1 << 17;
}
}

Expand Down Expand Up @@ -49,6 +50,7 @@ impl ArgFlags {
RequireEquals => Flags::REQUIRE_EQUALS,
Last => Flags::LAST,
CaseInsensitive => Flags::CASE_INSENSITIVE,
HideEnvValues => Flags::HIDE_ENV_VALS,
HideDefaultValue => Flags::HIDE_DEFAULT_VAL
}
}
Expand Down Expand Up @@ -96,6 +98,8 @@ pub enum ArgSettings {
HideDefaultValue,
/// Makes `Arg::possible_values` case insensitive
CaseInsensitive,
/// Hides ENV values in the help message
HideEnvValues,
#[doc(hidden)] RequiredUnlessAll,
#[doc(hidden)] ValueDelimiterNotSet,
}
Expand All @@ -121,6 +125,7 @@ impl FromStr for ArgSettings {
"last" => Ok(ArgSettings::Last),
"hidedefaultvalue" => Ok(ArgSettings::HideDefaultValue),
"caseinsensitive" => Ok(ArgSettings::CaseInsensitive),
"hideenvvalues" => Ok(ArgSettings::HideEnvValues),
_ => Err("unknown ArgSetting, cannot convert from str".to_owned()),
}
}
Expand Down Expand Up @@ -197,6 +202,10 @@ mod test {
"caseinsensitive".parse::<ArgSettings>().unwrap(),
ArgSettings::CaseInsensitive
);
assert_eq!(
"hideenvvalues".parse::<ArgSettings>().unwrap(),
ArgSettings::HideEnvValues
);
assert!("hahahaha".parse::<ArgSettings>().is_err());
}
}

0 comments on commit fb41d06

Please sign in to comment.