mirror of
https://github.com/clap-rs/clap
synced 2024-12-13 22:32:33 +00:00
Improve the logic for help & version building & parsing
This commit is contained in:
parent
e8a06e79cd
commit
5dd9584119
3 changed files with 48 additions and 34 deletions
|
@ -2353,50 +2353,58 @@ impl<'help> App<'help> {
|
|||
pub(crate) fn _create_help_and_version(&mut self) {
|
||||
debug!("App::_create_help_and_version");
|
||||
|
||||
if !(self
|
||||
.args
|
||||
.args
|
||||
.iter()
|
||||
.any(|x| x.long == Some("help") || x.id == Id::help_hash())
|
||||
|| self.is_set(AppSettings::DisableHelpFlag)
|
||||
if !(self.is_set(AppSettings::DisableHelpFlag)
|
||||
|| self
|
||||
.args
|
||||
.args
|
||||
.iter()
|
||||
.any(|x| x.long == Some("help") || x.id == Id::help_hash())
|
||||
|| self
|
||||
.subcommands
|
||||
.iter()
|
||||
.any(|sc| sc.short_flag == Some('h') || sc.long_flag == Some("help")))
|
||||
.any(|sc| sc.long_flag == Some("help")))
|
||||
{
|
||||
debug!("App::_create_help_and_version: Building --help");
|
||||
let mut help = Arg::new("help")
|
||||
.long("help")
|
||||
.about(self.help_about.unwrap_or("Prints help information"));
|
||||
if !self.args.args.iter().any(|x| x.short == Some('h')) {
|
||||
|
||||
if !(self.args.args.iter().any(|x| x.short == Some('h'))
|
||||
|| self.subcommands.iter().any(|sc| sc.short_flag == Some('h')))
|
||||
{
|
||||
help = help.short('h');
|
||||
}
|
||||
|
||||
self.args.push(help);
|
||||
}
|
||||
if !(self
|
||||
.args
|
||||
.args
|
||||
.iter()
|
||||
.any(|x| x.long == Some("version") || x.id == Id::version_hash())
|
||||
|| self.is_set(AppSettings::DisableVersionFlag)
|
||||
|
||||
if !(self.is_set(AppSettings::DisableVersionFlag)
|
||||
|| self
|
||||
.args
|
||||
.args
|
||||
.iter()
|
||||
.any(|x| x.long == Some("version") || x.id == Id::version_hash())
|
||||
|| self
|
||||
.subcommands
|
||||
.iter()
|
||||
.any(|sc| sc.short_flag == Some('V') || sc.long_flag == Some("version")))
|
||||
.any(|sc| sc.long_flag == Some("version")))
|
||||
{
|
||||
debug!("App::_create_help_and_version: Building --version");
|
||||
let mut version = Arg::new("version")
|
||||
.long("version")
|
||||
.about(self.version_about.unwrap_or("Prints version information"));
|
||||
if !self.args.args.iter().any(|x| x.short == Some('V')) {
|
||||
|
||||
if !(self.args.args.iter().any(|x| x.short == Some('V'))
|
||||
|| self.subcommands.iter().any(|sc| sc.short_flag == Some('V')))
|
||||
{
|
||||
version = version.short('V');
|
||||
}
|
||||
|
||||
self.args.push(version);
|
||||
}
|
||||
if self.has_subcommands()
|
||||
&& !self.is_set(AppSettings::DisableHelpSubcommand)
|
||||
|
||||
if !self.is_set(AppSettings::DisableHelpSubcommand)
|
||||
&& self.has_subcommands()
|
||||
&& !self.subcommands.iter().any(|s| s.id == Id::help_hash())
|
||||
{
|
||||
debug!("App::_create_help_and_version: Building help");
|
||||
|
|
|
@ -1118,18 +1118,25 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
arg
|
||||
);
|
||||
|
||||
// Needs to use app.settings.is_set instead of just is_set() because is_set() checks
|
||||
// both global and local settings, we only want to check local
|
||||
if arg == "help" && !self.app.settings.is_set(AS::NoAutoHelp) {
|
||||
debug!("Help");
|
||||
return Err(self.help_err(true));
|
||||
if let Some(help) = self.app.find(&Id::help_hash()) {
|
||||
if let Some(h) = help.long {
|
||||
if arg == h && !self.is_set(AS::NoAutoHelp) {
|
||||
debug!("Help");
|
||||
return Err(self.help_err(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
if arg == "version" && !self.app.settings.is_set(AS::NoAutoVersion) {
|
||||
debug!("Version");
|
||||
return Err(self.version_err(true));
|
||||
}
|
||||
debug!("Neither");
|
||||
|
||||
if let Some(version) = self.app.find(&Id::version_hash()) {
|
||||
if let Some(v) = version.long {
|
||||
if arg == v && !self.is_set(AS::NoAutoVersion) {
|
||||
debug!("Version");
|
||||
return Err(self.version_err(true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Neither");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -1139,24 +1146,25 @@ impl<'help, 'app> Parser<'help, 'app> {
|
|||
"Parser::check_for_help_and_version_char: Checking if -{} is help or version...",
|
||||
arg
|
||||
);
|
||||
// Needs to use app.settings.is_set instead of just is_set() because is_set() checks
|
||||
// both global and local settings, we only want to check local
|
||||
|
||||
if let Some(help) = self.app.find(&Id::help_hash()) {
|
||||
if let Some(h) = help.short {
|
||||
if arg == h && !self.app.settings.is_set(AS::NoAutoHelp) {
|
||||
if arg == h && !self.is_set(AS::NoAutoHelp) {
|
||||
debug!("Help");
|
||||
return Err(self.help_err(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(version) = self.app.find(&Id::version_hash()) {
|
||||
if let Some(v) = version.short {
|
||||
if arg == v && !self.app.settings.is_set(AS::NoAutoVersion) {
|
||||
if arg == v && !self.is_set(AS::NoAutoVersion) {
|
||||
debug!("Version");
|
||||
return Err(self.version_err(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Neither");
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1448,8 +1448,6 @@ fn escaped_whitespace_values() {
|
|||
|
||||
fn issue_1112_setup() -> App<'static> {
|
||||
App::new("test")
|
||||
.author("Kevin K.")
|
||||
.about("tests stuff")
|
||||
.version("1.3")
|
||||
.global_setting(AppSettings::NoAutoHelp)
|
||||
.arg(Arg::from("-h, --help 'some help'"))
|
||||
|
|
Loading…
Reference in a new issue