Skip to content

Commit ac5cd37

Browse files
committed
fix(usage): Don't include irrelevant parent args
This was identified in #4134
1 parent 19bc3b7 commit ac5cd37

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/builder/command.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4336,7 +4336,8 @@ impl<'help> App<'help> {
43364336
use std::fmt::Write;
43374337

43384338
let mut mid_string = String::from(" ");
4339-
if !self.is_subcommand_negates_reqs_set() {
4339+
if !self.is_subcommand_negates_reqs_set() && !self.is_args_conflicts_with_subcommands_set()
4340+
{
43404341
let reqs = Usage::new(self).get_required_usage_from(&[], None, true); // maybe Some(m)
43414342

43424343
for s in &reqs {
@@ -4419,7 +4420,9 @@ impl<'help> App<'help> {
44194420

44204421
if !self.is_set(AppSettings::BinNameBuilt) {
44214422
let mut mid_string = String::from(" ");
4422-
if !self.is_subcommand_negates_reqs_set() {
4423+
if !self.is_subcommand_negates_reqs_set()
4424+
&& !self.is_args_conflicts_with_subcommands_set()
4425+
{
44234426
let reqs = Usage::new(self).get_required_usage_from(&[], None, true); // maybe Some(m)
44244427

44254428
for s in &reqs {

tests/builder/help.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -829,14 +829,6 @@ fn multi_level_sc_help() {
829829
utils::assert_output(cmd, "ctest help subcmd multi", MULTI_SC_HELP, false);
830830
}
831831

832-
#[test]
833-
fn no_wrap_help() {
834-
let cmd = Command::new("ctest")
835-
.term_width(0)
836-
.override_help(MULTI_SC_HELP);
837-
utils::assert_output(cmd, "ctest --help", &format!("{}\n", MULTI_SC_HELP), false);
838-
}
839-
840832
#[test]
841833
fn no_wrap_default_help() {
842834
let cmd = Command::new("ctest").version("1.0").term_width(0);
@@ -2857,6 +2849,50 @@ OPTIONS:
28572849
utils::assert_eq(EXPECTED, String::from_utf8(buf).unwrap());
28582850
}
28592851

2852+
#[test]
2853+
fn parent_cmd_req_ignored_when_negates_reqs() {
2854+
static MULTI_SC_HELP: &str = "ctest-subcmd
2855+
2856+
USAGE:
2857+
ctest subcmd
2858+
2859+
OPTIONS:
2860+
-h, --help Print help information
2861+
";
2862+
2863+
let cmd = Command::new("ctest")
2864+
.arg(arg!(<input>))
2865+
.subcommand_negates_reqs(true)
2866+
.subcommand(Command::new("subcmd"));
2867+
utils::assert_output(cmd, "ctest subcmd --help", MULTI_SC_HELP, false);
2868+
}
2869+
2870+
#[test]
2871+
fn parent_cmd_req_ignored_when_conflicts() {
2872+
static MULTI_SC_HELP: &str = "ctest-subcmd
2873+
2874+
USAGE:
2875+
ctest subcmd
2876+
2877+
OPTIONS:
2878+
-h, --help Print help information
2879+
";
2880+
2881+
let cmd = Command::new("ctest")
2882+
.arg(arg!(<input>))
2883+
.args_conflicts_with_subcommands(true)
2884+
.subcommand(Command::new("subcmd"));
2885+
utils::assert_output(cmd, "ctest subcmd --help", MULTI_SC_HELP, false);
2886+
}
2887+
2888+
#[test]
2889+
fn no_wrap_help() {
2890+
let cmd = Command::new("ctest")
2891+
.term_width(0)
2892+
.override_help(MULTI_SC_HELP);
2893+
utils::assert_output(cmd, "ctest --help", &format!("{}\n", MULTI_SC_HELP), false);
2894+
}
2895+
28602896
#[test]
28612897
fn display_name_default() {
28622898
let mut cmd = Command::new("app").bin_name("app.exe");

0 commit comments

Comments
 (0)