#[derive(Debug, Clone)]
pub struct CustomCommand {
pub name: String,
pub description: String,
pub usage: Option<String>,
pub source_plugin: Option<String>,
}
impl CustomCommand {
pub fn new(name: impl Into<String>) -> Self {
CustomCommand {
name: name.into(),
description: String::new(),
usage: None,
source_plugin: None,
}
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.description = desc.into();
self
}
pub fn with_usage(mut self, usage: impl Into<String>) -> Self {
self.usage = Some(usage.into());
self
}
pub fn with_source(mut self, source: impl Into<String>) -> Self {
self.source_plugin = Some(source.into());
self
}
pub fn help_text(&self) -> String {
let mut text = String::new();
if !self.description.is_empty() {
text.push_str(&self.description);
text.push('\n');
}
if let Some(ref usage) = self.usage {
text.push('\n');
text.push_str("Usage: ");
text.push_str(usage);
text.push('\n');
}
if let Some(ref source) = self.source_plugin {
text.push('\n');
text.push_str("(Defined by: ");
text.push_str(source);
text.push(')');
}
text
}
}
pub struct CommandBuilder {
command: CustomCommand,
}
impl CommandBuilder {
pub fn new(name: impl Into<String>) -> Self {
CommandBuilder {
command: CustomCommand::new(name),
}
}
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.command.description = desc.into();
self
}
pub fn usage(mut self, usage: impl Into<String>) -> Self {
self.command.usage = Some(usage.into());
self
}
pub fn source(mut self, source: impl Into<String>) -> Self {
self.command.source_plugin = Some(source.into());
self
}
pub fn build(self) -> CustomCommand {
self.command
}
}