mod-cli 0.6.4

A fully customizable, feature-rich CLI framework for Rust. Define commands, prefixes, styled output, and more—built for flexibility and speed.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// pub mod custom; // feature = "custom-commands"
// use crate::custom::CustomCommand; // feature = "custom-commands"

#[cfg(feature = "internal-commands")]
use crate::commands::{FrameworkCommand, HelloCommand, HelpCommand, PingCommand};
use crate::output::hook;

#[cfg(feature = "async")]
use crate::command::AsyncCommand;
use crate::command::Command;
#[allow(unused_imports)]
use crate::error::ModCliError;
use std::collections::{HashMap, HashSet};

// Reduce type complexity for registry hooks and error formatter
type PreHookFn = dyn Fn(&str, &[String]) + Send + Sync;
type PostHookFn = dyn Fn(&str, &[String], Result<(), &str>) + Send + Sync;
type ErrorFmtFn = dyn Fn(&crate::error::ModCliError) -> String + Send + Sync;
type VisibilityPolicyFn = dyn Fn(&dyn Command, &HashSet<String>) -> bool + Send + Sync;
type AuthorizePolicyFn =
    dyn Fn(&dyn Command, &HashSet<String>, &[String]) -> Result<(), String> + Send + Sync;

/// Registry for commands and optional alias/prefix routing.
///
/// # Example
/// ```no_run
/// 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()]);
/// ```
pub struct CommandRegistry {
    prefix: String,
    commands: HashMap<String, Box<dyn Command>>,
    aliases: HashMap<String, String>,
    #[cfg(feature = "async")]
    async_commands: HashMap<String, Box<dyn AsyncCommand>>, // separate store for async commands
    #[cfg(feature = "async")]
    async_aliases: HashMap<String, String>,
    caps: HashSet<String>,
    visibility_policy: Option<Box<VisibilityPolicyFn>>,
    authorize_policy: Option<Box<AuthorizePolicyFn>>,
    pre_hook: Option<Box<PreHookFn>>,   // before dispatch
    post_hook: Option<Box<PostHookFn>>, // after dispatch
    error_formatter: Option<Box<ErrorFmtFn>>,
    #[cfg(feature = "dispatch-cache")]
    cache: std::sync::Mutex<Option<(String, String)>>,
}

impl Default for CommandRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl CommandRegistry {
    /// Creates a new command registry
    pub fn new() -> Self {
        let mut reg = Self {
            prefix: String::new(),
            commands: HashMap::new(),
            aliases: HashMap::new(),
            #[cfg(feature = "async")]
            async_commands: HashMap::new(),
            #[cfg(feature = "async")]
            async_aliases: HashMap::new(),
            caps: HashSet::new(),
            visibility_policy: None,
            authorize_policy: None,
            pre_hook: None,
            post_hook: None,
            error_formatter: None,
            #[cfg(feature = "dispatch-cache")]
            cache: std::sync::Mutex::new(None),
        };

        #[cfg(feature = "custom-commands")]
        reg.load_custom_commands();

        #[cfg(feature = "internal-commands")]
        reg.load_internal_commands();

        reg
    }

    /// Register an async command (feature: "async")
    #[cfg(feature = "async")]
    pub fn register_async(&mut self, cmd: Box<dyn AsyncCommand>) {
        let name = cmd.name().to_string();
        self.async_commands.insert(name.clone(), cmd);
        for &alias in self.async_commands[&name].aliases() {
            if !self.async_commands.contains_key(alias) {
                self.async_aliases.insert(alias.to_string(), name.clone());
            }
        }
    }

    /// Sets the command prefix
    /// Sets an optional prefix used for routing commands of the form `prefix:cmd`.
    pub fn set_prefix(&mut self, prefix: &str) {
        self.prefix = prefix.to_string();
    }

    /// Gets the command prefix
    /// Returns the configured prefix (empty string if not set).
    pub fn get_prefix(&self) -> &str {
        &self.prefix
    }

    /// Gets a command by name
    /// Gets a command by its primary name.
    #[inline(always)]
    pub fn get(&self, name: &str) -> Option<&dyn Command> {
        self.commands.get(name).map(|b| b.as_ref())
    }

    /// Gets a command by name with prefix
    /// Registers a command and records its aliases for reverse lookup.
    #[inline(always)]
    pub fn register(&mut self, cmd: Box<dyn Command>) {
        // capture name before moving the command
        let name = cmd.name().to_string();
        self.commands.insert(name.clone(), cmd);

        // map each alias -> primary name without intermediate Vec allocations
        for &alias in self.commands[&name].aliases() {
            // avoid alias clobbering existing command names
            if !self.commands.contains_key(alias) {
                // store alias as owned String
                self.aliases.insert(alias.to_string(), name.clone());
            }
        }
    }

    /// Returns all registered commands (read-only)
    /// Returns an iterator over all registered commands.
    pub fn all(&self) -> impl Iterator<Item = &Box<dyn Command>> {
        self.commands.values()
    }

    /// Returns all registered async commands (read-only)
    #[cfg(feature = "async")]
    pub fn all_async(&self) -> impl Iterator<Item = &Box<dyn AsyncCommand>> {
        self.async_commands.values()
    }

    // --- ASYNC DISPATCH (feature: "async") ---------------------------------
    #[cfg(feature = "async")]
    #[inline(always)]
    pub async fn try_execute_async(&self, cmd: &str, args: &[String]) -> Result<(), ModCliError> {
        if let Some(ref pre) = self.pre_hook {
            pre(cmd, args);
        }

        // Strip optional prefix from the incoming token
        let token: &str = if !self.prefix.is_empty() && cmd.len() > self.prefix.len() + 1 {
            let (maybe_prefix, rest_with_colon) = cmd.split_at(self.prefix.len());
            if maybe_prefix == self.prefix && rest_with_colon.as_bytes().first() == Some(&b':') {
                &rest_with_colon[1..]
            } else {
                cmd
            }
        } else {
            cmd
        };

        // Direct name
        if let Some(command) = self.async_commands.get(token) {
            if let Err(e) = self.is_authorized_async(args) {
                return Err(ModCliError::InvalidUsage(e));
            }
            command.execute_async(args).await?;
            if let Some(ref post) = self.post_hook {
                post(cmd, args, Ok(()));
            }
            return Ok(());
        }

        // Alias
        if let Some(primary) = self.async_aliases.get(token) {
            if let Some(command) = self.async_commands.get(primary.as_str()) {
                if let Err(e) = self.is_authorized_async(args) {
                    return Err(ModCliError::InvalidUsage(e));
                }
                command.execute_async(args).await?;
                if let Some(ref post) = self.post_hook {
                    post(cmd, args, Ok(()));
                }
                return Ok(());
            }
        }

        // Two-token nested: parent child -> parent:child
        if !args.is_empty() {
            let combined = format!("{token}:{}", args[0]);
            if let Some(command) = self.async_commands.get(combined.as_str()) {
                let rest = &args[1..];
                if let Err(e) = self.is_authorized_async(rest) {
                    return Err(ModCliError::InvalidUsage(e));
                }
                command.execute_async(rest).await?;
                if let Some(ref post) = self.post_hook {
                    post(cmd, args, Ok(()));
                }
                return Ok(());
            }
        }

        if let Some(ref post) = self.post_hook {
            post(cmd, args, Err("unknown"));
        }
        Err(ModCliError::UnknownCommand(cmd.to_string()))
    }

    /// Execute async and print user-friendly messages
    #[cfg(feature = "async")]
    #[inline(always)]
    pub async fn execute_async(&self, cmd: &str, args: &[String]) {
        if let Err(err) = self.try_execute_async(cmd, args).await {
            if let Some(ref fmt) = self.error_formatter {
                hook::error(&fmt(&err));
            } else {
                match err {
                    ModCliError::InvalidUsage(msg) => hook::error(&format!("Invalid usage: {msg}")),
                    ModCliError::UnknownCommand(name) => hook::unknown(&format!(
                        "[{name}]. Type `help` or `--help` for a list of available commands."
                    )),
                    other => hook::error(&format!("{other}")),
                }
            }
        }
    }

    // Authorization shim to reuse existing policy contract for async commands
    #[cfg(feature = "async")]
    #[inline(always)]
    fn is_authorized_async(&self, args: &[String]) -> Result<(), String> {
        if let Some(ref pol) = self.authorize_policy {
            struct Dummy;
            impl Command for Dummy {
                fn name(&self) -> &str {
                    "__async_dummy__"
                }
                fn execute(&self, _args: &[String]) {}
            }
            return pol(&Dummy, &self.caps, args);
        }
        Ok(())
    }

    // --- Capabilities API -----------------------------------------------------
    pub fn grant_cap<S: Into<String>>(&mut self, cap: S) {
        self.caps.insert(cap.into());
    }
    pub fn revoke_cap(&mut self, cap: &str) {
        self.caps.remove(cap);
    }
    pub fn has_cap(&self, cap: &str) -> bool {
        self.caps.contains(cap)
    }
    pub fn set_caps<I, S>(&mut self, caps: I)
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.caps.clear();
        for c in caps {
            self.caps.insert(c.into());
        }
    }

    pub fn set_visibility_policy<F>(&mut self, f: F)
    where
        F: Fn(&dyn Command, &HashSet<String>) -> bool + Send + Sync + 'static,
    {
        self.visibility_policy = Some(Box::new(f));
    }

    pub fn set_authorize_policy<F>(&mut self, f: F)
    where
        F: Fn(&dyn Command, &HashSet<String>, &[String]) -> Result<(), String>
            + Send
            + Sync
            + 'static,
    {
        self.authorize_policy = Some(Box::new(f));
    }

    pub fn set_pre_hook<F>(&mut self, f: F)
    where
        F: Fn(&str, &[String]) + Send + Sync + 'static,
    {
        self.pre_hook = Some(Box::new(f));
    }

    pub fn set_post_hook<F>(&mut self, f: F)
    where
        F: Fn(&str, &[String], Result<(), &str>) + Send + Sync + 'static,
    {
        self.post_hook = Some(Box::new(f));
    }

    pub fn set_error_formatter<F>(&mut self, f: F)
    where
        F: Fn(&crate::error::ModCliError) -> String + Send + Sync + 'static,
    {
        self.error_formatter = Some(Box::new(f));
    }

    #[inline(always)]
    pub fn is_visible(&self, cmd: &dyn Command) -> bool {
        if let Some(ref pol) = self.visibility_policy {
            return pol(cmd, &self.caps);
        }
        if cmd.hidden() {
            return false;
        }
        cmd.required_caps().iter().all(|c| self.caps.contains(*c))
    }

    #[inline(always)]
    pub fn is_authorized(&self, cmd: &dyn Command, args: &[String]) -> Result<(), String> {
        if let Some(ref pol) = self.authorize_policy {
            return pol(cmd, &self.caps, args);
        }
        if cmd.required_caps().iter().all(|c| self.caps.contains(*c)) {
            Ok(())
        } else {
            Err("Not authorized".into())
        }
    }

    // Note: runtime plugin loading has been removed from core for security/perf.

    /// 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):
    /// ```ignore
    /// use modcli::loader::CommandRegistry;
    /// let reg = CommandRegistry::new();
    /// // Will log an unknown command message via output hooks
    /// reg.execute("does-not-exist", &vec![]);
    /// ```
    #[inline(always)]
    pub fn execute(&self, cmd: &str, args: &[String]) {
        if let Err(err) = self.try_execute(cmd, args) {
            if let Some(ref fmt) = self.error_formatter {
                hook::error(&fmt(&err));
            } else {
                match err {
                    ModCliError::InvalidUsage(msg) => hook::error(&format!("Invalid usage: {msg}")),
                    ModCliError::UnknownCommand(name) => hook::unknown(&format!(
                        "[{name}]. Type `help` or `--help` for a list of available commands."
                    )),
                    other => hook::error(&format!("{other}")),
                }
            }
        }
    }

    /// 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):
    ///
    /// ```ignore
    /// 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"),
    ///     _ => {}
    /// }
    /// ```
    #[inline(always)]
    pub fn try_execute(&self, cmd: &str, args: &[String]) -> Result<(), ModCliError> {
        if let Some(ref pre) = self.pre_hook {
            pre(cmd, args);
        }
        // Strip optional prefix `<prefix>:` without intermediate allocations
        let token: &str = if !self.prefix.is_empty() && cmd.len() > self.prefix.len() + 1 {
            let (maybe_prefix, rest_with_colon) = cmd.split_at(self.prefix.len());
            if maybe_prefix == self.prefix && rest_with_colon.as_bytes().first() == Some(&b':') {
                &rest_with_colon[1..]
            } else {
                cmd
            }
        } else {
            cmd
        };

        #[cfg(feature = "dispatch-cache")]
        if let Ok(guard) = self.cache.lock() {
            if let Some((ref t, ref p)) = *guard {
                if t == token {
                    if let Some(command) = self.commands.get(p.as_str()) {
                        command.validate(args)?;
                        command.execute_with(args, self);
                        return Ok(());
                    }
                }
            }
        }

        // Try direct name
        if let Some(command) = self.commands.get(token) {
            if let Err(err) = self.is_authorized(command.as_ref(), args) {
                return Err(ModCliError::InvalidUsage(err));
            }
            command.validate(args)?;
            command.execute_with(args, self);
            #[cfg(feature = "dispatch-cache")]
            if let Ok(mut guard) = self.cache.lock() {
                *guard = Some((token.to_string(), token.to_string()));
            }
            if let Some(ref post) = self.post_hook {
                post(cmd, args, Ok(()));
            }
            return Ok(());
        }

        // Try alias mapping
        if let Some(primary) = self.aliases.get(token) {
            if let Some(command) = self.commands.get(primary.as_str()) {
                if let Err(err) = self.is_authorized(command.as_ref(), args) {
                    return Err(ModCliError::InvalidUsage(err));
                }
                command.validate(args)?;
                command.execute_with(args, self);
                #[cfg(feature = "dispatch-cache")]
                if let Ok(mut guard) = self.cache.lock() {
                    *guard = Some((token.to_string(), primary.clone()));
                }
                if let Some(ref post) = self.post_hook {
                    post(cmd, args, Ok(()));
                }
                return Ok(());
            }
        }

        // Two-token nested dispatch: "parent child ..." -> "parent:child"
        if !args.is_empty() {
            let combined = format!("{token}:{}", args[0]);
            if let Some(command) = self.commands.get(combined.as_str()) {
                let rest = &args[1..];
                if let Err(err) = self.is_authorized(command.as_ref(), rest) {
                    return Err(ModCliError::InvalidUsage(err));
                }
                command.validate(rest)?;
                command.execute_with(rest, self);
                if let Some(ref post) = self.post_hook {
                    post(cmd, args, Ok(()));
                }
                return Ok(());
            }
        }
        let err = ModCliError::UnknownCommand(cmd.to_string());
        if let Some(ref post) = self.post_hook {
            post(cmd, args, Err("unknown"));
        }
        Err(err)
    }

    #[cfg(feature = "internal-commands")]
    pub fn load_internal_commands(&mut self) {
        self.register(Box::new(PingCommand));
        self.register(Box::new(HelloCommand));
        self.register(Box::new(FrameworkCommand));
        self.register(Box::new(HelpCommand::new()));
    }

    // Note: JSON loader has been removed from core. Use code registration.

    pub fn len(&self) -> usize {
        self.commands.len()
    }

    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }

    #[cfg(feature = "custom-commands")]
    pub fn load_custom_commands(&mut self) {
        //self.register(Box::new(CustomCommand));
    }
}