modcli/
lib.rs

1//! ModCLI — a lightweight, modular CLI framework for Rust.
2//!
3//! # Quick Start
4//! ```no_run
5//! use modcli::ModCli;
6//! let args: Vec<String> = std::env::args().skip(1).collect();
7//! let mut cli = ModCli::new();
8//! cli.run(args);
9//! ```
10//!
11//! # Features
12//! - Custom commands via the `Command` trait
13//! - Styled output, gradients, progress, tables
14//! - Optional internal helper commands
15//!
16//! Note: Runtime plugins and JSON/config loaders have been removed from core for
17//! security and performance. Configure your CLI directly in code.
18
19pub mod command;
20pub mod error;
21pub mod input;
22pub mod loader;
23pub mod output;
24pub mod parser;
25
26pub use crate::command::Command as CliCustom;
27use crate::loader::CommandRegistry;
28
29#[cfg(feature = "internal-commands")]
30pub mod commands;
31
32#[cfg(feature = "custom-commands")]
33pub mod custom;
34
35/// Represents a CLI application and provides command registration and dispatch.
36///
37/// Typical usage:
38/// ```no_run
39/// use modcli::ModCli;
40/// let args: Vec<String> = std::env::args().skip(1).collect();
41/// let mut cli = ModCli::new();
42/// cli.run(args);
43/// ```
44pub struct ModCli {
45    pub registry: CommandRegistry,
46}
47
48impl Default for ModCli {
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54impl ModCli {
55    /// Creates a new ModCli instance.
56    ///
57    /// # Example
58    /// ```
59    /// use modcli::ModCli;
60    /// let cli = ModCli::new();
61    /// ```
62    ///
63    /// # Arguments
64    /// * `args` - A vector of command-line arguments
65    ///
66    /// # Returns
67    /// A new instance of `ModCli`
68    pub fn new() -> Self {
69        Self {
70            registry: CommandRegistry::new(),
71        }
72    }
73
74    /// Sets the command prefix used for prefix routing (e.g., `tool:hello`).
75    pub fn set_prefix(&mut self, prefix: &str) {
76        self.registry.set_prefix(prefix);
77    }
78
79    /// Gets the current command prefix.
80    pub fn get_prefix(&self) -> &str {
81        self.registry.get_prefix()
82    }
83
84    /// Runs the CLI by dispatching the first arg as the command and the rest as arguments.
85    /// Prints an error if no command is provided.
86    pub fn run(&mut self, args: Vec<String>) {
87        if args.is_empty() {
88            crate::output::hook::status("No command provided. Try `help`.");
89            return;
90        }
91
92        let command = &args[0];
93        let rest = &args[1..];
94
95        self.registry.execute(command, rest);
96    }
97}
98
99/// Returns the version of the ModCLI framework (from `modcli/Cargo.toml`).
100///
101/// Useful for surfacing framework version from applications.
102pub fn modcli_version() -> &'static str {
103    env!("CARGO_PKG_VERSION")
104}