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: StringThe application name displayed in help screens.
version_message: StringVersion 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: boolInternal: Whether global flags have been parsed.
children: HashMap<String, Box<CliRouter>>Internal: Child routers for nested command structures.
Implementations§
Source§impl CliRouter
impl CliRouter
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new CLI router.
§Example
use falcon_cli::CliRouter;
let mut router = CliRouter::new();
router.app_name("My Application");Sourcepub fn add<T>(
&mut self,
alias: &str,
shortcuts: Vec<&str>,
value_flags: Vec<&str>,
)where
T: CliCommand + Default + 'static,
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 commandshortcuts- Vector of alternate ways to invoke the commandvalue_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"]
);Sourcepub fn version_message(&mut self, msg: &str)
pub fn version_message(&mut self, msg: &str)
Sourcepub fn global(&mut self, short: &str, long: &str, is_value: bool, desc: &str)
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 valuedesc- 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");Sourcepub fn has_global(&mut self, flag: &str) -> bool
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");
}Sourcepub fn get_global(&mut self, flag: &str) -> Option<String>
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);
}Sourcepub fn ignore(&mut self, flag: &str, is_value: bool)
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 ignoreis_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);Sourcepub fn lookup(&mut self) -> Option<(CliRequest, &Box<dyn CliCommand>)>
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.
Sourcepub fn add_category(&mut self, alias: &str, title: &str, description: &str)
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 identifiertitle- The display title for the categorydescription- 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§
Auto Trait Implementations§
impl Freeze for CliRouter
impl !RefUnwindSafe for CliRouter
impl !Send for CliRouter
impl !Sync for CliRouter
impl Unpin for CliRouter
impl !UnwindSafe for CliRouter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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