clap/tests/hidden_args.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

346 lines
7 KiB
Rust

mod utils;
use clap::{App, AppSettings, Arg};
static HIDDEN_ARGS: &str = "test 1.4
Kevin K.
tests stuff
USAGE:
test [OPTIONS]
OPTIONS:
-F, --flag2 some other flag
-h, --help Print help information
--option <opt> some option
-V, --version Print version information
";
#[test]
fn hidden_args() {
let app = App::new("test")
.author("Kevin K.")
.about("tests stuff")
.version("1.4")
.args(&[
Arg::from("-f, --flag 'some flag'").hidden(true),
Arg::from("-F, --flag2 'some other flag'"),
Arg::from("--option [opt] 'some option'"),
Arg::new("DUMMY").hidden(true),
]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_ARGS,
false
));
}
static HIDDEN_SHORT_ARGS: &str = "test 2.31.2
Steve P.
hides short args
USAGE:
test [OPTIONS]
OPTIONS:
-h, --help Print help information
-v, --visible This text should be visible
-V, --version Print version information
";
static HIDDEN_SHORT_ARGS_LONG_HELP: &str = "test 2.31.2
Steve P.
hides short args
USAGE:
test [OPTIONS]
OPTIONS:
-c, --config
Some help text describing the --config arg
-h, --help
Print help information
-v, --visible
This text should be visible
-V, --version
Print version information
";
/// Ensure hidden with short option
#[test]
fn hidden_short_args() {
let app = App::new("test")
.about("hides short args")
.author("Steve P.")
.version("2.31.2")
.args(&[
Arg::new("cfg")
.short('c')
.long("config")
.hidden_short_help(true)
.about("Some help text describing the --config arg"),
Arg::new("visible")
.short('v')
.long("visible")
.about("This text should be visible"),
]);
assert!(utils::compare_output(
app,
"test -h",
HIDDEN_SHORT_ARGS,
false
));
}
/// Ensure visible with opposite option
#[test]
fn hidden_short_args_long_help() {
let app = App::new("test")
.about("hides short args")
.author("Steve P.")
.version("2.31.2")
.args(&[
Arg::new("cfg")
.short('c')
.long("config")
.hidden_short_help(true)
.about("Some help text describing the --config arg"),
Arg::new("visible")
.short('v')
.long("visible")
.about("This text should be visible"),
]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_SHORT_ARGS_LONG_HELP,
false
));
}
static HIDDEN_LONG_ARGS: &str = "test 2.31.2
Steve P.
hides long args
USAGE:
test [OPTIONS]
OPTIONS:
-h, --help
Print help information
-v, --visible
This text should be visible
-V, --version
Print version information
";
#[test]
fn hidden_long_args() {
let app = App::new("test")
.about("hides long args")
.author("Steve P.")
.version("2.31.2")
.args(&[
Arg::new("cfg")
.short('c')
.long("config")
.hidden_long_help(true)
.about("Some help text describing the --config arg"),
Arg::new("visible")
.short('v')
.long("visible")
.about("This text should be visible"),
]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_LONG_ARGS,
false
));
}
static HIDDEN_LONG_ARGS_SHORT_HELP: &str = "test 2.31.2
Steve P.
hides long args
USAGE:
test [OPTIONS]
OPTIONS:
-c, --config Some help text describing the --config arg
-h, --help Print help information
-v, --visible This text should be visible
-V, --version Print version information
";
#[test]
fn hidden_long_args_short_help() {
let app = App::new("test")
.about("hides long args")
.author("Steve P.")
.version("2.31.2")
.args(&[
Arg::new("cfg")
.short('c')
.long("config")
.hidden_long_help(true)
.about("Some help text describing the --config arg"),
Arg::new("visible")
.short('v')
.long("visible")
.about("This text should be visible"),
]);
assert!(utils::compare_output(
app,
"test -h",
HIDDEN_LONG_ARGS_SHORT_HELP,
false
));
}
static HIDDEN_POS_ARGS: &str = "test 1.4
USAGE:
test [another]
ARGS:
<another> another pos
OPTIONS:
-h, --help Print help information
-V, --version Print version information
";
#[test]
fn hidden_pos_args() {
let app = App::new("test").version("1.4").args(&[
Arg::new("pos").about("some pos").hidden(true),
Arg::new("another").about("another pos"),
]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_POS_ARGS,
false
));
}
static HIDDEN_SUBCMDS: &str = "test 1.4
USAGE:
test
OPTIONS:
-h, --help Print help information
-V, --version Print version information
";
#[test]
fn hidden_subcmds() {
let app = App::new("test")
.version("1.4")
.subcommand(App::new("sub").setting(AppSettings::Hidden));
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_SUBCMDS,
false
));
}
static HIDDEN_OPT_ARGS_ONLY: &str = "test 1.4
USAGE:
test
After help
";
#[test]
fn hidden_opt_args_only() {
let app = App::new("test")
.version("1.4")
.after_help("After help")
.mut_arg("help", |a| a.hidden(true))
.mut_arg("version", |a| a.hidden(true))
.args(&[Arg::from("--option [opt] 'some option'").hidden(true)]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_OPT_ARGS_ONLY,
false
));
}
static HIDDEN_POS_ARGS_ONLY: &str = "test 1.4
USAGE:
test
After help
";
#[test]
fn hidden_pos_args_only() {
let app = App::new("test")
.version("1.4")
.after_help("After help")
.mut_arg("help", |a| a.hidden(true))
.mut_arg("version", |a| a.hidden(true))
.args(&[Arg::new("pos").about("some pos").hidden(true)]);
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_POS_ARGS_ONLY,
false
));
}
static HIDDEN_SUBCMDS_ONLY: &str = "test 1.4
USAGE:
test
After help
";
#[test]
fn hidden_subcmds_only() {
let app = App::new("test")
.version("1.4")
.after_help("After help")
.mut_arg("help", |a| a.hidden(true))
.mut_arg("version", |a| a.hidden(true))
.subcommand(App::new("sub").setting(AppSettings::Hidden));
assert!(utils::compare_output(
app,
"test --help",
HIDDEN_SUBCMDS_ONLY,
false
));
}