From 27df8b9d98d13709dad3929a009f40ebff089a1a Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 29 Oct 2015 00:23:05 -0400 Subject: [PATCH] fix(Versionless SubCommands): fixes a bug where the -V flag was needlessly built Closes #329 --- src/app/app.rs | 32 +++++++++++--------------------- src/app/errors.rs | 2 ++ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/app/app.rs b/src/app/app.rs index be6f6f0f03c..d33800cd8e0 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -991,7 +991,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ self.settings.set(&AppSettings::NeedsSubcommandHelp); } if self.settings.is_set(&AppSettings::VersionlessSubcommands) { - self.settings.set(&AppSettings::DisableVersion); + subcmd.settings.set(&AppSettings::DisableVersion); } if self.settings.is_set(&AppSettings::GlobalVersion) && subcmd.version.is_none() && self.version.is_some() { @@ -1514,6 +1514,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ } // flush the buffer + debugln!("Flushing the buffer..."); w.flush() } @@ -2325,7 +2326,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }); } // process::exit(0); @@ -2582,7 +2583,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }), } } @@ -2603,7 +2604,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }), } } @@ -2686,11 +2687,8 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ }; self.long_list.push("help"); self.flags.insert("hclap_help", arg); - // self.settings.unset(&AppSettings::NeedsLongHelp); } - if !self.settings.is_set(&AppSettings::VersionlessSubcommands) || - (self.settings.is_set(&AppSettings::VersionlessSubcommands) && - self.settings.is_set(&AppSettings::DisableVersion)) && + if !self.settings.is_set(&AppSettings::DisableVersion) && !self.flags.values().any(|a| a.long.is_some() && a.long.unwrap() == "version") { if self.version_short.is_none() && !self.short_list.contains(&'V') { self.version_short = Some('V'); @@ -2724,7 +2722,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }); } return Err(ClapError { @@ -2743,7 +2741,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }); } return Err(ClapError { @@ -2769,7 +2767,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }); } return Err(ClapError { @@ -2785,7 +2783,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ Format::Error("error:"), INTERNAL_ERROR_MSG, e.description()), - error_type: ClapErrorType::MissingSubcommand, + error_type: ClapErrorType::InternalError, }); } return Err(ClapError { @@ -3153,15 +3151,7 @@ impl<'a, 'v, 'ab, 'u, 'h, 'ar> App<'a, 'v, 'ab, 'u, 'h, 'ar>{ -> Result, ClapError> { let arg = &full_arg[..].trim_left_matches(|c| c == '-'); for c in arg.chars() { - if let Err(e) = self.check_for_help_and_version(c) { - return Err(ClapError { - error: format!("{} {}\n\terror message: {}\n", - Format::Error("error:"), - INTERNAL_ERROR_MSG, - e.description()), - error_type: ClapErrorType::MissingSubcommand, - }); - } + try!(self.check_for_help_and_version(c)); // Check for matching short in options, and return the name // (only ones with shorts, of course) diff --git a/src/app/errors.rs b/src/app/errors.rs index 5b60c7e3b89..9109efd3b4a 100644 --- a/src/app/errors.rs +++ b/src/app/errors.rs @@ -251,6 +251,8 @@ pub enum ClapErrorType { /// assert_eq!(result.unwrap_err().error_type, ClapErrorType::VersionDisplayed); /// ``` VersionDisplayed, + /// Represents an internal error, please consider filing a bug report if this happens! + InternalError, } /// Command line argument parser error