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

160 lines
3.6 KiB
Rust

mod utils;
use clap::App;
static EXAMPLE1_TMPL_S: &str = "{bin} {version}
{author}
{about}
USAGE:
{usage}
{all-args}";
static EXAMPLE1_TMPS_F: &str = "{bin} {version}
{author}
{about}
USAGE:
{usage}
OPTIONS:
{options}
ARGS:
{positionals}
SUBCOMMANDS:
{subcommands}";
static CUSTOM_TEMPL_HELP: &str = "MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
USAGE:
MyApp [OPTIONS] <output> [SUBCOMMAND]
OPTIONS:
-c, --config <FILE> Sets a custom config file
-d Turn debugging information on
-h, --help Print help information
-V, --version Print version information
ARGS:
<output> Sets an optional output file
SUBCOMMANDS:
help Print this message or the help of the given subcommand(s)
test does testing things
";
static SIMPLE_TEMPLATE: &str = "MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
USAGE:
MyApp [OPTIONS] <output> [SUBCOMMAND]
ARGS:
<output> Sets an optional output file
OPTIONS:
-c, --config <FILE> Sets a custom config file
-d Turn debugging information on
-h, --help Print help information
-V, --version Print version information
SUBCOMMANDS:
help Print this message or the help of the given subcommand(s)
test does testing things
";
#[test]
fn with_template() {
let app = get_app().help_template(EXAMPLE1_TMPL_S);
assert!(utils::compare_output(
app,
"MyApp --help",
SIMPLE_TEMPLATE,
false
));
}
#[test]
fn custom_template() {
let app = get_app().help_template(EXAMPLE1_TMPS_F);
assert!(utils::compare_output(
app,
"MyApp --help",
CUSTOM_TEMPL_HELP,
false
));
}
#[test]
fn template_empty() {
let app = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("");
assert!(utils::compare_output(app, "MyApp --help", "\n", false));
}
#[test]
fn template_notag() {
let app = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("test no tag test");
assert!(utils::compare_output(
app,
"MyApp --help",
"test no tag test\n",
false
));
}
#[test]
fn template_unknowntag() {
let app = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("test {unknown_tag} test");
assert!(utils::compare_output(
app,
"MyApp --help",
"test {unknown_tag} test\n",
false
));
}
#[test]
fn template_author_version() {
let app = App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("{author}\n{version}\n{about}\n{bin}");
assert!(utils::compare_output(
app,
"MyApp --help",
"Kevin K. <kbknapp@gmail.com>\n1.0\nDoes awesome things\nMyApp\n",
false
));
}
// ----------
fn get_app() -> App<'static> {
App::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.arg("-c, --config=[FILE] 'Sets a custom config file'")
.arg("<output> 'Sets an optional output file'")
.arg("-d... 'Turn debugging information on'")
.subcommand(
App::new("test")
.about("does testing things")
.arg("-l, --list 'lists test values'"),
)
}