#[derive(Debug, Clone)]
pub struct Arg {
pub name: String,
pub help: String,
pub required: bool,
pub variadic: bool,
}
impl Arg {
pub fn new<S: Into<String>>(name: S) -> Self {
Arg {
name: name.into(),
help: String::new(),
required: true,
variadic: false,
}
}
pub fn help<S: Into<String>>(mut self, h: S) -> Self {
self.help = h.into();
self
}
pub fn optional(mut self) -> Self {
self.required = false;
self
}
pub fn variadic(mut self) -> Self {
self.variadic = true;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FlagKind {
Bool,
Value,
}
#[derive(Debug, Clone)]
pub struct Flag {
pub long: String,
pub short: Option<char>,
pub value_name: Option<String>,
pub help: String,
pub kind: FlagKind,
pub default: Option<String>,
}
impl Flag {
pub fn switch<S: Into<String>>(long: S) -> Self {
Flag {
long: long.into(),
short: None,
value_name: None,
help: String::new(),
kind: FlagKind::Bool,
default: None,
}
}
pub fn value<S: Into<String>>(long: S) -> Self {
let long = long.into();
let vn = long.to_uppercase();
Flag {
long,
short: None,
value_name: Some(vn),
help: String::new(),
kind: FlagKind::Value,
default: None,
}
}
pub fn short(mut self, c: char) -> Self {
self.short = Some(c);
self
}
pub fn help<S: Into<String>>(mut self, h: S) -> Self {
self.help = h.into();
self
}
pub fn value_name<S: Into<String>>(mut self, n: S) -> Self {
self.value_name = Some(n.into());
self
}
pub fn default<S: Into<String>>(mut self, d: S) -> Self {
self.default = Some(d.into());
self
}
pub(crate) fn invocation(&self) -> String {
let mut s = String::new();
if let Some(c) = self.short {
s.push('-');
s.push(c);
s.push_str(", ");
} else {
s.push_str(" ");
}
s.push_str("--");
s.push_str(&self.long);
if self.kind == FlagKind::Value {
let vn = self.value_name.as_deref().unwrap_or("VALUE");
s.push_str(&format!(" <{}>", vn));
}
s
}
}
#[derive(Clone)]
pub struct Command {
pub name: String,
pub about: String,
pub long_about: String,
pub args: Vec<Arg>,
pub flags: Vec<Flag>,
pub examples: Vec<String>,
pub destructive: bool,
pub subcommands: Vec<Command>,
}
impl Command {
pub fn new<S: Into<String>>(name: S) -> Self {
Command {
name: name.into(),
about: String::new(),
long_about: String::new(),
args: Vec::new(),
flags: Vec::new(),
examples: Vec::new(),
destructive: false,
subcommands: Vec::new(),
}
}
pub fn is_group(&self) -> bool {
!self.subcommands.is_empty()
}
}