Private keys

This commit is contained in:
Donough Liu 2020-12-27 01:18:43 +08:00
parent f54328ee4c
commit 2b7fd731ae
3 changed files with 21 additions and 24 deletions

View file

@ -1,11 +1,11 @@
use crate::{build::Arg, util::Id, INTERNAL_ERROR_MSG};
use std::{ffi::OsString, ops::Index};
use std::{ffi::OsString, ops::Index, iter::Iterator};
#[derive(PartialEq, Debug, Clone)]
pub(crate) struct Key {
pub(crate) key: KeyType,
pub(crate) index: usize,
key: KeyType,
index: usize,
}
#[derive(Default, PartialEq, Debug, Clone)]
@ -97,6 +97,11 @@ impl<'help> MKeyMap<'help> {
self.args.is_empty()
}
/// Return iterators of all keys.
pub(crate) fn keys<'key>(&'key self) -> impl Iterator<Item = &'key KeyType> {
self.keys.iter().map(|x| &x.key)
}
pub(crate) fn _build(&mut self) {
for (i, arg) in self.args.iter_mut().enumerate() {
for k in _get_keys(arg) {

View file

@ -52,7 +52,7 @@ where
.filter_map(|subcommand| {
subcommand._build();
let longs = subcommand.args.keys.iter().map(|x| &x.key).filter_map(|a| {
let longs = subcommand.args.keys().filter_map(|a| {
if let KeyType::Long(v) = a {
Some(v.to_string_lossy().into_owned())
} else {

View file

@ -122,11 +122,10 @@ impl<'help, 'app> Parser<'help, 'app> {
let highest_idx = self
.app
.args
.keys
.iter()
.keys()
.filter_map(|x| {
if let KeyType::Position(n) = x.key {
Some(n)
if let KeyType::Position(n) = x {
Some(*n)
} else {
None
}
@ -139,9 +138,7 @@ impl<'help, 'app> Parser<'help, 'app> {
let num_p = self
.app
.args
.keys
.iter()
.map(|x| &x.key)
.keys()
.filter(|x| x.is_position())
.count();
@ -334,9 +331,8 @@ impl<'help, 'app> Parser<'help, 'app> {
let positional_count = self
.app
.args
.keys
.iter()
.filter(|x| x.key.is_position())
.keys()
.filter(|x| x.is_position())
.count();
while let Some((arg_os, remaining_args)) = it.next() {
@ -542,9 +538,8 @@ impl<'help, 'app> Parser<'help, 'app> {
pos_counter = self
.app
.args
.keys
.iter()
.filter(|x| x.key.is_position())
.keys()
.filter(|x| x.is_position())
.count();
}
@ -570,9 +565,8 @@ impl<'help, 'app> Parser<'help, 'app> {
== self
.app
.args
.keys
.iter()
.filter(|x| x.key.is_position())
.keys()
.filter(|x| x.is_position())
.count())
{
self.app.settings.set(AS::TrailingValues);
@ -1621,9 +1615,7 @@ impl<'help, 'app> Parser<'help, 'app> {
let longs = self
.app
.args
.keys
.iter()
.map(|x| &x.key)
.keys()
.filter_map(|x| match x {
KeyType::Long(l) => Some(l.to_string_lossy().into_owned()),
_ => None,
@ -1726,7 +1718,7 @@ impl<'help, 'app> Parser<'help, 'app> {
}
pub(crate) fn has_positionals(&self) -> bool {
self.app.args.keys.iter().any(|x| x.key.is_position())
self.app.args.keys().any(|x| x.is_position())
}
pub(crate) fn has_subcommands(&self) -> bool {