#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum ArgKind {
Flag,
Count,
Option,
Positional,
}
#[derive(Clone, Debug)]
pub struct Arg {
pub(crate) name: String,
pub(crate) kind: ArgKind,
pub(crate) short: Option<char>,
pub(crate) long: Option<String>,
pub(crate) help: Option<String>,
pub(crate) required: bool,
pub(crate) multiple: bool,
pub(crate) default: Option<String>,
}
impl Arg {
fn new(name: impl Into<String>, kind: ArgKind) -> Arg {
let name = name.into();
let long = match kind {
ArgKind::Flag | ArgKind::Count | ArgKind::Option => Some(name.clone()),
ArgKind::Positional => None,
};
Arg {
name,
kind,
short: None,
long,
help: None,
required: false,
multiple: false,
default: None,
}
}
#[must_use]
pub fn flag(name: impl Into<String>) -> Arg {
Arg::new(name, ArgKind::Flag)
}
#[must_use]
pub fn count(name: impl Into<String>) -> Arg {
Arg::new(name, ArgKind::Count)
}
#[must_use]
pub fn option(name: impl Into<String>) -> Arg {
Arg::new(name, ArgKind::Option)
}
#[must_use]
pub fn positional(name: impl Into<String>) -> Arg {
Arg::new(name, ArgKind::Positional)
}
#[must_use]
pub fn short(mut self, short: char) -> Arg {
self.short = Some(short);
self
}
#[must_use]
pub fn long(mut self, long: impl Into<String>) -> Arg {
self.long = Some(long.into());
self
}
#[must_use]
pub fn help(mut self, help: impl Into<String>) -> Arg {
self.help = Some(help.into());
self
}
#[must_use]
pub fn required(mut self, required: bool) -> Arg {
self.required = required;
self
}
#[must_use]
pub fn multiple(mut self, multiple: bool) -> Arg {
self.multiple = multiple;
self
}
#[must_use]
pub fn default(mut self, value: impl Into<String>) -> Arg {
self.default = Some(value.into());
self
}
pub(crate) fn long_name(&self) -> Option<&str> {
self.long.as_deref()
}
}