Ditch flags!

This commit is contained in:
CreepySkeleton 2020-06-18 22:34:45 +03:00
parent f72b728ed7
commit f46af5b96b
5 changed files with 16 additions and 22 deletions

View file

@ -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<Arg> {
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(

View file

@ -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<Item = &Arg<'b>> {
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 {

View file

@ -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 {

View file

@ -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::<Vec<_>>();
self.write_args(&*opts_flags)?;
self.write_args(&opts_flags)?;
}
b"flags" => {
self.write_args(&*flags!(self.parser.app).collect::<Vec<_>>())?;
self.write_args(&self.parser.app.get_flags_no_heading().collect::<Vec<_>>())?;
}
b"options" => {
self.write_args(&*opts!(self.parser.app).collect::<Vec<_>>())?;

View file

@ -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" {