Skip to content

Commit 89e6ea8

Browse files
crazymerlynkbknapp
authored andcommitted
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
1 parent 0b4177f commit 89e6ea8

File tree

3 files changed

+68
-25
lines changed

3 files changed

+68
-25
lines changed

src/app/help.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,16 @@ impl<'a> Help<'a> {
498498
fn spec_vals(&self, a: &ArgWithDisplay) -> String {
499499
debugln!("Help::spec_vals: a={}", a);
500500
let mut spec_vals = vec![];
501-
if let Some(pv) = a.default_val() {
502-
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
503-
spec_vals.push(format!(" [default: {}]",
504-
if self.color {
505-
self.cizer.good(pv.to_string_lossy())
506-
} else {
507-
Format::None(pv.to_string_lossy())
508-
}));
501+
if !a.is_set(ArgSettings::HideDefaultValue) {
502+
if let Some(pv) = a.default_val() {
503+
debugln!("Help::spec_vals: Found default value...[{:?}]", pv);
504+
spec_vals.push(format!(" [default: {}]",
505+
if self.color {
506+
self.cizer.good(pv.to_string_lossy())
507+
} else {
508+
Format::None(pv.to_string_lossy())
509+
}));
510+
}
509511
}
510512
if let Some(ref aliases) = a.aliases() {
511513
debugln!("Help::spec_vals: Found aliases...{:?}", aliases);

src/args/arg.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,40 @@ impl<'a, 'b> Arg<'a, 'b> {
17071707
}
17081708
}
17091709

1710+
/// Specifies if the default value of an argument should be displayed in the help text or
1711+
/// not. Defaults to `false` (i.e. show default value)
1712+
///
1713+
/// This is useful when default behavior of an arg is explained elsewhere in the help text.
1714+
///
1715+
/// # Examples
1716+
///
1717+
/// ```rust
1718+
/// # use clap::{App, Arg};
1719+
/// Arg::with_name("config")
1720+
/// .hide_default_value(true)
1721+
/// # ;
1722+
/// ```
1723+
///
1724+
/// ```rust
1725+
/// # use clap::{App, Arg};
1726+
/// let m = App::new("connect")
1727+
/// .arg(Arg::with_name("host")
1728+
/// .long("host")
1729+
/// .default_value("localhost")
1730+
/// .hide_default_value(true));
1731+
///
1732+
/// ```
1733+
///
1734+
/// If we were to run the above program with `--help` the `[default: localhost]` portion of
1735+
/// the help text would be omitted.
1736+
pub fn hide_default_value(self, hide: bool) -> Self {
1737+
if hide {
1738+
self.set(ArgSettings::HideDefaultValue)
1739+
} else {
1740+
self.unset(ArgSettings::HideDefaultValue)
1741+
}
1742+
}
1743+
17101744
/// Specifies the index of a positional argument **starting at** 1.
17111745
///
17121746
/// **NOTE:** The index refers to position according to **other positional argument**. It does
@@ -3332,4 +3366,4 @@ impl<'n, 'e> PartialEq for Arg<'n, 'e> {
33323366
fn eq(&self, other: &Arg<'n, 'e>) -> bool {
33333367
self.b == other.b
33343368
}
3335-
}
3369+
}

src/args/settings.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ use std::str::FromStr;
44

55
bitflags! {
66
flags Flags: u16 {
7-
const REQUIRED = 1 << 0,
8-
const MULTIPLE = 1 << 1,
9-
const EMPTY_VALS = 1 << 2,
10-
const GLOBAL = 1 << 3,
11-
const HIDDEN = 1 << 4,
12-
const TAKES_VAL = 1 << 5,
13-
const USE_DELIM = 1 << 6,
14-
const NEXT_LINE_HELP = 1 << 7,
15-
const R_UNLESS_ALL = 1 << 8,
16-
const REQ_DELIM = 1 << 9,
17-
const DELIM_NOT_SET = 1 << 10,
18-
const HIDE_POS_VALS = 1 << 11,
19-
const ALLOW_TAC_VALS = 1 << 12,
20-
const REQUIRE_EQUALS = 1 << 13,
21-
const LAST = 1 << 14,
7+
const REQUIRED = 1 << 0,
8+
const MULTIPLE = 1 << 1,
9+
const EMPTY_VALS = 1 << 2,
10+
const GLOBAL = 1 << 3,
11+
const HIDDEN = 1 << 4,
12+
const TAKES_VAL = 1 << 5,
13+
const USE_DELIM = 1 << 6,
14+
const NEXT_LINE_HELP = 1 << 7,
15+
const R_UNLESS_ALL = 1 << 8,
16+
const REQ_DELIM = 1 << 9,
17+
const DELIM_NOT_SET = 1 << 10,
18+
const HIDE_POS_VALS = 1 << 11,
19+
const ALLOW_TAC_VALS = 1 << 12,
20+
const REQUIRE_EQUALS = 1 << 13,
21+
const LAST = 1 << 14,
22+
const HIDE_DEFAULT_VAL = 1 << 15,
2223
}
2324
}
2425

@@ -44,7 +45,8 @@ impl ArgFlags {
4445
HidePossibleValues => HIDE_POS_VALS,
4546
AllowLeadingHyphen => ALLOW_TAC_VALS,
4647
RequireEquals => REQUIRE_EQUALS,
47-
Last => LAST
48+
Last => LAST,
49+
HideDefaultValue => HIDE_DEFAULT_VAL
4850
}
4951
}
5052

@@ -87,6 +89,8 @@ pub enum ArgSettings {
8789
/// Specifies that the arg is the last positional argument and may be accessed early via `--`
8890
/// syntax
8991
Last,
92+
/// Hides the default value from the help string
93+
HideDefaultValue,
9094
#[doc(hidden)]
9195
RequiredUnlessAll,
9296
#[doc(hidden)]
@@ -112,6 +116,7 @@ impl FromStr for ArgSettings {
112116
"allowleadinghyphen" => Ok(ArgSettings::AllowLeadingHyphen),
113117
"requireequals" => Ok(ArgSettings::RequireEquals),
114118
"last" => Ok(ArgSettings::Last),
119+
"hidedefaultvalue" => Ok(ArgSettings::HideDefaultValue),
115120
_ => Err("unknown ArgSetting, cannot convert from str".to_owned()),
116121
}
117122
}
@@ -153,6 +158,8 @@ mod test {
153158
ArgSettings::RequireEquals);
154159
assert_eq!("last".parse::<ArgSettings>().unwrap(),
155160
ArgSettings::Last);
161+
assert_eq!("hidedefaultvalue".parse::<ArgSettings>().unwrap(),
162+
ArgSettings::HideDefaultValue);
156163
assert!("hahahaha".parse::<ArgSettings>().is_err());
157164
}
158165
}

0 commit comments

Comments
 (0)