Skip to content

Commit

Permalink
imp: adds '[SUBCOMMAND]' to usage strings with only AppSettings::Allo…
Browse files Browse the repository at this point in the history
…wExternalSubcommands is used with no other subcommands

Closes #1093
  • Loading branch information
kbknapp committed Nov 7, 2017
1 parent fb08bb5 commit e78bb75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/app/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn create_help_usage(p: &Parser, incl_reqs: bool) -> String {
// supporting multiple values
if p.opts.iter().any(|o| o.is_set(ArgSettings::Multiple)) &&
p.positionals.values().any(|p| !p.is_set(ArgSettings::Required)) &&
!p.has_visible_subcommands() && !has_last {
!(p.has_visible_subcommands() || p.is_set(AS::AllowExternalSubcommands)) && !has_last {
usage.push_str(" [--]");
}
let not_req_or_hidden =
Expand Down Expand Up @@ -131,7 +131,7 @@ pub fn create_help_usage(p: &Parser, incl_reqs: bool) -> String {
}

// incl_reqs is only false when this function is called recursively
if p.has_visible_subcommands() && incl_reqs {
if p.has_visible_subcommands() && incl_reqs || p.is_set(AS::AllowExternalSubcommands) {
if p.is_set(AS::SubcommandsNegateReqs) || p.is_set(AS::ArgsNegateSubcommands) {
if !p.is_set(AS::ArgsNegateSubcommands) {
usage.push_str("\n ");
Expand Down
17 changes: 17 additions & 0 deletions tests/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ use clap::{App, Arg, SubCommand, AppSettings, ErrorKind};

include!("../clap-test.rs");

static ALLOW_EXT_SC: &'static str = "clap-test v1.4.8
USAGE:
clap-test [SUBCOMMAND]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information";

static DONT_COLLAPSE_ARGS: &'static str = "clap-test v1.4.8
USAGE:
Expand Down Expand Up @@ -651,4 +660,12 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {

assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
}

#[test]
fn issue_1093_allow_ext_sc() {
let app = App::new("clap-test")
.version("v1.4.8")
.setting(AppSettings::AllowExternalSubcommands);
assert!(test::compare_output(app, "clap-test --help", ALLOW_EXT_SC, false));
}

0 comments on commit e78bb75

Please sign in to comment.