Remove _some_ of pubs

This commit is contained in:
CreepySkeleton 2020-03-19 10:17:52 +03:00
parent bc738e12c2
commit 4fc4a00b8f
15 changed files with 140 additions and 461 deletions

View file

@ -1,5 +1,6 @@
mod settings;
pub use self::settings::{AppFlags, AppSettings};
pub use self::settings::AppSettings;
// Std
use std::collections::HashMap;
@ -15,7 +16,7 @@ use std::process;
use yaml_rust::Yaml;
// Internal
use crate::build::{Arg, ArgGroup, ArgSettings};
use crate::build::{app::settings::AppFlags, Arg, ArgGroup, ArgSettings};
use crate::mkeymap::MKeyMap;
use crate::output::fmt::ColorWhen;
use crate::output::{Help, Usage};
@ -26,9 +27,10 @@ use crate::INTERNAL_ERROR_MSG;
type Id = u64;
#[doc(hidden)]
// FIXME (@CreepySkeleton): some of this variants are never constructed
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Propagation {
#[allow(unused)]
pub(crate) enum Propagation {
To(Id),
Full,
NextLevel,
@ -65,56 +67,37 @@ pub enum Propagation {
/// [`App::get_matches`]: ./struct.App.html#method.get_matches
#[derive(Default, Debug, Clone)]
pub struct App<'b> {
#[doc(hidden)]
pub id: Id,
pub(crate) id: Id,
#[doc(hidden)]
pub name: String,
#[doc(hidden)]
pub bin_name: Option<String>,
#[doc(hidden)]
pub author: Option<&'b str>,
#[doc(hidden)]
pub version: Option<&'b str>,
#[doc(hidden)]
pub long_version: Option<&'b str>,
pub(crate) author: Option<&'b str>,
pub(crate) version: Option<&'b str>,
pub(crate) long_version: Option<&'b str>,
#[doc(hidden)]
pub about: Option<&'b str>,
#[doc(hidden)]
pub long_about: Option<&'b str>,
#[doc(hidden)]
pub more_help: Option<&'b str>,
#[doc(hidden)]
pub pre_help: Option<&'b str>,
pub(crate) long_about: Option<&'b str>,
pub(crate) more_help: Option<&'b str>,
pub(crate) pre_help: Option<&'b str>,
#[doc(hidden)]
pub aliases: Option<Vec<(&'b str, bool)>>, // (name, visible)
#[doc(hidden)]
pub usage_str: Option<&'b str>,
#[doc(hidden)]
pub usage: Option<String>,
#[doc(hidden)]
pub help_str: Option<&'b str>,
#[doc(hidden)]
pub disp_ord: usize,
#[doc(hidden)]
pub term_w: Option<usize>,
#[doc(hidden)]
pub max_w: Option<usize>,
#[doc(hidden)]
pub template: Option<&'b str>,
#[doc(hidden)]
pub settings: AppFlags,
#[doc(hidden)]
pub g_settings: AppFlags,
pub(crate) usage_str: Option<&'b str>,
pub(crate) usage: Option<String>,
pub(crate) help_str: Option<&'b str>,
pub(crate) disp_ord: usize,
pub(crate) term_w: Option<usize>,
pub(crate) max_w: Option<usize>,
pub(crate) template: Option<&'b str>,
pub(crate) settings: AppFlags,
pub(crate) g_settings: AppFlags,
#[doc(hidden)]
pub args: MKeyMap<'b>,
#[doc(hidden)]
pub subcommands: Vec<App<'b>>,
#[doc(hidden)]
pub replacers: HashMap<&'b str, &'b [&'b str]>,
#[doc(hidden)]
pub groups: Vec<ArgGroup<'b>>,
#[doc(hidden)]
pub help_headings: Vec<Option<&'b str>>,
pub(crate) replacers: HashMap<&'b str, &'b [&'b str]>,
pub(crate) groups: Vec<ArgGroup<'b>>,
pub(crate) help_headings: Vec<Option<&'b str>>,
}
impl<'b> App<'b> {
@ -1532,7 +1515,7 @@ impl<'b> App<'b> {
true
}
pub fn _propagate(&mut self, prop: Propagation) {
pub(crate) fn _propagate(&mut self, prop: Propagation) {
macro_rules! propagate_subcmd {
($_self:expr, $sc:expr) => {{
// We have to create a new scope in order to tell rustc the borrow of `sc` is
@ -1794,7 +1777,7 @@ impl<'b> App<'b> {
// Should we color the output? None=determined by output location, true=yes, false=no
#[doc(hidden)]
pub fn color(&self) -> ColorWhen {
pub(crate) fn color(&self) -> ColorWhen {
debugln!("App::color;");
debug!("App::color: Color setting...");
if self.is_set(AppSettings::ColorNever) {
@ -1821,55 +1804,31 @@ impl<'b> App<'b> {
self.settings.is_set(s) || self.g_settings.is_set(s)
}
pub fn set(&mut self, s: AppSettings) {
self.settings.set(s)
}
pub fn set_global(&mut self, s: AppSettings) {
self.g_settings.set(s)
}
pub fn unset_global(&mut self, s: AppSettings) {
self.g_settings.unset(s)
}
pub fn unset(&mut self, s: AppSettings) {
self.settings.unset(s)
}
pub fn has_subcommands(&self) -> bool {
!self.subcommands.is_empty()
}
pub fn has_args(&self) -> bool {
pub(crate) fn set(&mut self, s: AppSettings) {
self.settings.set(s)
}
pub(crate) fn unset(&mut self, s: AppSettings) {
self.settings.unset(s)
}
pub(crate) fn has_args(&self) -> bool {
!self.args.is_empty()
}
pub fn has_opts(&self) -> bool {
pub(crate) fn has_opts(&self) -> bool {
opts!(self).count() > 0
}
pub fn has_flags(&self) -> bool {
pub(crate) fn has_flags(&self) -> bool {
flags!(self).count() > 0
}
pub fn has_positionals(&self) -> bool {
positionals!(self).count() > 0
}
pub fn has_visible_opts(&self) -> bool {
opts!(self).any(|o| !o.is_set(ArgSettings::Hidden))
}
pub fn has_visible_flags(&self) -> bool {
flags!(self).any(|o| !o.is_set(ArgSettings::Hidden))
}
pub fn has_visible_positionals(&self) -> bool {
positionals!(self).any(|o| !o.is_set(ArgSettings::Hidden))
}
pub fn has_visible_subcommands(&self) -> bool {
pub(crate) fn has_visible_subcommands(&self) -> bool {
subcommands!(self)
.filter(|sc| sc.name != "help")
.any(|sc| !sc.is_set(AppSettings::Hidden))

View file

@ -52,7 +52,7 @@ bitflags! {
#[doc(hidden)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct AppFlags(Flags);
pub(crate) struct AppFlags(Flags);
impl BitOr for AppFlags {
type Output = Self;
@ -67,15 +67,6 @@ impl Default for AppFlags {
}
}
impl AppFlags {
pub fn new() -> Self {
AppFlags::default()
}
pub fn zeroed() -> Self {
AppFlags(Flags::empty())
}
}
impl_settings! { AppSettings, AppFlags,
ArgRequiredElseHelp("argrequiredelsehelp")
=> Flags::A_REQUIRED_ELSE_HELP,
@ -659,7 +650,7 @@ pub enum AppSettings {
HidePossibleValuesInHelp,
/// Tells `clap` to panic if help strings are omitted
///
///
/// # Examples
///
/// ```rust

View file

@ -1,5 +1,6 @@
mod settings;
pub use self::settings::{ArgFlags, ArgSettings};
pub use self::settings::ArgSettings;
// Std
use std::borrow::Cow;
@ -18,7 +19,7 @@ use crate::util::VecMap;
use yaml_rust;
// Internal
use crate::build::UsageParser;
use crate::build::{arg::settings::ArgFlags, usage_parser::UsageParser};
use crate::util::Key;
#[cfg(any(target_os = "windows", target_arch = "wasm32"))]
use crate::util::OsStrExt3;
@ -54,70 +55,46 @@ type Id = u64;
#[allow(missing_debug_implementations)]
#[derive(Default, Clone)]
pub struct Arg<'help> {
#[doc(hidden)]
pub id: Id,
pub(crate) id: Id,
#[doc(hidden)]
pub name: &'help str,
#[doc(hidden)]
pub help: Option<&'help str>,
#[doc(hidden)]
pub long_help: Option<&'help str>,
pub(crate) long_help: Option<&'help str>,
#[doc(hidden)]
pub blacklist: Option<Vec<Id>>,
#[doc(hidden)]
pub settings: ArgFlags,
#[doc(hidden)]
pub r_unless: Option<Vec<Id>>,
#[doc(hidden)]
pub overrides: Option<Vec<Id>>,
#[doc(hidden)]
pub groups: Option<Vec<Id>>,
#[doc(hidden)]
pub requires: Option<Vec<(Option<&'help str>, Id)>>,
pub(crate) settings: ArgFlags,
pub(crate) r_unless: Option<Vec<Id>>,
pub(crate) overrides: Option<Vec<Id>>,
pub(crate) groups: Option<Vec<Id>>,
pub(crate) requires: Option<Vec<(Option<&'help str>, Id)>>,
#[doc(hidden)]
pub short: Option<char>,
#[doc(hidden)]
pub long: Option<&'help str>,
#[doc(hidden)]
pub aliases: Option<Vec<(&'help str, bool)>>, // (name, visible)
#[doc(hidden)]
pub disp_ord: usize,
#[doc(hidden)]
pub unified_ord: usize,
pub(crate) aliases: Option<Vec<(&'help str, bool)>>, // (name, visible)
pub(crate) disp_ord: usize,
pub(crate) unified_ord: usize,
#[doc(hidden)]
pub possible_vals: Option<Vec<&'help str>>,
#[doc(hidden)]
pub val_names: Option<VecMap<&'help str>>,
#[doc(hidden)]
pub num_vals: Option<u64>,
#[doc(hidden)]
pub max_vals: Option<u64>,
#[doc(hidden)]
pub min_vals: Option<u64>,
#[doc(hidden)]
pub validator: Option<Validator>,
#[doc(hidden)]
pub validator_os: Option<ValidatorOs>,
#[doc(hidden)]
pub val_delim: Option<char>,
#[doc(hidden)]
pub default_vals: Option<Vec<&'help OsStr>>,
#[doc(hidden)]
pub default_vals_ifs: Option<VecMap<(Id, Option<&'help OsStr>, &'help OsStr)>>,
#[doc(hidden)]
pub env: Option<(&'help OsStr, Option<OsString>)>,
#[doc(hidden)]
pub terminator: Option<&'help str>,
pub(crate) val_names: Option<VecMap<&'help str>>,
pub(crate) num_vals: Option<u64>,
pub(crate) max_vals: Option<u64>,
pub(crate) min_vals: Option<u64>,
pub(crate) validator: Option<Validator>,
pub(crate) validator_os: Option<ValidatorOs>,
pub(crate) val_delim: Option<char>,
pub(crate) default_vals: Option<Vec<&'help OsStr>>,
pub(crate) default_vals_ifs: Option<VecMap<(Id, Option<&'help OsStr>, &'help OsStr)>>,
pub(crate) env: Option<(&'help OsStr, Option<OsString>)>,
pub(crate) terminator: Option<&'help str>,
#[doc(hidden)]
pub index: Option<u64>,
#[doc(hidden)]
pub r_ifs: Option<Vec<(Id, &'help str)>>,
pub(crate) r_ifs: Option<Vec<(Id, &'help str)>>,
#[doc(hidden)]
pub help_heading: Option<&'help str>,
#[doc(hidden)]
pub global: bool,
#[doc(hidden)]
pub exclusive: bool,
pub(crate) global: bool,
pub(crate) exclusive: bool,
}
impl<'help> Arg<'help> {
@ -817,7 +794,7 @@ impl<'help> Arg<'help> {
/// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind, ErrorKind::ArgumentConflict);
/// ```
///
///
/// [`Arg::conflicts_with_all(names)`]: ./struct.Arg.html#method.conflicts_with_all
/// [`Arg::exclusive(true)`]: ./struct.Arg.html#method.exclusive
@ -4066,6 +4043,7 @@ impl<'help> Arg<'help> {
self
}
// FIXME: (@CreepySkeleton)
#[doc(hidden)]
pub fn _build(&mut self) {
if (self.is_set(ArgSettings::UseValueDelimiter)
@ -4092,30 +4070,25 @@ impl<'help> Arg<'help> {
}
// @TODO @p6 @naming @internal: rename to set_mut
#[doc(hidden)]
pub fn setb(&mut self, s: ArgSettings) {
pub(crate) fn setb(&mut self, s: ArgSettings) {
self.settings.set(s);
}
// @TODO @p6 @naming @internal: rename to unset_mut
#[doc(hidden)]
pub fn unsetb(&mut self, s: ArgSettings) {
pub(crate) fn unsetb(&mut self, s: ArgSettings) {
self.settings.unset(s);
}
#[doc(hidden)]
pub fn has_switch(&self) -> bool {
pub(crate) fn has_switch(&self) -> bool {
self.short.is_some() || self.long.is_some()
}
#[doc(hidden)]
pub fn longest_filter(&self) -> bool {
pub(crate) fn longest_filter(&self) -> bool {
self.is_set(ArgSettings::TakesValue) || self.long.is_some() || self.short.is_none()
}
// Used for positionals when printing
#[doc(hidden)]
pub fn multiple_str(&self) -> &str {
pub(crate) fn multiple_str(&self) -> &str {
let mult_vals = self
.val_names
.as_ref()
@ -4131,8 +4104,7 @@ impl<'help> Arg<'help> {
}
// Used for positionals when printing
#[doc(hidden)]
pub fn name_no_brackets(&self) -> Cow<str> {
pub(crate) fn name_no_brackets(&self) -> Cow<str> {
debugln!("PosBuilder::name_no_brackets:{}", self.name);
let mut delim = String::new();
delim.push(if self.is_set(ArgSettings::RequireDelimiter) {

View file

@ -31,13 +31,7 @@ bitflags! {
#[doc(hidden)]
#[derive(Debug, Clone, Copy)]
pub struct ArgFlags(Flags);
impl ArgFlags {
pub fn new() -> Self {
ArgFlags::default()
}
}
pub(crate) struct ArgFlags(Flags);
// @TODO @p6 @internal: Reorder alphabetically
impl_settings! { ArgSettings, ArgFlags,

View file

@ -82,25 +82,17 @@ type Id = u64;
/// [requirement]: ./struct.Arg.html#method.requires
#[derive(Default)]
pub struct ArgGroup<'a> {
#[doc(hidden)]
pub id: Id,
#[doc(hidden)]
pub name: &'a str,
#[doc(hidden)]
pub args: Vec<Id>,
#[doc(hidden)]
pub required: bool,
#[doc(hidden)]
pub requires: Option<Vec<Id>>,
#[doc(hidden)]
pub conflicts: Option<Vec<Id>>,
#[doc(hidden)]
pub multiple: bool,
pub(crate) id: Id,
pub(crate) name: &'a str,
pub(crate) args: Vec<Id>,
pub(crate) required: bool,
pub(crate) requires: Option<Vec<Id>>,
pub(crate) conflicts: Option<Vec<Id>>,
pub(crate) multiple: bool,
}
impl<'a> ArgGroup<'a> {
#[doc(hidden)]
pub fn _with_id(id: Id) -> Self {
pub(crate) fn _with_id(id: Id) -> Self {
ArgGroup {
id,
..ArgGroup::default()

View file

@ -7,7 +7,6 @@ pub mod arg;
mod arg_group;
mod usage_parser;
pub use self::app::{App, AppFlags, AppSettings, Propagation};
pub use self::arg::{Arg, ArgFlags, ArgSettings};
pub use self::app::{App, AppSettings};
pub use self::arg::{Arg, ArgSettings};
pub use self::arg_group::ArgGroup;
pub use self::usage_parser::UsageParser;

View file

@ -15,9 +15,8 @@ enum UsageToken {
Default,
}
#[doc(hidden)]
#[derive(Debug)]
pub struct UsageParser<'a> {
pub(crate) struct UsageParser<'a> {
usage: &'a str,
pos: usize,
start: usize,
@ -37,12 +36,12 @@ impl<'a> UsageParser<'a> {
}
}
pub fn from_usage(usage: &'a str) -> Self {
pub(crate) fn from_usage(usage: &'a str) -> Self {
debugln!("UsageParser::from_usage;");
UsageParser::new(usage)
}
pub fn parse(mut self) -> Arg<'a> {
pub(crate) fn parse(mut self) -> Arg<'a> {
debugln!("UsageParser::parse;");
let mut arg = Arg::default();
arg.disp_ord = 999;

View file

@ -445,11 +445,10 @@
)]
#[cfg(not(feature = "std"))]
compile_error!("`std` feature is currently required to build this crate");
compile_error!("`std` feature is currently required to build `clap`");
pub use crate::build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation};
pub use crate::build::{App, AppSettings, Arg, ArgGroup, ArgSettings};
pub use crate::derive::{Clap, FromArgMatches, IntoApp, Subcommand};
pub use crate::output::fmt::Format;
pub use crate::parse::errors::{Error, ErrorKind, Result};
pub use crate::parse::{ArgMatches, OsValues, SubCommand, Values};
@ -464,9 +463,6 @@ pub use clap_derive::{self, *};
#[cfg_attr(feature = "derive", doc(hidden))]
pub use lazy_static;
#[doc(hidden)]
pub use mkeymap::KeyType;
#[macro_use]
#[allow(missing_docs)]
pub mod macros;

View file

@ -829,19 +829,19 @@ macro_rules! impl_settings {
$( $setting:ident($str:expr) => $flag:path ),+
) => {
impl $flags {
pub fn set(&mut self, s: $settings) {
pub(crate) fn set(&mut self, s: $settings) {
match s {
$($settings::$setting => self.0.insert($flag)),*
}
}
pub fn unset(&mut self, s: $settings) {
pub(crate) fn unset(&mut self, s: $settings) {
match s {
$($settings::$setting => self.0.remove($flag)),*
}
}
pub fn is_set(&self, s: $settings) -> bool {
pub(crate) fn is_set(&self, s: $settings) -> bool {
match s {
$($settings::$setting => self.0.contains($flag)),*
}
@ -927,7 +927,7 @@ macro_rules! flags {
$app.args
.args
.$how()
.filter(|a| !a.settings.is_set($crate::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| !a.is_set($crate::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| !a.help_heading.is_some())
}};
($app:expr) => {
@ -942,7 +942,7 @@ macro_rules! opts {
$app.args
.args
.$how()
.filter(|a| a.settings.is_set($crate::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| a.is_set($crate::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| !a.help_heading.is_some())
}};
($app:expr) => {

View file

@ -4,21 +4,22 @@ use std::ffi::{OsStr, OsString};
type Id = u64;
#[derive(PartialEq, Debug, Clone)]
pub struct Key {
pub key: KeyType,
pub index: usize,
pub(crate) struct Key {
pub(crate) key: KeyType,
pub(crate) index: usize,
}
#[derive(Default, PartialEq, Debug, Clone)]
pub struct MKeyMap<'b> {
pub keys: Vec<Key>,
pub(crate) keys: Vec<Key>,
pub args: Vec<Arg<'b>>,
// FIXME (@CreepySkeleton): this seems useless
built: bool, // mutation isn't possible after being built
}
#[doc(hidden)]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum KeyType {
pub(crate) enum KeyType {
Short(char),
Long(OsString),
Position(u64),
@ -52,36 +53,17 @@ impl PartialEq<char> for KeyType {
}
impl<'b> MKeyMap<'b> {
pub fn new() -> Self {
MKeyMap::default()
}
//TODO ::from(x), ::with_capacity(n) etc
//? set theory ops?
#[deprecated(since = "3.0.0", note = "Use `contains` instead")]
pub fn contains_long(&self, l: &str) -> bool {
self.contains(l)
}
#[deprecated(since = "3.0.0", note = "Use `contains` instead")]
pub fn contains_short(&self, c: char) -> bool {
self.contains(c)
}
pub fn contains<K>(&self, key: K) -> bool
pub(crate) fn contains<K>(&self, key: K) -> bool
where
KeyType: PartialEq<K>,
{
self.keys.iter().any(|x| x.key == key)
}
pub fn insert(&mut self, key: KeyType, value: Arg<'b>) -> usize {
let index = self.push(value);
self.keys.push(Key { key, index });
index
}
pub fn push(&mut self, value: Arg<'b>) -> usize {
pub(crate) fn push(&mut self, value: Arg<'b>) -> usize {
if self.built {
panic!("Cannot add Args to the map after the map is built");
}
@ -93,7 +75,7 @@ impl<'b> MKeyMap<'b> {
}
//TODO ::push_many([x, y])
pub fn insert_key(&mut self, key: KeyType, index: usize) {
pub(crate) fn insert_key(&mut self, key: KeyType, index: usize) {
if index >= self.args.len() {
panic!("Index out of bounds");
}
@ -104,7 +86,7 @@ impl<'b> MKeyMap<'b> {
// ! Arg mutation functionality
pub fn get(&self, key: &KeyType) -> Option<&Arg<'b>> {
pub(crate) fn get(&self, key: &KeyType) -> Option<&Arg<'b>> {
self.keys
.iter()
.find(|k| k.key == *key)
@ -112,33 +94,11 @@ impl<'b> MKeyMap<'b> {
}
//TODO ::get_first([KeyA, KeyB])
pub fn get_mut(&mut self, key: &KeyType) -> Option<&mut Arg<'b>> {
let key = self.keys.iter().find(|k| k.key == *key);
match key {
Some(k) => self.args.get_mut(k.index),
None => None,
}
}
pub fn is_empty(&self) -> bool {
pub(crate) fn is_empty(&self) -> bool {
self.keys.is_empty() && self.args.is_empty()
}
pub fn remove_key(&mut self, key: &KeyType) {
self.keys
.iter()
.position(|k| k.key == *key)
.map(|i| self.keys.swap_remove(i));
}
pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) {
let index = self.find_by_name(name);
self.keys.push(Key { key, index });
}
pub fn _build(&mut self) {
pub(crate) fn _build(&mut self) {
self.built = true;
for (i, arg) in self.args.iter_mut().enumerate() {
@ -148,63 +108,11 @@ impl<'b> MKeyMap<'b> {
}
}
pub fn make_entries_by_index(&mut self, index: usize) {
let short;
let positional;
let mut longs: Vec<_>;
let arg = &self.args[index];
short = arg.short.map(KeyType::Short);
positional = arg.index.map(KeyType::Position);
longs = arg
.aliases
.clone()
.map(|v| {
v.iter()
.map(|(n, _)| KeyType::Long(OsString::from(n)))
.collect()
})
.unwrap_or_default();
longs.extend(arg.long.map(|l| KeyType::Long(OsString::from(l))));
if let Some(s) = short {
self.insert_key(s, index)
}
if let Some(p) = positional {
self.insert_key(p, index)
}
}
pub fn find_by_name(&mut self, name: &str) -> usize {
self.args
.iter()
.position(|x| x.name == name)
.expect("No such name found")
}
pub fn remove(&mut self, key: &KeyType) -> Option<Arg<'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
let idx = self.keys.iter().position(|k| k.key == *key);
if let Some(idx) = idx {
let arg = self.args.swap_remove(idx);
for key in _get_keys(&arg) {
self.remove_key(&key);
}
return Some(arg);
}
None
}
//TODO ::remove_many([KeyA, KeyB])
//? probably shouldn't add a possibility for removal?
//? or remove by replacement by some dummy object, so the order is preserved
pub fn remove_by_name(&mut self, _name: Id) -> Option<Arg<'b>> {
pub(crate) fn remove_by_name(&mut self, _name: Id) -> Option<Arg<'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
@ -239,130 +147,3 @@ fn _get_keys(arg: &Arg) -> Vec<KeyType> {
keys
}
#[cfg(test)]
mod tests {
use self::KeyType::*;
use super::*;
#[test]
fn get_some_value() {
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
assert_eq!(
map.get(&Long(OsString::from("One"))),
Some(&Arg::with_name("Value1"))
);
}
#[test]
fn get_none_value() {
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.get(&Long(OsString::from("Two")));
assert_eq!(map.get(&Long(OsString::from("Two"))), None);
}
// #[test]
// fn insert_delete_value() {
// let mut map = MKeyMap::new();
// map.insert("One", clap::Arg::with_name("Value1"));
// assert_eq!(map.remove("One"), Some(clap::Arg::with_name("Value1")));
// assert!(map.is_empty());
// }
#[test]
fn insert_duplicate_key() {
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
assert_eq!(
map.insert(Long(OsString::from("One")), Arg::with_name("Value2")),
1
);
}
#[test]
// #[should_panic(expected = "Len changed")]
fn insert_duplicate_value() {
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
let orig_len = map.args.len();
map.insert(Long(OsString::from("Two")), Arg::with_name("Value1"));
assert_eq!(map.args.len(), orig_len + 1 /* , "Len changed" */);
// assert_eq!(
// map.get(&Long(OsString::from("One"))),
// map.get(&Long(OsString::from("Two")))
// );
}
// #[test]
// fn insert_delete_none() {
// let mut map = MKeyMap::new();
// map.insert("One", clap::Arg::with_name("Value1"));
// assert_eq!(map.remove("Two"), None);
// assert!(!map.is_empty());
// assert_eq!(map.get("One"), Some(clap::Arg::with_name("Value1")));
// }
#[test]
fn insert_multiple_keys() {
let mut map: MKeyMap = MKeyMap::new();
let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.insert_key(Long(OsString::from("Two")), index);
assert_eq!(
map.get(&Long(OsString::from("One"))),
map.get(&Long(OsString::from("Two")))
);
assert_eq!(map.args.len(), 1);
}
// #[test]
// fn insert_by_name() {
// let mut map: MKeyMap<Arg> = MKeyMap::new();
// let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
// map.insert_key_by_name(Long(OsString::from("Two")), "Value1");
// assert_eq!(
// map.get(Long(OsString::from("One"))),
// map.get(Long(OsString::from("Two")))
// );
// assert_eq!(map.values.len(), 1);
// }
#[test]
fn get_mutable() {
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
assert_eq!(
map.get_mut(&Long(OsString::from("One"))),
Some(&mut Arg::with_name("Value1"))
);
}
#[test]
fn remove_key() {
let mut map: MKeyMap = MKeyMap::new();
let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.insert_key(Long(OsString::from("Two")), index);
map.remove_key(&Long(OsString::from("One")));
assert_eq!(map.keys.len(), 1);
assert_eq!(map.args.len(), 1);
}
}

View file

@ -9,8 +9,8 @@ use atty;
use std::env;
use std::fmt;
#[doc(hidden)]
#[derive(Debug, Copy, Clone, PartialEq)]
#[doc(hidden)]
pub enum ColorWhen {
Auto,
Always,
@ -18,7 +18,7 @@ pub enum ColorWhen {
}
#[cfg(feature = "color")]
pub fn is_a_tty(stderr: bool) -> bool {
pub(crate) fn is_a_tty(stderr: bool) -> bool {
debugln!("is_a_tty: stderr={:?}", stderr);
let stream = if stderr {
atty::Stream::Stderr
@ -29,23 +29,21 @@ pub fn is_a_tty(stderr: bool) -> bool {
}
#[cfg(not(feature = "color"))]
pub fn is_a_tty(_: bool) -> bool {
pub(crate) fn is_a_tty(_: bool) -> bool {
debugln!("is_a_tty;");
false
}
pub fn is_term_dumb() -> bool {
pub(crate) fn is_term_dumb() -> bool {
env::var("TERM").ok() == Some(String::from("dumb"))
}
#[doc(hidden)]
pub struct ColorizerOption {
pub use_stderr: bool,
pub when: ColorWhen,
pub(crate) struct ColorizerOption {
pub(crate) use_stderr: bool,
pub(crate) when: ColorWhen,
}
#[doc(hidden)]
pub struct Colorizer {
pub(crate) struct Colorizer {
when: ColorWhen,
}
@ -60,7 +58,7 @@ macro_rules! color {
}
impl Colorizer {
pub fn new(option: &ColorizerOption) -> Colorizer {
pub(crate) fn new(option: &ColorizerOption) -> Colorizer {
let is_a_tty = is_a_tty(option.use_stderr);
let is_term_dumb = is_term_dumb();
Colorizer {
@ -72,7 +70,7 @@ impl Colorizer {
}
}
pub fn good<T>(&self, msg: T) -> Format<T>
pub(crate) fn good<T>(&self, msg: T) -> Format<T>
where
T: fmt::Display + AsRef<str>,
{
@ -80,7 +78,7 @@ impl Colorizer {
color!(self, Good, msg)
}
pub fn warning<T>(&self, msg: T) -> Format<T>
pub(crate) fn warning<T>(&self, msg: T) -> Format<T>
where
T: fmt::Display + AsRef<str>,
{
@ -88,7 +86,7 @@ impl Colorizer {
color!(self, Warning, msg)
}
pub fn error<T>(&self, msg: T) -> Format<T>
pub(crate) fn error<T>(&self, msg: T) -> Format<T>
where
T: fmt::Display + AsRef<str>,
{
@ -96,7 +94,7 @@ impl Colorizer {
color!(self, Error, msg)
}
pub fn none<T>(&self, msg: T) -> Format<T>
pub(crate) fn none<T>(&self, msg: T) -> Format<T>
where
T: fmt::Display + AsRef<str>,
{
@ -117,8 +115,7 @@ impl Default for Colorizer {
/// Defines styles for different types of error messages. Defaults to Error=Red, Warning=Yellow,
/// and Good=Green
#[derive(Debug)]
#[doc(hidden)]
pub enum Format<T> {
pub(crate) enum Format<T> {
/// Defines the style used for errors, defaults to Red
Error(T),
/// Defines the style used for warnings, defaults to Yellow

View file

@ -22,7 +22,7 @@ use unicode_width::UnicodeWidthStr;
#[cfg(not(feature = "wrap_help"))]
mod term_size {
pub fn dimensions() -> Option<(usize, usize)> {
pub(crate) fn dimensions() -> Option<(usize, usize)> {
None
}
}
@ -36,7 +36,7 @@ const TAB: &str = " ";
/// `clap` Help Writer.
///
/// Wraps a writer stream providing different methods to generate help for `clap` objects.
pub struct Help<'b, 'c, 'd, 'w> {
pub(crate) struct Help<'b, 'c, 'd, 'w> {
writer: &'w mut dyn Write,
parser: &'d Parser<'b, 'c>,
next_line_help: bool,
@ -52,7 +52,7 @@ pub struct Help<'b, 'c, 'd, 'w> {
// Public Functions
impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
/// Create a new `Help` instance.
pub fn new(
pub(crate) fn new(
w: &'w mut dyn Write,
parser: &'d Parser<'b, 'c>,
use_long: bool,
@ -92,7 +92,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
}
/// Writes the parser help to the wrapped stream.
pub fn write_help(&mut self) -> ClapResult<()> {
pub(crate) fn write_help(&mut self) -> ClapResult<()> {
debugln!("Help::write_help;");
if let Some(h) = self.parser.app.help_str {
write!(self.writer, "{}", h).map_err(Error::from)?;
@ -639,7 +639,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
/// Writes help for all arguments (options, flags, args, subcommands)
/// including titles of a Parser Object to the wrapped stream.
pub fn write_all_args(&mut self) -> ClapResult<()> {
pub(crate) fn write_all_args(&mut self) -> ClapResult<()> {
debugln!("Help::write_all_args;");
let flags = self.parser.has_flags();
// Strange filter/count vs fold... https://github.com/rust-lang/rust/issues/33038
@ -804,7 +804,7 @@ impl<'b, 'c, 'd, 'w> Help<'b, 'c, 'd, 'w> {
}
/// Writes default help for a Parser Object to the wrapped stream.
pub fn write_default_help(&mut self) -> ClapResult<()> {
pub(crate) fn write_default_help(&mut self) -> ClapResult<()> {
debugln!("Help::write_default_help;");
if let Some(h) = self.parser.app.pre_help {
self.write_before_after_help(h)?;

View file

@ -3,5 +3,5 @@ mod usage;
pub mod fmt;
pub use self::help::Help;
pub use self::usage::Usage;
pub(crate) use self::help::Help;
pub(crate) use self::usage::Usage;

View file

@ -9,7 +9,7 @@ use crate::INTERNAL_ERROR_MSG;
type Id = u64;
pub struct Usage<'b, 'c, 'z>
pub(crate) struct Usage<'b, 'c, 'z>
where
'b: 'c,
'c: 'z,
@ -18,13 +18,13 @@ where
}
impl<'b, 'c, 'z> Usage<'b, 'c, 'z> {
pub fn new(p: &'z Parser<'b, 'c>) -> Self {
pub(crate) fn new(p: &'z Parser<'b, 'c>) -> Self {
Usage { p }
}
// Creates a usage string for display. This happens just after all arguments were parsed, but before
// any subcommands have been parsed (so as to give subcommands their own usage recursively)
pub fn create_usage_with_title(&self, used: &[Id]) -> String {
pub(crate) fn create_usage_with_title(&self, used: &[Id]) -> String {
debugln!("usage::create_usage_with_title;");
let mut usage = String::with_capacity(75);
usage.push_str("USAGE:\n ");
@ -33,7 +33,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> {
}
// Creates a usage string (*without title*) if one was not provided by the user manually.
pub fn create_usage_no_title(&self, used: &[Id]) -> String {
pub(crate) fn create_usage_no_title(&self, used: &[Id]) -> String {
debugln!("usage::create_usage_no_title;");
if let Some(u) = self.p.app.usage_str {
String::from(&*u)
@ -45,7 +45,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> {
}
// Creates a usage string for display in help messages (i.e. not for errors)
pub fn create_help_usage(&self, incl_reqs: bool) -> String {
pub(crate) fn create_help_usage(&self, incl_reqs: bool) -> String {
debugln!("Usage::create_help_usage; incl_reqs={:?}", incl_reqs);
let mut usage = String::with_capacity(75);
let name = self
@ -310,7 +310,7 @@ impl<'b, 'c, 'z> Usage<'b, 'c, 'z> {
// `incl_last`: should we incldue args that are Arg::Last? (i.e. `prog [foo] -- [last]). We
// can't do that for required usages being built for subcommands because it would look like:
// `prog [foo] -- [last] <subcommand>` which is totally wrong.
pub fn get_required_usage_from(
pub(crate) fn get_required_usage_from(
&self,
incls: &[Id],
matcher: Option<&ArgMatcher>,

View file

@ -11,8 +11,7 @@ use crate::build::app::Propagation;
use crate::build::AppSettings as AS;
use crate::build::{App, Arg, ArgSettings};
use crate::mkeymap::KeyType;
use crate::output::Help;
use crate::output::Usage;
use crate::output::{Help, Usage};
use crate::parse::errors::Error as ClapError;
use crate::parse::errors::ErrorKind;
use crate::parse::errors::Result as ClapResult;