From e78bb757a3df16e82d539e450c06767a6bfcf859 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Mon, 6 Nov 2017 20:15:04 -0500 Subject: [PATCH] imp: adds '[SUBCOMMAND]' to usage strings with only AppSettings::AllowExternalSubcommands is used with no other subcommands Closes #1093 --- src/app/usage.rs | 4 ++-- tests/app_settings.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/app/usage.rs b/src/app/usage.rs index 39489dc75ae..cd2fb3bbb0b 100644 --- a/src/app/usage.rs +++ b/src/app/usage.rs @@ -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 = @@ -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 "); diff --git a/tests/app_settings.rs b/tests/app_settings.rs index 7d99364ffc9..258ebd7025a 100644 --- a/tests/app_settings.rs +++ b/tests/app_settings.rs @@ -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: @@ -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)); } \ No newline at end of file