clap/tests/builder/template_help.rs

180 lines
4.1 KiB
Rust
Raw Normal View History

use super::utils;
2022-02-12 03:48:29 +00:00
use clap::{arg, Command};
2022-12-21 15:47:53 +00:00
#[cfg(not(feature = "unstable-v5"))]
static EXAMPLE1_TMPL_S: &str = "{bin} {version}
{author}
{about}
Usage: {usage}
{all-args}";
#[cfg(feature = "unstable-v5")]
static EXAMPLE1_TMPL_S: &str = "{name} {version}
2020-02-04 08:10:53 +00:00
{author}
{about}
Usage: {usage}
2020-02-04 08:10:53 +00:00
{all-args}";
2022-12-21 15:47:53 +00:00
#[cfg(not(feature = "unstable-v5"))]
static EXAMPLE1_TMPS_F: &str = "{bin} {version}
{author}
{about}
Usage: {usage}
Options:
{options}
Arguments:
{positionals}
Commands:
{subcommands}";
#[cfg(feature = "unstable-v5")]
static EXAMPLE1_TMPS_F: &str = "{name} {version}
2020-02-04 08:10:53 +00:00
{author}
{about}
Usage: {usage}
2020-02-04 08:10:53 +00:00
Options:
2020-02-04 08:10:53 +00:00
{options}
Arguments:
2020-02-04 08:10:53 +00:00
{positionals}
Commands:
2020-02-04 08:10:53 +00:00
{subcommands}";
static CUSTOM_TEMPL_HELP: &str = "MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
Usage: MyApp [OPTIONS] <output> [COMMAND]
Options:
-c, --config <FILE> Sets a custom config file
-d... Turn debugging information on
-h, --help Print help
-V, --version Print version
Arguments:
<output> Sets an optional output file
Commands:
test does testing things
help Print this message or the help of the given subcommand(s)
";
static SIMPLE_TEMPLATE: &str = "MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
Usage: MyApp [OPTIONS] <output> [COMMAND]
Commands:
test does testing things
help Print this message or the help of the given subcommand(s)
Arguments:
<output> Sets an optional output file
Options:
-c, --config <FILE> Sets a custom config file
-d... Turn debugging information on
-h, --help Print help
-V, --version Print version
";
#[test]
fn with_template() {
2022-02-14 21:47:20 +00:00
let cmd = get_app().help_template(EXAMPLE1_TMPL_S);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "MyApp --help", SIMPLE_TEMPLATE, false);
}
#[test]
fn custom_template() {
2022-02-14 21:47:20 +00:00
let cmd = get_app().help_template(EXAMPLE1_TMPS_F);
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "MyApp --help", CUSTOM_TEMPL_HELP, false);
}
#[test]
fn template_empty() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("");
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "MyApp --help", "\n", false);
}
#[test]
fn template_notag() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("test no tag test");
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "MyApp --help", "test no tag test\n", false);
}
#[test]
fn template_unknowntag() {
2022-02-14 21:47:20 +00:00
let cmd = Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("test {unknown_tag} test");
2022-04-29 20:32:25 +00:00
utils::assert_output(cmd, "MyApp --help", "test {unknown_tag} test\n", false);
}
#[test]
fn template_author_version() {
2022-12-21 15:47:53 +00:00
#[cfg(not(feature = "unstable-v5"))]
let cmd = Command::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("{author}\n{version}\n{about}\n{bin}");
#[cfg(feature = "unstable-v5")]
2022-02-14 21:47:20 +00:00
let cmd = Command::new("MyApp")
2018-01-25 04:05:05 +00:00
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.help_template("{author}\n{version}\n{about}\n{name}");
2022-12-21 15:47:53 +00:00
2022-04-29 20:32:25 +00:00
utils::assert_output(
2022-02-14 21:47:20 +00:00
cmd,
2018-01-25 04:05:05 +00:00
"MyApp --help",
"Kevin K. <kbknapp@gmail.com>\n1.0\nDoes awesome things\nMyApp\n",
2022-04-29 20:32:25 +00:00
false,
);
}
// ----------
fn get_app() -> Command {
2022-02-12 03:48:29 +00:00
Command::new("MyApp")
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
2021-11-19 20:33:11 +00:00
.arg(
arg!(
-c --config <FILE> "Sets a custom config file"
)
.required(false),
)
.arg(arg!(
<output> "Sets an optional output file"
))
2021-11-19 20:33:11 +00:00
.arg(arg!(
d: -d ... "Turn debugging information on"
))
2018-01-25 04:05:05 +00:00
.subcommand(
2022-02-12 03:48:29 +00:00
Command::new("test")
2018-01-25 04:05:05 +00:00
.about("does testing things")
2021-11-19 20:33:11 +00:00
.arg(arg!(-l --list "lists test values")),
2018-01-25 04:05:05 +00:00
)
}