App

Struct App 

Source
pub struct App {
    pub info: AppInfo,
    pub root: Command,
}

Fields§

§info: AppInfo§root: Command

Implementations§

Source§

impl App

Source

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().

Source

pub fn command(self, cmd: Command) -> Self

Source

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(())
    }));
Source

pub fn pre_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self

Set the pre_run handler for the root command

Source

pub fn post_run(self, f: Box<dyn Fn(&mut dyn Context) -> Result<()>>) -> Self

Set the post_run handler for the root command

Source

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.

Source

pub fn parse(&self, args: Vec<String>) -> Result<ContextInfo>

Source

pub fn execute(&self) -> Result<()>

Run the application with command line arguments

This will:

  1. Parse command-line arguments from std::env::args
  2. Handle –version flag (print version and exit)
  3. Handle –help flag or no command (print help and exit)
  4. Find and execute the matched command

Returns Ok(()) on success, or an error if:

  • Argument parsing fails
  • Command not found
  • Command execution fails
Source

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.

Source

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
Source

pub fn available_commands(&self) -> Vec<&str>

Get all top-level command names for error messages

Source

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.

Source

pub fn print_version(&self)

Print version information to stdout

Source

pub fn write_version(&self, w: &mut dyn Write) -> Result<()>

Write version information to a writer

Source

pub fn print_help(&self)

Print help information for the application

Source

pub fn print_help_for_path(&self, path: &[String])

Print help information for a specific command path

Source

pub fn write_help_for_path( &self, w: &mut dyn Write, path: &[String], ) -> Result<()>

Write help information for a specific command path to a writer

Auto Trait Implementations§

§

impl Freeze for App

§

impl !RefUnwindSafe for App

§

impl !Send for App

§

impl !Sync for App

§

impl Unpin for App

§

impl !UnwindSafe for App

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.