pub trait Command {
const COMMAND: CommandInfo;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct CommandInfo {
pub name: &'static str,
pub about: &'static str,
pub long_about: &'static str,
pub version: Option<&'static str>,
pub args: &'static [ArgInfo],
pub commands: &'static [CommandInfo],
pub subcommand_required: bool,
pub visible_aliases: &'static [&'static str],
pub aliases: &'static [&'static str],
pub hidden: bool,
}
impl CommandInfo {
pub const fn new(name: &'static str) -> Self {
CommandInfo {
name,
about: "",
long_about: "",
version: None,
args: &[],
commands: &[],
subcommand_required: false,
visible_aliases: &[],
aliases: &[],
hidden: false,
}
}
pub const fn subcommand_required(mut self, required: bool) -> Self {
self.subcommand_required = required;
self
}
pub const fn visible_aliases(mut self, aliases: &'static [&'static str]) -> Self {
self.visible_aliases = aliases;
self
}
pub const fn aliases(mut self, aliases: &'static [&'static str]) -> Self {
self.aliases = aliases;
self
}
pub const fn hidden(mut self, hidden: bool) -> Self {
self.hidden = hidden;
self
}
pub const fn about(mut self, about: &'static str) -> Self {
self.about = about;
self
}
pub const fn long_about(mut self, long_about: &'static str) -> Self {
self.long_about = long_about;
self
}
pub const fn version(mut self, version: &'static str) -> Self {
self.version = Some(version);
self
}
pub const fn args(mut self, args: &'static [ArgInfo]) -> Self {
self.args = args;
self
}
pub const fn commands(mut self, commands: &'static [CommandInfo]) -> Self {
self.commands = commands;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ArgKind {
Positional,
Option,
Flag,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct ArgInfo {
pub id: &'static str,
pub kind: ArgKind,
pub short: Option<char>,
pub long: Option<&'static str>,
pub visible_short_aliases: &'static [char],
pub short_aliases: &'static [char],
pub visible_aliases: &'static [&'static str],
pub aliases: &'static [&'static str],
pub value_name: &'static str,
pub help: &'static str,
pub long_help: &'static str,
pub possible_values: &'static [&'static str],
pub env: Option<&'static str>,
pub hidden: bool,
pub required: bool,
pub multiple: bool,
pub last: bool,
pub global: bool,
}
impl ArgInfo {
pub const fn new(id: &'static str, kind: ArgKind) -> Self {
ArgInfo {
id,
kind,
short: None,
long: None,
visible_short_aliases: &[],
short_aliases: &[],
visible_aliases: &[],
aliases: &[],
value_name: "",
help: "",
long_help: "",
possible_values: &[],
env: None,
hidden: false,
required: false,
multiple: false,
last: false,
global: false,
}
}
pub const fn short(mut self, short: char) -> Self {
self.short = Some(short);
self
}
pub const fn long(mut self, long: &'static str) -> Self {
self.long = Some(long);
self
}
pub const fn visible_short_aliases(mut self, aliases: &'static [char]) -> Self {
self.visible_short_aliases = aliases;
self
}
pub const fn short_aliases(mut self, aliases: &'static [char]) -> Self {
self.short_aliases = aliases;
self
}
pub const fn visible_aliases(mut self, aliases: &'static [&'static str]) -> Self {
self.visible_aliases = aliases;
self
}
pub const fn aliases(mut self, aliases: &'static [&'static str]) -> Self {
self.aliases = aliases;
self
}
pub const fn value_name(mut self, value_name: &'static str) -> Self {
self.value_name = value_name;
self
}
pub const fn help(mut self, help: &'static str) -> Self {
self.help = help;
self
}
pub const fn long_help(mut self, long_help: &'static str) -> Self {
self.long_help = long_help;
self
}
pub const fn possible_values(mut self, values: &'static [&'static str]) -> Self {
self.possible_values = values;
self
}
pub const fn env(mut self, env: &'static str) -> Self {
self.env = Some(env);
self
}
pub const fn hidden(mut self, hidden: bool) -> Self {
self.hidden = hidden;
self
}
pub const fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
pub const fn multiple(mut self, multiple: bool) -> Self {
self.multiple = multiple;
self
}
pub const fn last(mut self, last: bool) -> Self {
self.last = last;
self
}
pub const fn global(mut self, global: bool) -> Self {
self.global = global;
self
}
pub const fn takes_value(&self) -> bool {
matches!(self.kind, ArgKind::Positional | ArgKind::Option)
}
pub const fn is_positional(&self) -> bool {
matches!(self.kind, ArgKind::Positional)
}
pub const fn long_or_short_help(&self) -> &'static str {
if self.long_help.is_empty() {
self.help
} else {
self.long_help
}
}
}