CommandRegistry

Struct CommandRegistry 

Source
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

Source

pub fn new() -> Self

Creates a new command registry

Source

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.

Source

pub fn get_prefix(&self) -> &str

Gets the command prefix Returns the configured prefix (empty string if not set).

Source

pub fn get(&self, name: &str) -> Option<&dyn Command>

Gets a command by name Gets a command by its primary name.

Source

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.

Source

pub fn all(&self) -> impl Iterator<Item = &Box<dyn Command>>

Returns all registered commands (read-only) Returns an iterator over all registered commands.

Source

pub fn grant_cap<S: Into<String>>(&mut self, cap: S)

Source

pub fn revoke_cap(&mut self, cap: &str)

Source

pub fn has_cap(&self, cap: &str) -> bool

Source

pub fn set_caps<I, S>(&mut self, caps: I)
where I: IntoIterator<Item = S>, S: Into<String>,

Source

pub fn set_visibility_policy<F>(&mut self, f: F)
where F: Fn(&dyn Command, &HashSet<String>) -> bool + Send + Sync + 'static,

Source

pub fn set_authorize_policy<F>(&mut self, f: F)
where F: Fn(&dyn Command, &HashSet<String>, &[String]) -> Result<(), String> + Send + Sync + 'static,

Source

pub fn set_pre_hook<F>(&mut self, f: F)
where F: Fn(&str, &[String]) + Send + Sync + 'static,

Source

pub fn set_post_hook<F>(&mut self, f: F)
where F: Fn(&str, &[String], Result<(), &str>) + Send + Sync + 'static,

Source

pub fn set_error_formatter<F>(&mut self, f: F)
where F: Fn(&ModCliError) -> String + Send + Sync + 'static,

Source

pub fn is_visible(&self, cmd: &dyn Command) -> bool

Source

pub fn is_authorized( &self, cmd: &dyn Command, args: &[String], ) -> Result<(), String>

Source

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![]);
Source

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): when validate() 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"),
    _ => {}
}
Source

pub fn load_internal_commands(&mut self)

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn load_custom_commands(&mut self)

Trait Implementations§

Source§

impl Default for CommandRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.