use std::iter;
use crate::util::eq_ignore_case;
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ArgValue<'help> {
pub(crate) name: &'help str,
pub(crate) about: Option<&'help str>,
pub(crate) aliases: Vec<&'help str>, pub(crate) hidden: bool,
}
impl<'help> From<&'help str> for ArgValue<'help> {
fn from(s: &'help str) -> Self {
Self::new(s)
}
}
impl<'help> From<&'help &'help str> for ArgValue<'help> {
fn from(s: &'help &'help str) -> Self {
Self::new(s)
}
}
impl<'help> ArgValue<'help> {
#[inline]
pub fn get_name(&self) -> &str {
self.name
}
#[inline]
pub fn get_about(&self) -> Option<&str> {
self.about
}
#[inline]
pub fn is_hidden(&self) -> bool {
self.hidden
}
pub fn get_visible_name(&self) -> Option<&str> {
if self.hidden {
None
} else {
Some(self.name)
}
}
pub fn get_name_and_aliases(&self) -> impl Iterator<Item = &str> {
iter::once(&self.name).chain(&self.aliases).copied()
}
pub fn matches(&self, value: &str, ignore_case: bool) -> bool {
if ignore_case {
self.get_name_and_aliases()
.any(|name| eq_ignore_case(name, value))
} else {
self.get_name_and_aliases().any(|name| name == value)
}
}
}
impl<'help> ArgValue<'help> {
pub fn new(name: &'help str) -> Self {
ArgValue {
name,
..Default::default()
}
}
#[inline]
pub fn about(mut self, about: &'help str) -> Self {
self.about = Some(about);
self
}
#[inline]
pub fn hidden(mut self, yes: bool) -> Self {
self.hidden = yes;
self
}
pub fn alias(mut self, name: &'help str) -> Self {
self.aliases.push(name);
self
}
pub fn aliases<I>(mut self, names: I) -> Self
where
I: IntoIterator<Item = &'help str>,
{
self.aliases.extend(names.into_iter());
self
}
}