use crate::ui::formatter::FormatGenerator;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Argument {
pub name: String,
pub required: bool,
pub literal: String,
pub description: Option<String>,
pub variadic: bool,
}
impl Argument {
pub fn new(val: &str) -> Self {
Self {
name: val.into(),
required: false,
description: None,
literal: "".into(),
variadic: false,
}
}
pub fn generate(value: &str, description: Option<String>) -> Self {
let (name, required, variadic) = clean_arg(value.trim());
Self {
name,
required,
literal: value.to_string(),
description,
variadic,
}
}
pub fn help(mut self, val: &str) -> Self {
self.description = Some(val.into());
self
}
pub fn is_variadic(mut self, val: bool) -> Self {
self.variadic = val;
self
}
pub fn is_required(mut self, val: bool) -> Self {
self.required = val;
self
}
}
fn clean_arg(val: &str) -> (String, bool, bool) {
let delimiters;
let required = if val.starts_with('<') {
delimiters = vec!['<', '>'];
true
} else {
delimiters = vec!['[', ']'];
false
};
let mut name = val
.replace(delimiters[0], "")
.replace(delimiters[1], "")
.replace('-', "_");
let variadic = if name.ends_with("...") {
name = name.replace("...", "");
true
} else {
false
};
(name, required, variadic)
}
impl FormatGenerator for Argument {
fn generate(&self, ptrn: crate::ui::formatter::Pattern) -> (String, String) {
use crate::ui::formatter::Pattern;
match &ptrn {
Pattern::Custom(ptrn) => {
let base = &ptrn.args_fmter;
let mut floating = String::from("");
let mut leading = base
.replace("{{name}}", &self.name)
.replace("{{literal}}", &self.literal);
if base.contains("{{description}}") {
leading =
leading.replace("{{description}}", &self.description.clone().unwrap());
} else {
floating = self.description.clone().unwrap()
}
(leading, floating)
}
_ => (self.literal.clone(), self.description.clone().unwrap()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_arg_creation() {
let a = Argument::generate("<test-app>", Some("Dummy help str".into()));
assert!(a.required);
assert!(!a.variadic);
assert_eq!(a.name, "test_app");
assert_eq!(a.description, Some("Dummy help str".into()));
}
}