From f46af5b96b5c5c49dd7ae972bc2c4730df2bde6e Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Thu, 18 Jun 2020 22:34:45 +0300 Subject: [PATCH] Ditch flags! --- clap_generate/src/generators/mod.rs | 4 ++-- src/build/app/mod.rs | 10 +++++++++- src/macros.rs | 14 -------------- src/output/help.rs | 8 ++++---- src/output/usage.rs | 2 +- 5 files changed, 16 insertions(+), 22 deletions(-) diff --git a/clap_generate/src/generators/mod.rs b/clap_generate/src/generators/mod.rs index e7710850..4cf7d284 100644 --- a/clap_generate/src/generators/mod.rs +++ b/clap_generate/src/generators/mod.rs @@ -4,7 +4,7 @@ mod shells; use std::io::Write; // Internal -use clap::{find_subcmd, flags, match_alias, App, AppSettings, Arg}; +use clap::{find_subcmd, match_alias, App, AppSettings, Arg}; pub use shells::*; /// Generator trait which can be used to write generators @@ -179,7 +179,7 @@ pub trait Generator { fn flags<'b>(p: &'b App<'b>) -> Vec { debug!("flags: name={}", p.get_name()); - let mut flags: Vec<_> = flags!(p).cloned().collect(); + let mut flags: Vec<_> = p.get_flags_no_heading().cloned().collect(); if flags.iter().find(|x| x.get_name() == "help").is_none() { flags.push( diff --git a/src/build/app/mod.rs b/src/build/app/mod.rs index 372152d4..3cbed189 100644 --- a/src/build/app/mod.rs +++ b/src/build/app/mod.rs @@ -151,6 +151,14 @@ impl<'b> App<'b> { &self.args.args } + /// Iterate through the *flags* that don't have custom heading. + pub fn get_flags_no_heading(&self) -> impl Iterator> { + self.get_arguments() + .iter() + .filter(|a| !a.is_set(ArgSettings::TakesValue) && a.get_index().is_none()) + .filter(|a| !a.get_help_heading().is_some()) + } + /// Get the list of arguments the given argument conflicts with /// /// ### Panics @@ -1959,7 +1967,7 @@ impl<'b> App<'b> { } pub(crate) fn has_flags(&self) -> bool { - flags!(self).count() > 0 + self.get_flags_no_heading().count() > 0 } pub(crate) fn has_visible_subcommands(&self) -> bool { diff --git a/src/macros.rs b/src/macros.rs index 3a4f3f5b..73b99edd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -553,20 +553,6 @@ macro_rules! debug { ($($arg:tt)*) => {}; } -#[macro_export] -#[doc(hidden)] -macro_rules! flags { - ($app:expr, $how:ident) => {{ - $app.get_arguments() - .$how() - .filter(|a| !a.is_set($crate::ArgSettings::TakesValue) && a.get_index().is_none()) - .filter(|a| !a.get_help_heading().is_some()) - }}; - ($app:expr) => { - $crate::flags!($app, iter) - }; -} - #[macro_export] #[doc(hidden)] macro_rules! opts { diff --git a/src/output/help.rs b/src/output/help.rs index dbbddd9e..4cab0f28 100644 --- a/src/output/help.rs +++ b/src/output/help.rs @@ -709,8 +709,8 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { self.none("\n\n")?; } self.warning("FLAGS:\n")?; - let flags_v: Vec<_> = flags!(self.parser.app).collect(); - self.write_args(&*flags_v)?; + let flags_v: Vec<_> = self.parser.app.get_flags_no_heading().collect(); + self.write_args(&flags_v)?; first = false; } if !opts.is_empty() { @@ -1053,10 +1053,10 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> { .iter() .filter(|a| a.has_switch()) .collect::>(); - self.write_args(&*opts_flags)?; + self.write_args(&opts_flags)?; } b"flags" => { - self.write_args(&*flags!(self.parser.app).collect::>())?; + self.write_args(&self.parser.app.get_flags_no_heading().collect::>())?; } b"options" => { self.write_args(&*opts!(self.parser.app).collect::>())?; diff --git a/src/output/usage.rs b/src/output/usage.rs index 3bd0e82d..97379d28 100644 --- a/src/output/usage.rs +++ b/src/output/usage.rs @@ -275,7 +275,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> { // Determines if we need the `[FLAGS]` tag in the usage string fn needs_flags_tag(&self) -> bool { debug!("Usage::needs_flags_tag"); - 'outer: for f in flags!(self.p.app) { + 'outer: for f in self.p.app.get_flags_no_heading() { debug!("Usage::needs_flags_tag:iter: f={}", f.name); if let Some(l) = f.long { if l == "help" || l == "version" {