Merge pull request #2703 from clap-rs/hide_env

Remove TakesValue restriction for HideEnv*
This commit is contained in:
Pavan Kumar Sunkara 2021-08-16 10:45:08 +01:00 committed by GitHub
commit 257adbc882
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 257 additions and 170 deletions

View file

@ -84,9 +84,5 @@ fn assert_app_flags(arg: &Arg) {
checker!(Last requires TakesValue); checker!(Last requires TakesValue);
checker!(HideDefaultValue requires TakesValue); checker!(HideDefaultValue requires TakesValue);
checker!(MultipleValues requires TakesValue); checker!(MultipleValues requires TakesValue);
#[cfg(feature = "env")]
checker!(HideEnv requires TakesValue);
#[cfg(feature = "env")]
checker!(HideEnvValues requires TakesValue);
checker!(IgnoreCase requires TakesValue); checker!(IgnoreCase requires TakesValue);
} }

View file

@ -3927,8 +3927,6 @@ impl<'help> Arg<'help> {
/// ///
/// This is useful when the variable option is explained elsewhere in the help text. /// This is useful when the variable option is explained elsewhere in the help text.
/// ///
/// **NOTE:** Setting this requires [`ArgSettings::TakesValue`]
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
@ -3967,8 +3965,6 @@ impl<'help> Arg<'help> {
/// ///
/// This is useful when ENV vars contain sensitive values. /// This is useful when ENV vars contain sensitive values.
/// ///
/// **NOTE:** Setting this requires [`ArgSettings::TakesValue`]
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust

View file

@ -549,60 +549,6 @@ FLAGS:
-V, --version -V, --version
Print version information"; Print version information";
#[cfg(feature = "env")]
static HIDE_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café.";
#[cfg(feature = "env")]
static SHOW_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]";
#[cfg(feature = "env")]
static HIDE_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR]
-p, --pos <VAL> Some vals [possible values: fast, slow]";
#[cfg(feature = "env")]
static SHOW_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
-p, --pos <VAL> Some vals [possible values: fast, slow]";
static CUSTOM_HELP_SECTION: &str = "blorp 1.4 static CUSTOM_HELP_SECTION: &str = "blorp 1.4
Will M. Will M.
@ -1733,114 +1679,6 @@ fn issue_1052_require_delim_help() {
)); ));
} }
#[cfg(feature = "env")]
#[test]
fn hide_env() {
use std::env;
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_env(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(app, "ctest --help", HIDE_ENV, false));
}
#[cfg(feature = "env")]
#[test]
fn show_env() {
use std::env;
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_env(false)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(app, "ctest --help", SHOW_ENV, false));
}
#[cfg(feature = "env")]
#[test]
fn hide_env_vals() {
use std::env;
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest")
.version("0.1")
.arg(
Arg::new("pos")
.short('p')
.long("pos")
.value_name("VAL")
.possible_values(&["fast", "slow"])
.about("Some vals")
.takes_value(true),
)
.arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_env_values(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(
app,
"ctest --help",
HIDE_ENV_VALS,
false
));
}
#[cfg(feature = "env")]
#[test]
fn show_env_vals() {
use std::env;
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest")
.version("0.1")
.arg(
Arg::new("pos")
.short('p')
.long("pos")
.value_name("VAL")
.possible_values(&["fast", "slow"])
.about("Some vals")
.takes_value(true),
)
.arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_possible_values(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(
app,
"ctest --help",
SHOW_ENV_VALS,
false
));
}
#[test] #[test]
fn custom_headers_headers() { fn custom_headers_headers() {
let app = App::new("blorp") let app = App::new("blorp")

257
tests/help_env.rs Normal file
View file

@ -0,0 +1,257 @@
#![cfg(feature = "env")]
use std::env;
use clap::{App, Arg};
mod utils;
static HIDE_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café.";
static SHOW_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]";
static HIDE_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR]";
static SHOW_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
FLAGS:
-h, --help Print help information
-V, --version Print version information
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]";
static HIDE_ENV_FLAG: &str = "ctest 0.1
USAGE:
ctest [FLAGS]
FLAGS:
-c, --cafe A coffeehouse, coffee shop, or café.
-h, --help Print help information
-V, --version Print version information";
static SHOW_ENV_FLAG: &str = "ctest 0.1
USAGE:
ctest [FLAGS]
FLAGS:
-c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
-h, --help Print help information
-V, --version Print version information";
static HIDE_ENV_VALS_FLAG: &str = "ctest 0.1
USAGE:
ctest [FLAGS]
FLAGS:
-c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR]
-h, --help Print help information
-V, --version Print version information";
static SHOW_ENV_VALS_FLAG: &str = "ctest 0.1
USAGE:
ctest [FLAGS]
FLAGS:
-c, --cafe A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
-h, --help Print help information
-V, --version Print version information";
#[test]
fn hide_env() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_env(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(app, "ctest --help", HIDE_ENV, false));
}
#[test]
fn show_env() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(app, "ctest --help", SHOW_ENV, false));
}
#[test]
fn hide_env_vals() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.hide_env_values(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(
app,
"ctest --help",
HIDE_ENV_VALS,
false
));
}
#[test]
fn show_env_vals() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.value_name("FILE")
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café.")
.takes_value(true),
);
assert!(utils::compare_output(
app,
"ctest --help",
SHOW_ENV_VALS,
false
));
}
#[test]
fn hide_env_flag() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.hide_env(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café."),
);
assert!(utils::compare_output(
app,
"ctest --help",
HIDE_ENV_FLAG,
false
));
}
#[test]
fn show_env_flag() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café."),
);
assert!(utils::compare_output(
app,
"ctest --help",
SHOW_ENV_FLAG,
false
));
}
#[test]
fn hide_env_vals_flag() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.hide_env_values(true)
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café."),
);
assert!(utils::compare_output(
app,
"ctest --help",
HIDE_ENV_VALS_FLAG,
false
));
}
#[test]
fn show_env_vals_flag() {
env::set_var("ENVVAR", "MYVAL");
let app = App::new("ctest").version("0.1").arg(
Arg::new("cafe")
.short('c')
.long("cafe")
.env("ENVVAR")
.about("A coffeehouse, coffee shop, or café."),
);
assert!(utils::compare_output(
app,
"ctest --help",
SHOW_ENV_VALS_FLAG,
false
));
}