mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
Ditch flags!
This commit is contained in:
parent
f72b728ed7
commit
f46af5b96b
5 changed files with 16 additions and 22 deletions
|
@ -4,7 +4,7 @@ mod shells;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
use clap::{find_subcmd, flags, match_alias, App, AppSettings, Arg};
|
use clap::{find_subcmd, match_alias, App, AppSettings, Arg};
|
||||||
pub use shells::*;
|
pub use shells::*;
|
||||||
|
|
||||||
/// Generator trait which can be used to write generators
|
/// 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> {
|
fn flags<'b>(p: &'b App<'b>) -> Vec<Arg> {
|
||||||
debug!("flags: name={}", p.get_name());
|
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() {
|
if flags.iter().find(|x| x.get_name() == "help").is_none() {
|
||||||
flags.push(
|
flags.push(
|
||||||
|
|
|
@ -151,6 +151,14 @@ impl<'b> App<'b> {
|
||||||
&self.args.args
|
&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
|
/// Get the list of arguments the given argument conflicts with
|
||||||
///
|
///
|
||||||
/// ### Panics
|
/// ### Panics
|
||||||
|
@ -1959,7 +1967,7 @@ impl<'b> App<'b> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn has_flags(&self) -> bool {
|
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 {
|
pub(crate) fn has_visible_subcommands(&self) -> bool {
|
||||||
|
|
|
@ -553,20 +553,6 @@ macro_rules! debug {
|
||||||
($($arg:tt)*) => {};
|
($($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]
|
#[macro_export]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
macro_rules! opts {
|
macro_rules! opts {
|
||||||
|
|
|
@ -709,8 +709,8 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
|
||||||
self.none("\n\n")?;
|
self.none("\n\n")?;
|
||||||
}
|
}
|
||||||
self.warning("FLAGS:\n")?;
|
self.warning("FLAGS:\n")?;
|
||||||
let flags_v: Vec<_> = flags!(self.parser.app).collect();
|
let flags_v: Vec<_> = self.parser.app.get_flags_no_heading().collect();
|
||||||
self.write_args(&*flags_v)?;
|
self.write_args(&flags_v)?;
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
if !opts.is_empty() {
|
if !opts.is_empty() {
|
||||||
|
@ -1053,10 +1053,10 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|a| a.has_switch())
|
.filter(|a| a.has_switch())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
self.write_args(&*opts_flags)?;
|
self.write_args(&opts_flags)?;
|
||||||
}
|
}
|
||||||
b"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" => {
|
b"options" => {
|
||||||
self.write_args(&*opts!(self.parser.app).collect::<Vec<_>>())?;
|
self.write_args(&*opts!(self.parser.app).collect::<Vec<_>>())?;
|
||||||
|
|
|
@ -275,7 +275,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> {
|
||||||
// Determines if we need the `[FLAGS]` tag in the usage string
|
// Determines if we need the `[FLAGS]` tag in the usage string
|
||||||
fn needs_flags_tag(&self) -> bool {
|
fn needs_flags_tag(&self) -> bool {
|
||||||
debug!("Usage::needs_flags_tag");
|
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);
|
debug!("Usage::needs_flags_tag:iter: f={}", f.name);
|
||||||
if let Some(l) = f.long {
|
if let Some(l) = f.long {
|
||||||
if l == "help" || l == "version" {
|
if l == "help" || l == "version" {
|
||||||
|
|
Loading…
Reference in a new issue