clap/tests/help_env.rs
Ed Page 61c9e6265b fix(help)!: Merge OPTIONS / FLAGS default groups
For those that want the original behavior, you can usxe
`arg.help_heading(Some("FLAGS"))` on your flags.  Limitations:
- This will not give you a special sort order
- This will not get a `[FLAGS]` added to usage

For templates, we removed `{unified}` and `{flags}`.  To help people
catch these, a debug_assert was added.

I'm unsure but I think there might be a change in behavior in calcuating
when to show `[OPTION]` in usage.  The old code only looked at
`required` while flags looked only at arg groups.  We now look at both.

Ideally we'd add these in `_build` and remove special casing for
no-groups except in the sort order of groups.  I feel like thats best
left for later.

This also reduced the scope of `App`s public API.
`get_*_with_no_heading` seemed a bit specialized to be in the public
API.  #2853 looks at splitting it out into its own PR.

BREAKING CHANGE: Multiple
- `UnifiedHelpMessage` removed
- `{flags}` and `{unified}` are removed and will assert when present.
- `get_*_with_no_heading` removed

Fixes #2807
2021-10-13 11:42:10 -05:00

257 lines
5.5 KiB
Rust

#![cfg(feature = "env")]
use std::env;
use clap::{App, Arg};
mod utils;
static HIDE_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café.
-h, --help Print help information
-V, --version Print version information
";
static SHOW_ENV: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
-h, --help Print help information
-V, --version Print version information
";
static HIDE_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR]
-h, --help Print help information
-V, --version Print version information
";
static SHOW_ENV_VALS: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
OPTIONS:
-c, --cafe <FILE> A coffeehouse, coffee shop, or café. [env: ENVVAR=MYVAL]
-h, --help Print help information
-V, --version Print version information
";
static HIDE_ENV_FLAG: &str = "ctest 0.1
USAGE:
ctest [OPTIONS]
OPTIONS:
-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 [OPTIONS]
OPTIONS:
-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 [OPTIONS]
OPTIONS:
-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 [OPTIONS]
OPTIONS:
-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
));
}