Skip to content

Commit

Permalink
fix(help): Subcommand help looks like --help
Browse files Browse the repository at this point in the history
Like was said in clap-rs#2435, this is what people would expect.

While we should note this in a compatibility section in the changelog, I
do not consider this a breaking change since we should be free to adjust
the help output as needed.  We are cautious when people might build up
their own content around it (like clap-rs#3312) but apps should already handle
this with `--help` so this shouldn't be a major change.

We aren't offering a way for people to disable this, assuming people
won't need to.  Longer term, we are looking at support "Actions" (clap-rs#3405)
and expect those to help customize the flags.  We'll need something
similar for the `help` subcommand.

Fixes clap-rs#3440
  • Loading branch information
epage committed Feb 11, 2022
1 parent 272f840 commit 0fc83e4
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 130 deletions.
1 change: 0 additions & 1 deletion examples/tutorial_builder/03_04_subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use clap::{app_from_crate, arg, App, AppSettings};
fn main() {
let matches = app_from_crate!()
.propagate_version(true)
.global_setting(AppSettings::UseLongFormatForHelpSubcommand)
.setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(
App::new("add")
Expand Down
3 changes: 1 addition & 2 deletions examples/tutorial_derive/03_04_subcommands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use clap::{AppSettings, Parser, Subcommand};
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[clap(author, version, about, long_about = None)]
#[clap(propagate_version = true)]
#[clap(global_setting(AppSettings::UseLongFormatForHelpSubcommand))]
struct Cli {
#[clap(subcommand)]
command: Commands,
Expand Down
22 changes: 2 additions & 20 deletions src/build/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,26 +176,8 @@ pub enum AppSettings {
)]
AllowInvalidUtf8ForExternalSubcommands,

/// Specifies that the help subcommand should print the long help message (`--help`).
///
/// **NOTE:** This setting is useless if [`AppSettings::DisableHelpSubcommand`] or [`AppSettings::NoAutoHelp`] is set,
/// or if the app contains no subcommands at all.
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg, AppSettings};
/// App::new("myprog")
/// .global_setting(AppSettings::UseLongFormatForHelpSubcommand)
/// .subcommand(App::new("test")
/// .arg(Arg::new("foo")
/// .help("short form about message")
/// .long_help("long form about message")
/// )
/// )
/// .get_matches();
/// ```
/// [long format]: crate::App::long_about
/// Deprecated, this is now the default
#[deprecated(since = "3.1.0", note = "This is now the default")]
UseLongFormatForHelpSubcommand,

/// Deprecated, replaced with [`App::subcommand_negates_reqs`] and
Expand Down
2 changes: 1 addition & 1 deletion src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl<'help, 'app> Parser<'help, 'app> {

let parser = Parser::new(&mut sc);

Err(parser.help_err(self.app.is_set(AS::UseLongFormatForHelpSubcommand)))
Err(parser.help_err(true))
}

fn is_new_arg(&self, next: &RawOsStr, current_positional: &Arg) -> bool {
Expand Down
105 changes: 0 additions & 105 deletions tests/builder/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,54 +79,6 @@ SUBCOMMANDS:
info
";

static LONG_FORMAT_FOR_HELP_SUBCOMMAND: &str = "myprog-test
USAGE:
myprog test [foo]
ARGS:
<foo>
long form help message
OPTIONS:
-h, --help
Print help information
";

static LONG_FORMAT_FOR_NESTED_HELP_SUBCOMMAND: &str = "myprog-test-nested
long form about message
USAGE:
myprog test nested
OPTIONS:
-h, --help
Print help information
";

static LONG_FORMAT_SINGLE_ARG_HELP_SUBCOMMAND: &str = "myprog 1.0
USAGE:
myprog [foo] [SUBCOMMAND]
ARGS:
<foo>
long form help message
OPTIONS:
-h, --help
Print help information
-V, --version
Print version information
SUBCOMMANDS:
help
Print this message or the help of the given subcommand(s)
test
";

#[test]
fn setting() {
#![allow(deprecated)]
Expand Down Expand Up @@ -1137,63 +1089,6 @@ fn aaos_option_use_delim_false() {
);
}

#[test]
fn nested_help_subcommand_with_global_setting() {
let m = App::new("myprog")
.global_setting(AppSettings::UseLongFormatForHelpSubcommand)
.subcommand(
App::new("test").subcommand(
App::new("nested")
.about("short form about message")
.long_about("long form about message"),
),
);
assert!(utils::compare_output(
m,
"myprog test help nested",
LONG_FORMAT_FOR_NESTED_HELP_SUBCOMMAND,
false
));
}

#[test]
fn single_arg_help_with_long_format_setting() {
let m = App::new("myprog")
.version("1.0")
.setting(AppSettings::UseLongFormatForHelpSubcommand)
.subcommand(App::new("test"))
.arg(
Arg::new("foo")
.help("short form help message")
.long_help("long form help message"),
);
assert!(utils::compare_output(
m,
"myprog help",
LONG_FORMAT_SINGLE_ARG_HELP_SUBCOMMAND,
false
));
}

#[test]
fn use_long_format_for_help_subcommand_with_setting() {
let m = App::new("myprog")
.setting(AppSettings::UseLongFormatForHelpSubcommand)
.subcommand(
App::new("test").arg(
Arg::new("foo")
.help("short form help message")
.long_help("long form help message"),
),
);
assert!(utils::compare_output(
m,
"myprog help test",
LONG_FORMAT_FOR_HELP_SUBCOMMAND,
false
));
}

#[test]
fn no_auto_help() {
let app = App::new("myprog")
Expand Down
1 change: 0 additions & 1 deletion tests/derive/structopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn compatible() {
#[derive(StructOpt)]
#[structopt(author, version, about)]
#[structopt(global_setting(AppSettings::PropagateVersion))]
#[structopt(global_setting(AppSettings::UseLongFormatForHelpSubcommand))]
struct Cli {
#[structopt(subcommand)]
command: Commands,
Expand Down

0 comments on commit 0fc83e4

Please sign in to comment.