Skip to content

Commit

Permalink
Merge pull request clap-rs#2268 from ldm0/fixme
Browse files Browse the repository at this point in the history
Better mkeymap
  • Loading branch information
pksunkara authored Dec 28, 2020
2 parents 4bb7763 + 0f115f1 commit a0269a4
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 167 deletions.
9 changes: 4 additions & 5 deletions src/build/app/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(crate) fn assert_app(app: &App) {
}
}

for arg in &app.args.args {
for arg in app.args.args() {
arg._debug_asserts();

if let Some(s) = arg.short.as_ref() {
Expand Down Expand Up @@ -214,15 +214,15 @@ pub(crate) fn assert_app(app: &App) {

// Groups should not have naming conflicts with Args
assert!(
!app.args.args.iter().any(|x| x.id == group.id),
!app.args.args().any(|x| x.id == group.id),
"Argument group name '{}' must not conflict with argument name",
group.name,
);

for arg in &group.args {
// Args listed inside groups should exist
assert!(
app.args.args.iter().any(|x| x.id == *arg),
app.args.args().any(|x| x.id == *arg),
"Argument group '{}' contains non-existent argument '{:?}'",
group.name,
arg
Expand All @@ -232,8 +232,7 @@ pub(crate) fn assert_app(app: &App) {
if group.required {
assert!(
app.args
.args
.iter()
.args ()
.any(|x| x.id == *arg && x.default_vals.is_empty()),
"Argument group '{}' is required but contains argument '{:?}' which has a default value.",
group.name,
Expand Down
45 changes: 17 additions & 28 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'help> App<'help> {
/// Iterate through the set of arguments.
#[inline]
pub fn get_arguments(&self) -> impl Iterator<Item = &Arg<'help>> {
self.args.args.iter()
self.args.args()
}

/// Get the list of *positional* arguments.
Expand Down Expand Up @@ -242,12 +242,7 @@ impl<'help> App<'help> {
fn get_subcommands_containing(&self, arg: &Arg) -> Vec<&App<'help>> {
let mut vec = std::vec::Vec::new();
for idx in 0..self.subcommands.len() {
if self.subcommands[idx]
.args
.args
.iter()
.any(|ar| ar.id == arg.id)
{
if self.subcommands[idx].args.args().any(|ar| ar.id == arg.id) {
vec.push(&self.subcommands[idx]);
vec.append(&mut self.subcommands[idx].get_subcommands_containing(arg));
}
Expand All @@ -270,12 +265,11 @@ impl<'help> App<'help> {
.iter()
.map(|id| {
self.args
.args
.iter()
.args()
.chain(
self.get_subcommands_containing(arg)
.iter()
.flat_map(|x| x.args.args.iter()),
.flat_map(|x| x.args.args()),
)
.find(|arg| arg.id == *id)
.expect(
Expand Down Expand Up @@ -303,7 +297,7 @@ impl<'help> App<'help> {
arg.blacklist
.iter()
.map(|id| {
self.args.args.iter().find(|arg| arg.id == *id).expect(
self.args.args().find(|arg| arg.id == *id).expect(
"App::get_arg_conflicts_with: \
The passed arg conflicts with an arg unknown to the app",
)
Expand Down Expand Up @@ -2251,8 +2245,7 @@ impl<'help> App<'help> {
fn get_used_global_args(&self, matcher: &ArgMatcher) -> Vec<Id> {
let global_args: Vec<_> = self
.args
.args
.iter()
.args()
.filter(|a| a.global)
.map(|ga| ga.id.clone())
.collect();
Expand Down Expand Up @@ -2301,7 +2294,7 @@ impl<'help> App<'help> {
self._create_help_and_version();

let mut pos_counter = 1;
for a in self.args.args.iter_mut() {
for a in self.args.args_mut() {
// Fill in the groups
for g in &a.groups {
if let Some(ag) = self.groups.iter_mut().find(|grp| grp.id == *g) {
Expand Down Expand Up @@ -2341,8 +2334,7 @@ impl<'help> App<'help> {
if self.is_set(AppSettings::HelpRequired) || help_required_globally {
let args_missing_help: Vec<String> = self
.args
.args
.iter()
.args()
.filter(|arg| arg.about.is_none() && arg.long_about.is_none())
.map(|arg| String::from(arg.name))
.collect();
Expand All @@ -2366,7 +2358,7 @@ impl<'help> App<'help> {
where
F: Fn(&Arg) -> bool,
{
two_elements_of(self.args.args.iter().filter(|a: &&Arg| condition(a)))
two_elements_of(self.args.args().filter(|a: &&Arg| condition(a)))
}

// just in case
Expand Down Expand Up @@ -2410,7 +2402,7 @@ impl<'help> App<'help> {
}
{
// FIXME: This doesn't belong here at all.
for a in $_self.args.args.iter().filter(|a| a.global) {
for a in $_self.args.args().filter(|a| a.global) {
if $sc.find(&a.id).is_none() {
$sc.args.push(a.clone());
}
Expand All @@ -2432,8 +2424,7 @@ impl<'help> App<'help> {
if !(self.is_set(AppSettings::DisableHelpFlag)
|| self
.args
.args
.iter()
.args()
.any(|x| x.long == Some("help") || x.id == Id::help_hash())
|| self
.subcommands
Expand All @@ -2445,7 +2436,7 @@ impl<'help> App<'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().any(|x| x.short == Some('h'))
|| self.subcommands.iter().any(|sc| sc.short_flag == Some('h')))
{
help = help.short('h');
Expand All @@ -2457,8 +2448,7 @@ impl<'help> App<'help> {
if !(self.is_set(AppSettings::DisableVersionFlag)
|| self
.args
.args
.iter()
.args()
.any(|x| x.long == Some("version") || x.id == Id::version_hash())
|| self
.subcommands
Expand All @@ -2470,7 +2460,7 @@ impl<'help> App<'help> {
.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().any(|x| x.short == Some('V'))
|| self.subcommands.iter().any(|sc| sc.short_flag == Some('V')))
{
version = version.short('V');
Expand Down Expand Up @@ -2499,8 +2489,7 @@ impl<'help> App<'help> {
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
for (i, a) in self
.args
.args
.iter_mut()
.args_mut()
.filter(|a| a.has_switch())
.filter(|a| a.disp_ord == 999)
.enumerate()
Expand Down Expand Up @@ -2601,7 +2590,7 @@ impl<'help> App<'help> {
// Internal Query Methods
impl<'help> App<'help> {
pub(crate) fn find(&self, arg_id: &Id) -> Option<&Arg<'help>> {
self.args.args.iter().find(|a| a.id == *arg_id)
self.args.args().find(|a| a.id == *arg_id)
}

#[inline]
Expand Down Expand Up @@ -2691,7 +2680,7 @@ impl<'help> App<'help> {

#[cfg(debug_assertions)]
pub(crate) fn id_exists(&self, id: &Id) -> bool {
self.args.args.iter().any(|x| x.id == *id) || self.groups.iter().any(|x| x.id == *id)
self.args.args().any(|x| x.id == *id) || self.groups.iter().any(|x| x.id == *id)
}

/// Iterate through the groups this arg is member of.
Expand Down
Loading

0 comments on commit a0269a4

Please sign in to comment.