Skip to content

Commit

Permalink
Merge pull request #4566 from hitenkoku/fix/4565
Browse files Browse the repository at this point in the history
removed {bin} variable from help_template
  • Loading branch information
epage committed Dec 22, 2022
2 parents 957288c + b72ad58 commit 9242e1d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion clap_bench/benches/04_new_help.rs
Expand Up @@ -199,7 +199,7 @@ pub fn example10(c: &mut Criterion) {
}

pub fn example4_template(c: &mut Criterion) {
let mut cmd = app_example4().help_template("{bin} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
let mut cmd = app_example4().help_template("{name} {version}\n{author}\n{about}\n\nUSAGE:\n {usage}\n\nOPTIONS:\n{options}\n\nARGS:\n{args}\n");
c.bench_function("example4_template", |b| b.iter(|| build_help(&mut cmd)));
}

Expand Down
2 changes: 1 addition & 1 deletion clap_bench/benches/05_ripgrep.rs
Expand Up @@ -256,7 +256,7 @@ const USAGE: &str = "
rg [OPTIONS] --type-list";

const TEMPLATE: &str = "\
{bin} {version}
{name} {version}
{author}
{about}
Expand Down
4 changes: 2 additions & 2 deletions src/builder/command.rs
Expand Up @@ -1744,7 +1744,7 @@ impl Command {
/// Valid tags are:
///
/// * `{name}` - Display name for the (sub-)command.
/// * `{bin}` - Binary name.
/// * `{bin}` - Binary name.(deprecated)
/// * `{version}` - Version number.
/// * `{author}` - Author information.
/// * `{author-with-newline}` - Author followed by `\n`.
Expand Down Expand Up @@ -1772,7 +1772,7 @@ impl Command {
/// # use clap::Command;
/// Command::new("myprog")
/// .version("1.0")
/// .help_template("{bin} ({version}) - {usage}")
/// .help_template("{name} ({version}) - {usage}")
/// # ;
/// ```
///
Expand Down
7 changes: 7 additions & 0 deletions src/builder/debug_asserts.rs
Expand Up @@ -359,6 +359,13 @@ pub(crate) fn assert_app(cmd: &Command) {
cmd.get_name(),
"`{unified}` template variable was removed in clap3, use `{options}` instead"
);
#[cfg(feature = "unstable-v5")]
assert!(
!help_template.to_string().contains("{bin}"),
"Command {}: {}",
cmd.get_name(),
"`{bin}` template variable was removed in clap5, use `{name}` instead"
)
}

cmd._panic_on_missing_help(cmd.is_help_expected_set());
Expand Down
1 change: 1 addition & 0 deletions src/output/help_template.rs
Expand Up @@ -137,6 +137,7 @@ impl<'cmd, 'writer> HelpTemplate<'cmd, 'writer> {
"name" => {
self.write_display_name();
}
#[cfg(not(feature = "unstable-v5"))]
"bin" => {
self.write_bin_name();
}
Expand Down
21 changes: 21 additions & 0 deletions tests/builder/help.rs
Expand Up @@ -1042,6 +1042,7 @@ Options:
-V, --version Print version information
";

#[cfg(not(feature = "unstable-v5"))]
let cmd = Command::new("ripgrep")
.version("0.5")
.override_usage(
Expand All @@ -1057,6 +1058,26 @@ Options:
Usage: {usage}
Options:
{options}",
);

#[cfg(feature = "unstable-v5")]
let cmd = Command::new("ripgrep")
.version("0.5")
.override_usage(
"\
rg [OPTIONS] <pattern> [<path> ...]
rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
rg [OPTIONS] --files [<path> ...]
rg [OPTIONS] --type-list",
)
.help_template(
"\
{name} {version}
Usage: {usage}
Options:
{options}",
);
Expand Down
34 changes: 34 additions & 0 deletions tests/builder/template_help.rs
Expand Up @@ -2,6 +2,7 @@ use super::utils;

use clap::{arg, Command};

#[cfg(not(feature = "unstable-v5"))]
static EXAMPLE1_TMPL_S: &str = "{bin} {version}
{author}
{about}
Expand All @@ -10,6 +11,16 @@ Usage: {usage}
{all-args}";

#[cfg(feature = "unstable-v5")]
static EXAMPLE1_TMPL_S: &str = "{name} {version}
{author}
{about}
Usage: {usage}
{all-args}";

#[cfg(not(feature = "unstable-v5"))]
static EXAMPLE1_TMPS_F: &str = "{bin} {version}
{author}
{about}
Expand All @@ -23,6 +34,20 @@ Arguments:
Commands:
{subcommands}";

#[cfg(feature = "unstable-v5")]
static EXAMPLE1_TMPS_F: &str = "{name} {version}
{author}
{about}
Usage: {usage}
Options:
{options}
Arguments:
{positionals}
Commands:
{subcommands}";

static CUSTOM_TEMPL_HELP: &str = "MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
Expand Down Expand Up @@ -105,11 +130,20 @@ fn template_unknowntag() {

#[test]
fn template_author_version() {
#[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")]
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{name}");

utils::assert_output(
cmd,
"MyApp --help",
Expand Down

0 comments on commit 9242e1d

Please sign in to comment.