mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 14:22:34 +00:00
api(Arg::hide_default_value): adds ability to hide the default value of an argument from the help string
Adds a new method, `Arg::hide_default_value`, which allows for specifying whether the default value of the argument should be hidden from the help string. Closes #902
This commit is contained in:
parent
0b4177f151
commit
89e6ea861e
3 changed files with 68 additions and 25 deletions
|
@ -498,14 +498,16 @@ impl<'a> Help<'a> {
|
||||||
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
|
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
|
||||||
debugln!("Help::spec_vals: a={}", a);
|
debugln!("Help::spec_vals: a={}", a);
|
||||||
let mut spec_vals = vec![];
|
let mut spec_vals = vec![];
|
||||||
if let Some(pv) = a.default_val() {
|
if !a.is_set(ArgSettings::HideDefaultValue) {
|
||||||
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
|
if let Some(pv) = a.default_val() {
|
||||||
spec_vals.push(format!(" [default: {}]",
|
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
|
||||||
if self.color {
|
spec_vals.push(format!(" [default: {}]",
|
||||||
self.cizer.good(pv.to_string_lossy())
|
if self.color {
|
||||||
} else {
|
self.cizer.good(pv.to_string_lossy())
|
||||||
Format::None(pv.to_string_lossy())
|
} else {
|
||||||
}));
|
Format::None(pv.to_string_lossy())
|
||||||
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Some(ref aliases) = a.aliases() {
|
if let Some(ref aliases) = a.aliases() {
|
||||||
debugln!("Help::spec_vals: Found aliases...{:?}", aliases);
|
debugln!("Help::spec_vals: Found aliases...{:?}", aliases);
|
||||||
|
|
|
@ -1707,6 +1707,40 @@ impl<'a, 'b> Arg<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Specifies if the default value of an argument should be displayed in the help text or
|
||||||
|
/// not. Defaults to `false` (i.e. show default value)
|
||||||
|
///
|
||||||
|
/// This is useful when default behavior of an arg is explained elsewhere in the help text.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
/// Arg::with_name("config")
|
||||||
|
/// .hide_default_value(true)
|
||||||
|
/// # ;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use clap::{App, Arg};
|
||||||
|
/// let m = App::new("connect")
|
||||||
|
/// .arg(Arg::with_name("host")
|
||||||
|
/// .long("host")
|
||||||
|
/// .default_value("localhost")
|
||||||
|
/// .hide_default_value(true));
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// If we were to run the above program with `--help` the `[default: localhost]` portion of
|
||||||
|
/// the help text would be omitted.
|
||||||
|
pub fn hide_default_value(self, hide: bool) -> Self {
|
||||||
|
if hide {
|
||||||
|
self.set(ArgSettings::HideDefaultValue)
|
||||||
|
} else {
|
||||||
|
self.unset(ArgSettings::HideDefaultValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Specifies the index of a positional argument **starting at** 1.
|
/// Specifies the index of a positional argument **starting at** 1.
|
||||||
///
|
///
|
||||||
/// **NOTE:** The index refers to position according to **other positional argument**. It does
|
/// **NOTE:** The index refers to position according to **other positional argument**. It does
|
||||||
|
|
|
@ -4,21 +4,22 @@ use std::str::FromStr;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
flags Flags: u16 {
|
flags Flags: u16 {
|
||||||
const REQUIRED = 1 << 0,
|
const REQUIRED = 1 << 0,
|
||||||
const MULTIPLE = 1 << 1,
|
const MULTIPLE = 1 << 1,
|
||||||
const EMPTY_VALS = 1 << 2,
|
const EMPTY_VALS = 1 << 2,
|
||||||
const GLOBAL = 1 << 3,
|
const GLOBAL = 1 << 3,
|
||||||
const HIDDEN = 1 << 4,
|
const HIDDEN = 1 << 4,
|
||||||
const TAKES_VAL = 1 << 5,
|
const TAKES_VAL = 1 << 5,
|
||||||
const USE_DELIM = 1 << 6,
|
const USE_DELIM = 1 << 6,
|
||||||
const NEXT_LINE_HELP = 1 << 7,
|
const NEXT_LINE_HELP = 1 << 7,
|
||||||
const R_UNLESS_ALL = 1 << 8,
|
const R_UNLESS_ALL = 1 << 8,
|
||||||
const REQ_DELIM = 1 << 9,
|
const REQ_DELIM = 1 << 9,
|
||||||
const DELIM_NOT_SET = 1 << 10,
|
const DELIM_NOT_SET = 1 << 10,
|
||||||
const HIDE_POS_VALS = 1 << 11,
|
const HIDE_POS_VALS = 1 << 11,
|
||||||
const ALLOW_TAC_VALS = 1 << 12,
|
const ALLOW_TAC_VALS = 1 << 12,
|
||||||
const REQUIRE_EQUALS = 1 << 13,
|
const REQUIRE_EQUALS = 1 << 13,
|
||||||
const LAST = 1 << 14,
|
const LAST = 1 << 14,
|
||||||
|
const HIDE_DEFAULT_VAL = 1 << 15,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +45,8 @@ impl ArgFlags {
|
||||||
HidePossibleValues => HIDE_POS_VALS,
|
HidePossibleValues => HIDE_POS_VALS,
|
||||||
AllowLeadingHyphen => ALLOW_TAC_VALS,
|
AllowLeadingHyphen => ALLOW_TAC_VALS,
|
||||||
RequireEquals => REQUIRE_EQUALS,
|
RequireEquals => REQUIRE_EQUALS,
|
||||||
Last => LAST
|
Last => LAST,
|
||||||
|
HideDefaultValue => HIDE_DEFAULT_VAL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,6 +89,8 @@ pub enum ArgSettings {
|
||||||
/// Specifies that the arg is the last positional argument and may be accessed early via `--`
|
/// Specifies that the arg is the last positional argument and may be accessed early via `--`
|
||||||
/// syntax
|
/// syntax
|
||||||
Last,
|
Last,
|
||||||
|
/// Hides the default value from the help string
|
||||||
|
HideDefaultValue,
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
RequiredUnlessAll,
|
RequiredUnlessAll,
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@ -112,6 +116,7 @@ impl FromStr for ArgSettings {
|
||||||
"allowleadinghyphen" => Ok(ArgSettings::AllowLeadingHyphen),
|
"allowleadinghyphen" => Ok(ArgSettings::AllowLeadingHyphen),
|
||||||
"requireequals" => Ok(ArgSettings::RequireEquals),
|
"requireequals" => Ok(ArgSettings::RequireEquals),
|
||||||
"last" => Ok(ArgSettings::Last),
|
"last" => Ok(ArgSettings::Last),
|
||||||
|
"hidedefaultvalue" => Ok(ArgSettings::HideDefaultValue),
|
||||||
_ => Err("unknown ArgSetting, cannot convert from str".to_owned()),
|
_ => Err("unknown ArgSetting, cannot convert from str".to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,6 +158,8 @@ mod test {
|
||||||
ArgSettings::RequireEquals);
|
ArgSettings::RequireEquals);
|
||||||
assert_eq!("last".parse::<ArgSettings>().unwrap(),
|
assert_eq!("last".parse::<ArgSettings>().unwrap(),
|
||||||
ArgSettings::Last);
|
ArgSettings::Last);
|
||||||
|
assert_eq!("hidedefaultvalue".parse::<ArgSettings>().unwrap(),
|
||||||
|
ArgSettings::HideDefaultValue);
|
||||||
assert!("hahahaha".parse::<ArgSettings>().is_err());
|
assert!("hahahaha".parse::<ArgSettings>().is_err());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue