pub struct CommandRegistry { /* private fields */ }
Expand description
Registry for commands and optional alias/prefix routing.
§Example
use modcli::loader::CommandRegistry;
use modcli::command::Command;
struct Echo;
impl Command for Echo {
fn name(&self) -> &str { "echo" }
fn execute(&self, args: &[String]) { println!("{}", args.join(" ")) }
}
let mut reg = CommandRegistry::new();
reg.register(Box::new(Echo));
reg.execute("echo", &["hi".into()]);
Implementations§
Source§impl CommandRegistry
impl CommandRegistry
Sourcepub fn set_prefix(&mut self, prefix: &str)
pub fn set_prefix(&mut self, prefix: &str)
Sets the command prefix
Sets an optional prefix used for routing commands of the form prefix:cmd
.
Sourcepub fn get_prefix(&self) -> &str
pub fn get_prefix(&self) -> &str
Gets the command prefix Returns the configured prefix (empty string if not set).
Sourcepub fn get(&self, name: &str) -> Option<&dyn Command>
pub fn get(&self, name: &str) -> Option<&dyn Command>
Gets a command by name Gets a command by its primary name.
Sourcepub fn register(&mut self, cmd: Box<dyn Command>)
pub fn register(&mut self, cmd: Box<dyn Command>)
Gets a command by name with prefix Registers a command and records its aliases for reverse lookup.
Sourcepub fn all(&self) -> impl Iterator<Item = &Box<dyn Command>>
pub fn all(&self) -> impl Iterator<Item = &Box<dyn Command>>
Returns all registered commands (read-only) Returns an iterator over all registered commands.
pub fn grant_cap<S: Into<String>>(&mut self, cap: S)
pub fn revoke_cap(&mut self, cap: &str)
pub fn has_cap(&self, cap: &str) -> bool
pub fn set_caps<I, S>(&mut self, caps: I)
pub fn set_visibility_policy<F>(&mut self, f: F)
pub fn set_pre_hook<F>(&mut self, f: F)
pub fn set_post_hook<F>(&mut self, f: F)
pub fn set_error_formatter<F>(&mut self, f: F)
pub fn is_visible(&self, cmd: &dyn Command) -> bool
Sourcepub fn execute(&self, cmd: &str, args: &[String])
pub fn execute(&self, cmd: &str, args: &[String])
Resolves and executes a command by name or alias, with optional prefix routing.
Behavior:
- Applies optional prefix routing (e.g.,
tool:hello
). - Resolves aliases to primary command names.
- Validates args via
Command::validate()
and logs a themed error on failure. - Executes the command via
execute_with()
. - Prints user-facing messages via
output::hook
and does not return an error.
Example (illustrative):
use modcli::loader::CommandRegistry;
let reg = CommandRegistry::new();
// Will log an unknown command message via output hooks
reg.execute("does-not-exist", &vec![]);
Sourcepub fn try_execute(&self, cmd: &str, args: &[String]) -> Result<(), ModCliError>
pub fn try_execute(&self, cmd: &str, args: &[String]) -> Result<(), ModCliError>
Resolves and executes a command by name or alias, with optional prefix routing. Returns a structured error instead of printing/logging directly.
Error mapping:
InvalidUsage(String)
: whenvalidate()
returns an error string.UnknownCommand(String)
: command not found after alias/prefix resolution.
Examples (illustrative):
use modcli::loader::CommandRegistry;
// Assume `reg` has commands registered
let reg = CommandRegistry::new();
// Success
let _ = reg.try_execute("help", &vec![]);
// Error mapping (unknown)
match reg.try_execute("does-not-exist", &vec![]) {
Err(modcli::error::ModCliError::UnknownCommand(name)) => assert_eq!(name, "does-not-exist"),
_ => {}
}