dynamic_cli/interface/cli.rs
1//! CLI (Command-Line Interface) implementation
2//!
3//! This module provides a simple CLI interface that parses command-line
4//! arguments, executes the corresponding command, and exits.
5//!
6//! # Example
7//!
8//! ```no_run
9//! use dynamic_cli::interface::CliInterface;
10//! use dynamic_cli::prelude::*;
11//!
12//! # #[derive(Default)]
13//! # struct MyContext;
14//! # impl ExecutionContext for MyContext {
15//! # fn as_any(&self) -> &dyn std::any::Any { self }
16//! # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
17//! # }
18//! # fn main() -> dynamic_cli::Result<()> {
19//! let registry = CommandRegistry::new();
20//! let context = Box::new(MyContext::default());
21//!
22//! let cli = CliInterface::new(registry, context);
23//! cli.run(std::env::args().skip(1).collect())?;
24//! # Ok(())
25//! # }
26//! ```
27
28use crate::context::ExecutionContext;
29use crate::error::{display_error, DynamicCliError, Result};
30use crate::parser::CliParser;
31use crate::registry::CommandRegistry;
32use std::process;
33
34/// CLI (Command-Line Interface) handler
35///
36/// Provides a simple interface for executing commands from command-line arguments.
37/// The CLI parses arguments, executes the command, and exits.
38///
39/// # Architecture
40///
41/// ```text
42/// Command-line args → CliParser → CommandExecutor → Handler
43/// ↓
44/// ExecutionContext
45/// ```
46///
47/// # Error Handling
48///
49/// Errors are displayed to stderr with colored formatting (if enabled)
50/// and the process exits with appropriate exit codes:
51/// - `0`: Success
52/// - `1`: Execution error
53/// - `2`: Argument parsing error
54/// - `3`: Other errors
55pub struct CliInterface {
56 /// Command registry containing all available commands
57 registry: CommandRegistry,
58
59 /// Execution context (owned by the interface)
60 context: Box<dyn ExecutionContext>,
61}
62
63impl CliInterface {
64 /// Create a new CLI interface
65 ///
66 /// # Arguments
67 ///
68 /// * `registry` - Command registry with all registered commands
69 /// * `context` - Execution context (will be consumed by the interface)
70 ///
71 /// # Example
72 ///
73 /// ```no_run
74 /// use dynamic_cli::interface::CliInterface;
75 /// use dynamic_cli::prelude::*;
76 ///
77 /// # #[derive(Default)]
78 /// # struct MyContext;
79 /// # impl ExecutionContext for MyContext {
80 /// # fn as_any(&self) -> &dyn std::any::Any { self }
81 /// # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
82 /// # }
83 /// let registry = CommandRegistry::new();
84 /// let context = Box::new(MyContext::default());
85 ///
86 /// let cli = CliInterface::new(registry, context);
87 /// ```
88 pub fn new(registry: CommandRegistry, context: Box<dyn ExecutionContext>) -> Self {
89 Self { registry, context }
90 }
91
92 /// Run the CLI with provided arguments
93 ///
94 /// Parses the arguments, executes the corresponding command, and handles errors.
95 /// This method consumes `self` as the CLI typically runs once and exits.
96 ///
97 /// # Arguments
98 ///
99 /// * `args` - Command-line arguments (typically from `env::args().skip(1)`)
100 ///
101 /// # Returns
102 ///
103 /// - `Ok(())` on success
104 /// - `Err(DynamicCliError)` on any error (parsing, validation, execution)
105 ///
106 /// # Exit Codes
107 ///
108 /// The caller should handle errors and exit with appropriate codes:
109 /// - Parse errors → exit code 2
110 /// - Execution errors → exit code 1
111 /// - Other errors → exit code 3
112 ///
113 /// # Example
114 ///
115 /// ```no_run
116 /// use dynamic_cli::interface::CliInterface;
117 /// use dynamic_cli::prelude::*;
118 /// use std::process;
119 ///
120 /// # #[derive(Default)]
121 /// # struct MyContext;
122 /// # impl ExecutionContext for MyContext {
123 /// # fn as_any(&self) -> &dyn std::any::Any { self }
124 /// # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
125 /// # }
126 /// # fn main() {
127 /// let registry = CommandRegistry::new();
128 /// let context = Box::new(MyContext::default());
129 /// let cli = CliInterface::new(registry, context);
130 ///
131 /// if let Err(e) = cli.run(std::env::args().skip(1).collect()) {
132 /// eprintln!("Error: {}", e);
133 /// process::exit(1);
134 /// }
135 /// # }
136 /// ```
137 pub fn run(mut self, args: Vec<String>) -> Result<()> {
138 // Handle empty arguments (show help or error)
139 if args.is_empty() {
140 return Err(DynamicCliError::Parse(
141 crate::error::ParseError::InvalidSyntax {
142 details: "No command specified".to_string(),
143 hint: Some("Try 'help' to see available commands".to_string()),
144 },
145 ));
146 }
147
148 // First argument is the command name
149 let command_name = &args[0];
150
151 // Resolve command name (handles aliases)
152 let resolved_name = self.registry.resolve_name(command_name).ok_or_else(|| {
153 crate::error::ParseError::unknown_command_with_suggestions(
154 command_name,
155 &self
156 .registry
157 .list_commands()
158 .iter()
159 .map(|cmd| cmd.name.clone())
160 .collect::<Vec<_>>(),
161 )
162 })?;
163
164 // Get command definition
165 let definition = self.registry.get_definition(resolved_name).ok_or_else(|| {
166 DynamicCliError::Registry(crate::error::RegistryError::MissingHandler {
167 command: resolved_name.to_string(),
168 })
169 })?;
170
171 // Parse arguments using CLI parser
172 let parser = CliParser::new(definition);
173 let parsed_args = parser.parse(&args[1..])?;
174
175 // Get handler and execute command
176 let handler = self.registry.get_handler(resolved_name).ok_or_else(|| {
177 DynamicCliError::Execution(crate::error::ExecutionError::HandlerNotFound {
178 command: resolved_name.to_string(),
179 implementation: definition.implementation.clone(),
180 })
181 })?;
182
183 handler.execute(&mut *self.context, &parsed_args)?;
184
185 Ok(())
186 }
187
188 /// Run the CLI with automatic error handling and exit
189 ///
190 /// This is a convenience method that:
191 /// 1. Runs the CLI with provided arguments
192 /// 2. Handles errors by displaying them to stderr
193 /// 3. Exits the process with appropriate exit code
194 ///
195 /// This method never returns.
196 ///
197 /// # Arguments
198 ///
199 /// * `args` - Command-line arguments
200 ///
201 /// # Example
202 ///
203 /// ```no_run
204 /// use dynamic_cli::interface::CliInterface;
205 /// use dynamic_cli::prelude::*;
206 ///
207 /// # #[derive(Default)]
208 /// # struct MyContext;
209 /// # impl ExecutionContext for MyContext {
210 /// # fn as_any(&self) -> &dyn std::any::Any { self }
211 /// # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
212 /// # }
213 /// # fn main() {
214 /// let registry = CommandRegistry::new();
215 /// let context = Box::new(MyContext::default());
216 /// let cli = CliInterface::new(registry, context);
217 ///
218 /// // This will handle errors and exit automatically
219 /// cli.run_and_exit(std::env::args().skip(1).collect());
220 /// # }
221 /// ```
222 pub fn run_and_exit(self, args: Vec<String>) -> ! {
223 match self.run(args) {
224 Ok(()) => process::exit(0),
225 Err(e) => {
226 display_error(&e);
227
228 // Exit with appropriate code based on error type
229 let exit_code = match e {
230 DynamicCliError::Parse(_) => 2,
231 DynamicCliError::Validation(_) => 2,
232 DynamicCliError::Execution(_) => 1,
233 _ => 3,
234 };
235
236 process::exit(exit_code);
237 }
238 }
239 }
240}
241
242#[cfg(test)]
243mod tests {
244 use super::*;
245 use crate::config::schema::{ArgumentDefinition, ArgumentType, CommandDefinition};
246 use std::collections::HashMap;
247
248 // Test context
249 #[derive(Default)]
250 struct TestContext {
251 executed_command: Option<String>,
252 }
253
254 impl ExecutionContext for TestContext {
255 fn as_any(&self) -> &dyn std::any::Any {
256 self
257 }
258
259 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
260 self
261 }
262 }
263
264 // Test handler
265 struct TestHandler {
266 name: String,
267 }
268
269 impl crate::executor::CommandHandler for TestHandler {
270 fn execute(
271 &self,
272 context: &mut dyn ExecutionContext,
273 _args: &HashMap<String, String>,
274 ) -> Result<()> {
275 let ctx = crate::context::downcast_mut::<TestContext>(context)
276 .expect("Failed to downcast context");
277 ctx.executed_command = Some(self.name.clone());
278 Ok(())
279 }
280 }
281
282 fn create_test_registry() -> CommandRegistry {
283 let mut registry = CommandRegistry::new();
284
285 // Create a simple command definition
286 let cmd_def = CommandDefinition {
287 name: "test".to_string(),
288 aliases: vec!["t".to_string()],
289 description: "Test command".to_string(),
290 required: false,
291 arguments: vec![],
292 options: vec![],
293 implementation: "test_handler".to_string(),
294 };
295
296 let handler = Box::new(TestHandler {
297 name: "test".to_string(),
298 });
299
300 registry
301 .register(cmd_def, handler)
302 .expect("Failed to register command");
303
304 registry
305 }
306
307 #[test]
308 fn test_cli_interface_creation() {
309 let registry = create_test_registry();
310 let context = Box::new(TestContext::default());
311
312 let _cli = CliInterface::new(registry, context);
313 // If this compiles and runs, creation works
314 }
315
316 #[test]
317 fn test_cli_run_simple_command() {
318 let registry = create_test_registry();
319 let context = Box::new(TestContext::default());
320 let cli = CliInterface::new(registry, context);
321
322 let result = cli.run(vec!["test".to_string()]);
323 assert!(result.is_ok());
324 }
325
326 #[test]
327 fn test_cli_run_with_alias() {
328 let registry = create_test_registry();
329 let context = Box::new(TestContext::default());
330 let cli = CliInterface::new(registry, context);
331
332 let result = cli.run(vec!["t".to_string()]);
333 assert!(result.is_ok());
334 }
335
336 #[test]
337 fn test_cli_empty_args() {
338 let registry = create_test_registry();
339 let context = Box::new(TestContext::default());
340 let cli = CliInterface::new(registry, context);
341
342 let result = cli.run(vec![]);
343 assert!(result.is_err());
344
345 match result.unwrap_err() {
346 DynamicCliError::Parse(crate::error::ParseError::InvalidSyntax { .. }) => {}
347 other => panic!("Expected InvalidSyntax error, got: {:?}", other),
348 }
349 }
350
351 #[test]
352 fn test_cli_unknown_command() {
353 let registry = create_test_registry();
354 let context = Box::new(TestContext::default());
355 let cli = CliInterface::new(registry, context);
356
357 let result = cli.run(vec!["unknown".to_string()]);
358 assert!(result.is_err());
359
360 match result.unwrap_err() {
361 DynamicCliError::Parse(crate::error::ParseError::UnknownCommand { .. }) => {}
362 other => panic!("Expected UnknownCommand error, got: {:?}", other),
363 }
364 }
365
366 #[test]
367 fn test_cli_command_with_args() {
368 let mut registry = CommandRegistry::new();
369
370 // Command with argument
371 let cmd_def = CommandDefinition {
372 name: "greet".to_string(),
373 aliases: vec![],
374 description: "Greet someone".to_string(),
375 required: false,
376 arguments: vec![ArgumentDefinition {
377 name: "name".to_string(),
378 arg_type: ArgumentType::String,
379 required: true,
380 description: "Name to greet".to_string(),
381 validation: vec![],
382 }],
383 options: vec![],
384 implementation: "greet_handler".to_string(),
385 };
386
387 struct GreetHandler;
388 impl crate::executor::CommandHandler for GreetHandler {
389 fn execute(
390 &self,
391 _context: &mut dyn ExecutionContext,
392 args: &HashMap<String, String>,
393 ) -> Result<()> {
394 assert_eq!(args.get("name"), Some(&"Alice".to_string()));
395 Ok(())
396 }
397 }
398
399 registry.register(cmd_def, Box::new(GreetHandler)).unwrap();
400
401 let context = Box::new(TestContext::default());
402 let cli = CliInterface::new(registry, context);
403
404 let result = cli.run(vec!["greet".to_string(), "Alice".to_string()]);
405 assert!(result.is_ok());
406 }
407}