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 /// Capability requirements for visibility/authorization.
40 /// The parent application grants capabilities at runtime on the registry.
41 /// Default: no requirements.
42 fn required_caps(&self) -> &[&str] {
43 &[]
44 }
45
46 fn validate(&self, _args: &[String]) -> Result<(), String> {
47 Ok(())
48 }
49
50 fn execute(&self, args: &[String]);
51
52 /// Execute with access to the registry context. Default delegates to `execute`.
53 /// Commands that need registry access (e.g., `help`) can override this.
54 fn execute_with(&self, args: &[String], _registry: &CommandRegistry) {
55 self.execute(args)
56 }
57}