modcli/command.rs
1/// Defines the trait for commands to implement.
2///
3/// # Example
4/// ```no_run
5/// use modcli::command::Command;
6/// use modcli::ModCli;
7///
8/// struct Hello;
9///
10/// impl Command for Hello {
11/// fn name(&self) -> &str { "hello" }
12/// fn help(&self) -> Option<&str> { Some("Greets the user") }
13/// fn validate(&self, _args: &[String]) -> Result<(), String> { Ok(()) }
14/// fn execute(&self, _args: &[String]) { println!("Hello!"); }
15/// }
16///
17/// let mut cli = ModCli::new();
18/// cli.registry.register(Box::new(Hello));
19/// let args = vec!["hello".to_string()];
20/// cli.run(args);
21/// ```
22use crate::loader::CommandRegistry;
23
24pub trait Command {
25 fn name(&self) -> &str;
26
27 fn aliases(&self) -> &[&str] {
28 &[]
29 }
30
31 fn help(&self) -> Option<&str> {
32 None
33 }
34
35 fn hidden(&self) -> bool {
36 false
37 }
38
39 fn validate(&self, _args: &[String]) -> Result<(), String> {
40 Ok(())
41 }
42
43 fn execute(&self, args: &[String]);
44
45 /// Execute with access to the registry context. Default delegates to `execute`.
46 /// Commands that need registry access (e.g., `help`) can override this.
47 fn execute_with(&self, args: &[String], _registry: &CommandRegistry) {
48 self.execute(args)
49 }
50}