pub struct App {
pub info: AppInfo,
pub root: Command,
}Fields§
§info: AppInfo§root: CommandImplementations§
Source§impl App
impl App
Sourcepub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
Create a new application with name and version
This is the recommended way to create an App. Use method chaining
to configure the application before calling run().
pub fn command(self, cmd: Command) -> Self
Sourcepub fn run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
pub fn run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
Set the run handler for the root command
This allows the app to execute logic directly without requiring subcommands. Useful for simple CLIs or testing scenarios.
§Example
use xacli_core::App;
let app = App::new("myapp", "1.0.0")
.run(Box::new(|ctx| {
println!("Hello from root!");
Ok(())
}));Sourcepub fn pre_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
pub fn pre_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
Set the pre_run handler for the root command
Sourcepub fn post_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
pub fn post_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self
Set the post_run handler for the root command
Sourcepub fn arg(self, arg: Arg) -> Self
pub fn arg(self, arg: Arg) -> Self
Add an argument to the root command
This allows adding arguments that apply when no subcommand is specified.
pub fn parse(&self, args: Vec<String>) -> Result<ContextInfo>
Sourcepub fn execute(&self) -> Result<()>
pub fn execute(&self) -> Result<()>
Run the application with command line arguments
This will:
- Parse command-line arguments from std::env::args
- Handle –version flag (print version and exit)
- Handle –help flag or no command (print help and exit)
- Find and execute the matched command
Returns Ok(()) on success, or an error if:
- Argument parsing fails
- Command not found
- Command execution fails
Sourcepub fn execute_with_ctx(&self, ctx: &mut dyn Context) -> Result<()>
pub fn execute_with_ctx(&self, ctx: &mut dyn Context) -> Result<()>
Run the application with a pre-configured context
This is useful for testing where you want to use a MockContext to capture output and inject input events.
The context should already contain the parsed arguments.
Sourcepub fn find_command_by_path(&self, path: &[String]) -> Option<&Command>
pub fn find_command_by_path(&self, path: &[String]) -> Option<&Command>
Find a command by its path
The path is a slice of command names, starting from the app name. For example, [“app”, “get”, “pod”] would find the “pod” subcommand of the “get” command.
Returns None if:
- The path is empty
- The first element doesn’t match the app name
- Any command in the path is not found
Sourcepub fn available_commands(&self) -> Vec<&str>
pub fn available_commands(&self) -> Vec<&str>
Get all top-level command names for error messages
Sourcepub fn check(&self) -> Result<()>
pub fn check(&self) -> Result<()>
Check the application configuration for errors
This verifies that:
- All leaf commands (commands without subcommands) have a run_fn set
Returns Ok(()) if all checks pass, or Error with details of what’s wrong.
Sourcepub fn print_version(&self)
pub fn print_version(&self)
Print version information to stdout
Sourcepub fn write_version(&self, w: &mut dyn Write) -> Result<()>
pub fn write_version(&self, w: &mut dyn Write) -> Result<()>
Write version information to a writer
Sourcepub fn print_help(&self)
pub fn print_help(&self)
Print help information for the application
Sourcepub fn print_help_for_path(&self, path: &[String])
pub fn print_help_for_path(&self, path: &[String])
Print help information for a specific command path