CliRouter

Struct CliRouter 

Source
pub struct CliRouter {
    pub app_name: String,
    pub version_message: String,
    pub handler_alias: Option<String>,
    pub handlers: HashMap<String, CliHandler>,
    pub commands: HashMap<String, Box<dyn CliCommand>>,
    pub categories: HashMap<String, CliCategory>,
    pub ignore_flags: HashMap<String, bool>,
    pub global_flags: Vec<CliGlobalFlag>,
    pub parsed_global_flags: bool,
    pub children: HashMap<String, Box<CliRouter>>,
}
Expand description

The main router for CLI commands.

This struct manages all registered commands, categories, and global flags. It handles parsing command line arguments and routing them to the appropriate command handler.

Fields§

§app_name: String

The application name displayed in help screens.

§version_message: String

Version message displayed with -v or –version flags.

§handler_alias: Option<String>

Internal: Alias of the handler for this router node.

§handlers: HashMap<String, CliHandler>

Map of command aliases to their handlers.

§commands: HashMap<String, Box<dyn CliCommand>>

Map of command aliases to their implementations.

§categories: HashMap<String, CliCategory>

Map of category aliases to their definitions.

§ignore_flags: HashMap<String, bool>

Flags to ignore during command lookup.

§global_flags: Vec<CliGlobalFlag>

List of global flags available to all commands.

§parsed_global_flags: bool

Internal: Whether global flags have been parsed.

§children: HashMap<String, Box<CliRouter>>

Internal: Child routers for nested command structures.

Implementations§

Source§

impl CliRouter

Source

pub fn new() -> Self

Creates a new CLI router.

§Example
use falcon_cli::CliRouter;

let mut router = CliRouter::new();
router.app_name("My Application");
Source

pub fn add<T>( &mut self, alias: &str, shortcuts: Vec<&str>, value_flags: Vec<&str>, )
where T: CliCommand + Default + 'static,

Registers a command with the router.

Links a struct that implements CliCommand to a command name, along with optional shortcuts and flags that expect values.

§Arguments
  • alias - The full name of the command
  • shortcuts - Vector of alternate ways to invoke the command
  • value_flags - Vector of flags that expect a value (e.g., ["--output", "--config"])
§Example
let mut router = CliRouter::new();
router.add::<BuildCommand>(
    "build",
    vec!["b"],
    vec!["--output", "--config"]
);
Source

pub fn app_name(&mut self, name: &str)

Sets the application name displayed in help screens.

§Arguments
  • name - The application name
§Example
let mut router = CliRouter::new();
router.app_name("MyApp v1.0");
Source

pub fn version_message(&mut self, msg: &str)

Sets the version message displayed with -v or –version.

§Arguments
  • msg - The version message
§Example
let mut router = CliRouter::new();
router.version_message("MyApp version 1.0.0");
Source

pub fn global(&mut self, short: &str, long: &str, is_value: bool, desc: &str)

Registers a global flag available to all commands.

Global flags are processed before command routing and can be checked using has_global() or retrieved using get_global().

§Arguments
  • short - Short form of the flag (e.g., “-v”)
  • long - Long form of the flag (e.g., “–verbose”)
  • is_value - Whether the flag expects a value
  • desc - Description of what the flag does
§Example
let mut router = CliRouter::new();
router.global("-v", "--verbose", false, "Enable verbose output");
router.global("-c", "--config", true, "Specify config file");
Source

pub fn has_global(&mut self, flag: &str) -> bool

Checks if a global flag was provided.

§Arguments
  • flag - The flag to check (short or long form)
§Returns

Returns true if the flag was provided, false otherwise.

§Example
let mut router = CliRouter::new();
router.global("-v", "--verbose", false, "Verbose output");
if router.has_global("-v") {
    println!("Verbose mode enabled");
}
Source

pub fn get_global(&mut self, flag: &str) -> Option<String>

Gets the value of a global flag.

§Arguments
  • flag - The flag to retrieve (short or long form)
§Returns

Returns Some(String) with the flag’s value, or None if not provided or not a value flag.

§Example
let mut router = CliRouter::new();
router.global("-c", "--config", true, "Config file");
if let Some(config) = router.get_global("--config") {
    println!("Using config: {}", config);
}
Source

pub fn ignore(&mut self, flag: &str, is_value: bool)

Adds a flag to ignore during command lookup.

Ignored flags are stripped from arguments before command routing occurs.

§Arguments
  • flag - The flag to ignore
  • is_value - Whether the flag expects a value (which should also be ignored)
§Example
let mut router = CliRouter::new();
router.ignore("--internal-flag", false);
router.ignore("--debug-port", true);
Source

pub fn lookup(&mut self) -> Option<(CliRequest, &Box<dyn CliCommand>)>

Looks up and routes to the appropriate command handler.

This method parses command line arguments, determines which command to execute, and returns the parsed request along with the command handler. It is automatically called by cli_run() and typically should not be called manually.

§Returns

Returns Some((CliRequest, &Box<dyn CliCommand>)) if a command was found, or None if no command matched.

Source

pub fn add_category(&mut self, alias: &str, title: &str, description: &str)

Adds a category for organizing related commands.

Categories are displayed in the help index and can contain multiple commands. Useful for organizing large CLI applications with many commands.

§Arguments
  • alias - The category’s identifier
  • title - The display title for the category
  • description - Description of what commands in this category do
§Example
let mut router = CliRouter::new();
router.add_category("database", "Database Commands", "Manage database operations");
router.add_category("user", "User Commands", "Manage user accounts");

Trait Implementations§

Source§

impl Default for CliRouter

Source§

fn default() -> CliRouter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,