Navigation Menu

Skip to content

Commit

Permalink
feat(Help): adds ability to hide the possible values on a per argumen…
Browse files Browse the repository at this point in the history
…t basis

Previously one could only hide the possible values of an argument application or command wide, and
not on a per argument basis. Now one can use the `Arg::hide_possible_values(bool)` method to hide
only that arguments possible values.

Closes #640
  • Loading branch information
kbknapp committed Sep 11, 2016
1 parent 82234fa commit 9151ef7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/app/help.rs
Expand Up @@ -495,7 +495,7 @@ impl<'a> Help<'a> {
} else {
Format::None(pv)
},
if self.hide_pv {
if self.hide_pv || a.is_set(ArgSettings::HidePossibleValues) {
"".into()
} else {
if let Some(ref pv) = a.possible_vals() {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl<'a> Help<'a> {
} else {
aliases.join(", ")
});
} else if !self.hide_pv {
} else if !self.hide_pv && !a.is_set(ArgSettings::HidePossibleValues) {
debugln!("Writing values");
if let Some(pv) = a.possible_vals() {
debugln!("Possible vals...{:?}", pv);
Expand Down
36 changes: 36 additions & 0 deletions src/args/arg.rs
Expand Up @@ -1081,6 +1081,42 @@ impl<'a, 'b> Arg<'a, 'b> {
}
}

/// Specifies if the possible values of an argument should be displayed in the help text or
/// not. Defaults to `false` (i.e. show possible values)
///
/// This is useful for args with many values, or ones which are explained elsewhere in the
/// help text.
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg};
/// Arg::with_name("config")
/// .hide_possible_values(true)
/// # ;
/// ```
///
/// ```rust
/// # use clap::{App, Arg};
/// let m = App::new("hide_posvals")
/// .arg(Arg::with_name("mode")
/// .long("mode")
/// .possible_values(&["fast", "slow"])
/// .takes_value(true)
/// .hide_possible_values(true));
///
/// ```
///
/// If we were to run the above program with `--help` the `[values: fast, slow]` portion of
/// the help text would be omitted.
pub fn hide_possible_values(self, hide: bool) -> Self {
if hide {
self.set(ArgSettings::HidePossibleValues)
} else {
self.unset(ArgSettings::HidePossibleValues)
}
}

/// Specifies the index of a positional argument **starting at** 1.
///
/// **NOTE:** The index refers to position according to **other positional argument**. It does
Expand Down
29 changes: 17 additions & 12 deletions src/args/settings.rs
Expand Up @@ -4,17 +4,18 @@ use std::str::FromStr;

bitflags! {
flags Flags: u16 {
const REQUIRED = 0b00000000001,
const MULTIPLE = 0b00000000010,
const EMPTY_VALS = 0b00000000100,
const GLOBAL = 0b00000001000,
const HIDDEN = 0b00000010000,
const TAKES_VAL = 0b00000100000,
const USE_DELIM = 0b00001000000,
const NEXT_LINE_HELP = 0b00010000000,
const R_UNLESS_ALL = 0b00100000000,
const REQ_DELIM = 0b01000000000,
const DELIM_NOT_SET = 0b10000000000,
const REQUIRED = 0b000000000001,
const MULTIPLE = 0b000000000010,
const EMPTY_VALS = 0b000000000100,
const GLOBAL = 0b000000001000,
const HIDDEN = 0b000000010000,
const TAKES_VAL = 0b000000100000,
const USE_DELIM = 0b000001000000,
const NEXT_LINE_HELP = 0b000010000000,
const R_UNLESS_ALL = 0b000100000000,
const REQ_DELIM = 0b001000000000,
const DELIM_NOT_SET = 0b010000000000,
const HIDE_POS_VALS = 0b100000000000,
}
}

Expand All @@ -38,7 +39,8 @@ impl ArgFlags {
NextLineHelp => NEXT_LINE_HELP,
RequiredUnlessAll => R_UNLESS_ALL,
RequireDelimiter => REQ_DELIM,
ValueDelimiterNotSet => DELIM_NOT_SET
ValueDelimiterNotSet => DELIM_NOT_SET,
HidePossibleValues => HIDE_POS_VALS
}
}

Expand Down Expand Up @@ -74,6 +76,8 @@ pub enum ArgSettings {
NextLineHelp,
/// Requires the use of a value delimiter for all multiple values
RequireDelimiter,
/// Hides the possible values from the help string
HidePossibleValues,
#[doc(hidden)]
RequiredUnlessAll,
#[doc(hidden)]
Expand All @@ -95,6 +99,7 @@ impl FromStr for ArgSettings {
"requiredunlessall" => Ok(ArgSettings::RequiredUnlessAll),
"requiredelimiter" => Ok(ArgSettings::RequireDelimiter),
"valuedelimiternotset" => Ok(ArgSettings::ValueDelimiterNotSet),
"hidepossiblevalues" => Ok(ArgSettings::HidePossibleValues),
_ => Err("unknown ArgSetting, cannot convert from str".to_owned()),
}
}
Expand Down

0 comments on commit 9151ef7

Please sign in to comment.