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::missing_handler(resolved_name))
167 })?;
168
169 // Parse arguments using CLI parser
170 let parser = CliParser::new(definition);
171 let parsed_args = parser.parse(&args[1..])?;
172
173 // Get handler and execute command
174 let handler = self.registry.get_handler(resolved_name).ok_or_else(|| {
175 DynamicCliError::Execution(crate::error::ExecutionError::handler_not_found(
176 resolved_name,
177 &definition.implementation,
178 ))
179 })?;
180
181 handler.execute(&mut *self.context, &parsed_args)?;
182
183 Ok(())
184 }
185
186 /// Run the CLI with automatic error handling and exit
187 ///
188 /// This is a convenience method that:
189 /// 1. Runs the CLI with provided arguments
190 /// 2. Handles errors by displaying them to stderr
191 /// 3. Exits the process with appropriate exit code
192 ///
193 /// This method never returns.
194 ///
195 /// # Arguments
196 ///
197 /// * `args` - Command-line arguments
198 ///
199 /// # Example
200 ///
201 /// ```no_run
202 /// use dynamic_cli::interface::CliInterface;
203 /// use dynamic_cli::prelude::*;
204 ///
205 /// # #[derive(Default)]
206 /// # struct MyContext;
207 /// # impl ExecutionContext for MyContext {
208 /// # fn as_any(&self) -> &dyn std::any::Any { self }
209 /// # fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
210 /// # }
211 /// # fn main() {
212 /// let registry = CommandRegistry::new();
213 /// let context = Box::new(MyContext::default());
214 /// let cli = CliInterface::new(registry, context);
215 ///
216 /// // This will handle errors and exit automatically
217 /// cli.run_and_exit(std::env::args().skip(1).collect());
218 /// # }
219 /// ```
220 pub fn run_and_exit(self, args: Vec<String>) -> ! {
221 match self.run(args) {
222 Ok(()) => process::exit(0),
223 Err(e) => {
224 display_error(&e);
225
226 // Exit with appropriate code based on error type
227 let exit_code = match e {
228 DynamicCliError::Parse(_) => 2,
229 DynamicCliError::Validation(_) => 2,
230 DynamicCliError::Execution(_) => 1,
231 _ => 3,
232 };
233
234 process::exit(exit_code);
235 }
236 }
237 }
238}
239
240#[cfg(test)]
241mod tests {
242 use super::*;
243 use crate::config::schema::{ArgumentDefinition, ArgumentType, CommandDefinition};
244 use std::collections::HashMap;
245
246 // Test context
247 #[derive(Default)]
248 struct TestContext {
249 executed_command: Option<String>,
250 }
251
252 impl ExecutionContext for TestContext {
253 fn as_any(&self) -> &dyn std::any::Any {
254 self
255 }
256
257 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
258 self
259 }
260 }
261
262 // Test handler
263 struct TestHandler {
264 name: String,
265 }
266
267 impl crate::executor::CommandHandler for TestHandler {
268 fn execute(
269 &self,
270 context: &mut dyn ExecutionContext,
271 _args: &HashMap<String, String>,
272 ) -> Result<()> {
273 let ctx = crate::context::downcast_mut::<TestContext>(context)
274 .expect("Failed to downcast context");
275 ctx.executed_command = Some(self.name.clone());
276 Ok(())
277 }
278 }
279
280 fn create_test_registry() -> CommandRegistry {
281 let mut registry = CommandRegistry::new();
282
283 // Create a simple command definition
284 let cmd_def = CommandDefinition {
285 name: "test".to_string(),
286 aliases: vec!["t".to_string()],
287 description: "Test command".to_string(),
288 required: false,
289 arguments: vec![],
290 options: vec![],
291 implementation: "test_handler".to_string(),
292 };
293
294 let handler = Box::new(TestHandler {
295 name: "test".to_string(),
296 });
297
298 registry
299 .register(cmd_def, handler)
300 .expect("Failed to register command");
301
302 registry
303 }
304
305 #[test]
306 fn test_cli_interface_creation() {
307 let registry = create_test_registry();
308 let context = Box::new(TestContext::default());
309
310 let _cli = CliInterface::new(registry, context);
311 // If this compiles and runs, creation works
312 }
313
314 #[test]
315 fn test_cli_run_simple_command() {
316 let registry = create_test_registry();
317 let context = Box::new(TestContext::default());
318 let cli = CliInterface::new(registry, context);
319
320 let result = cli.run(vec!["test".to_string()]);
321 assert!(result.is_ok());
322 }
323
324 #[test]
325 fn test_cli_run_with_alias() {
326 let registry = create_test_registry();
327 let context = Box::new(TestContext::default());
328 let cli = CliInterface::new(registry, context);
329
330 let result = cli.run(vec!["t".to_string()]);
331 assert!(result.is_ok());
332 }
333
334 #[test]
335 fn test_cli_empty_args() {
336 let registry = create_test_registry();
337 let context = Box::new(TestContext::default());
338 let cli = CliInterface::new(registry, context);
339
340 let result = cli.run(vec![]);
341 assert!(result.is_err());
342
343 match result.unwrap_err() {
344 DynamicCliError::Parse(crate::error::ParseError::InvalidSyntax { .. }) => {}
345 other => panic!("Expected InvalidSyntax error, got: {:?}", other),
346 }
347 }
348
349 #[test]
350 fn test_cli_unknown_command() {
351 let registry = create_test_registry();
352 let context = Box::new(TestContext::default());
353 let cli = CliInterface::new(registry, context);
354
355 let result = cli.run(vec!["unknown".to_string()]);
356 assert!(result.is_err());
357
358 match result.unwrap_err() {
359 DynamicCliError::Parse(crate::error::ParseError::UnknownCommand { .. }) => {}
360 other => panic!("Expected UnknownCommand error, got: {:?}", other),
361 }
362 }
363
364 #[test]
365 fn test_cli_command_with_args() {
366 let mut registry = CommandRegistry::new();
367
368 // Command with argument
369 let cmd_def = CommandDefinition {
370 name: "greet".to_string(),
371 aliases: vec![],
372 description: "Greet someone".to_string(),
373 required: false,
374 arguments: vec![ArgumentDefinition {
375 name: "name".to_string(),
376 arg_type: ArgumentType::String,
377 required: true,
378 description: "Name to greet".to_string(),
379 validation: vec![],
380 }],
381 options: vec![],
382 implementation: "greet_handler".to_string(),
383 };
384
385 struct GreetHandler;
386 impl crate::executor::CommandHandler for GreetHandler {
387 fn execute(
388 &self,
389 _context: &mut dyn ExecutionContext,
390 args: &HashMap<String, String>,
391 ) -> Result<()> {
392 assert_eq!(args.get("name"), Some(&"Alice".to_string()));
393 Ok(())
394 }
395 }
396
397 registry.register(cmd_def, Box::new(GreetHandler)).unwrap();
398
399 let context = Box::new(TestContext::default());
400 let cli = CliInterface::new(registry, context);
401
402 let result = cli.run(vec!["greet".to_string(), "Alice".to_string()]);
403 assert!(result.is_ok());
404 }
405}