pub struct Fli {
pub name: String,
pub version: String,
pub description: String,
pub root_command: FliCommand,
}Expand description
The main application struct for building CLI applications.
Fli serves as the entry point for defining commands, options, and running
the command-line parser. It wraps a root command that handles all subcommands.
§Examples
use fli::option_parser::ValueTypes;
use fli::Fli;
let mut app = Fli::new("myapp", "1.0.0", "A sample CLI application");
app.add_option("verbose", "Enable verbose output", "-v", "--verbose",
ValueTypes::None);
app.run();Fields§
§name: String§version: String§description: String§root_command: FliCommandImplementations§
Source§impl Fli
impl Fli
Sourcepub fn command(
&mut self,
name: &str,
description: &str,
) -> Result<&mut FliCommand>
pub fn command( &mut self, name: &str, description: &str, ) -> Result<&mut FliCommand>
Creates or retrieves a subcommand.
If a command with the given name doesn’t exist, it will be created and added to the root command. Returns a mutable reference to the command for chaining.
§Arguments
name- The command name (used to invoke it from CLI)description- Help text describing what the command does
§Returns
A mutable reference to the FliCommand instance.
§Panics
Panics if the command cannot be retrieved after insertion (should not happen).
§Examples
app.command("serve", "Start the web server")
.add_option("port", "Port to bind to", "-p", "--port",
ValueTypes::RequiredSingle(Value::Int(8080)));Sourcepub fn add_command(&mut self, command: FliCommand)
pub fn add_command(&mut self, command: FliCommand)
Adds a pre-configured command to the application.
Use this when you’ve constructed a FliCommand separately and want to
add it to the application.
§Arguments
command- A fully configuredFliCommandinstance
§Examples
let mut serve_cmd = FliCommand::new("serve", "Start server");
serve_cmd.add_option(/* ... */);
app.add_command(serve_cmd);Sourcepub fn set_callback(&mut self, callback: fn(&FliCallbackData))
pub fn set_callback(&mut self, callback: fn(&FliCallbackData))
Sets a callback function to execute when the root command is invoked.
This is useful for applications with a default action or for handling cases where no subcommand is specified.
§Arguments
callback- Function to execute, receivesFliCallbackDatawith parsed args
§Examples
app.set_callback(|data| {
println!("Running default action");
});Sourcepub fn add_option(
&mut self,
name: &str,
description: &str,
short_flag: &str,
long_flag: &str,
value: ValueTypes,
)
pub fn add_option( &mut self, name: &str, description: &str, short_flag: &str, long_flag: &str, value: ValueTypes, )
Adds an option to the root command.
Options are flags or parameters that can be passed to the application. They are accessible to all subcommands unless overridden.
§Arguments
name- Internal identifier for the optiondescription- Help text shown to usersshort_flag- Short form (e.g., “-v”)long_flag- Long form (e.g., “–verbose”)value- The type and default value for this option
§Examples
use fli::option_parser::ValueTypes;
app.add_option("config", "Config file path", "-c", "--config",
ValueTypes::OptionalSingle(None));Sourcepub fn mark_inheritable(&mut self, flag: &str) -> Result<()>
pub fn mark_inheritable(&mut self, flag: &str) -> Result<()>
Marks a single option as inheritable to all subcommands.
Inheritable options are automatically propagated to all subcommands created after marking. This is useful for common options like verbose, quiet, or color flags that should be available across all commands.
§Arguments
flag- The short or long flag of the option to mark as inheritable (e.g., “-v” or “–verbose”)
§Returns
Ok(()) if the option was successfully marked as inheritable, or an error if the flag doesn’t exist
§Examples
use fli::Fli;
use fli::option_parser::ValueTypes;
let mut app = Fli::new("myapp", "1.0.0", "My application");
app.add_option("verbose", "Enable verbose output", "-v", "--verbose", ValueTypes::None);
app.mark_inheritable("-v").unwrap();
// All subcommands will now have the -v/--verbose option
app.command("start", "Start the service").unwrap();§Notes
- Options must be added before marking them as inheritable
- Only subcommands created after marking will inherit the option
- Each subcommand gets its own copy of inherited options
Sourcepub fn mark_inheritable_many<I, S>(&mut self, flags: I) -> Result<()>
pub fn mark_inheritable_many<I, S>(&mut self, flags: I) -> Result<()>
Marks multiple options as inheritable to all subcommands.
This is a convenience method for marking several options at once, equivalent
to calling mark_inheritable() multiple times.
§Arguments
flags- An iterator of flag strings (short or long) to mark as inheritable
§Returns
Ok(()) if all options were successfully marked, or an error if any flag doesn’t exist
§Examples
use fli::Fli;
use fli::option_parser::ValueTypes;
let mut app = Fli::new("myapp", "1.0.0", "My application");
app.add_option("verbose", "Enable verbose", "-v", "--verbose", ValueTypes::None);
app.add_option("quiet", "Suppress output", "-q", "--quiet", ValueTypes::None);
app.add_option("color", "Enable colors", "-c", "--color", ValueTypes::None);
// Mark all three options as inheritable at once
app.mark_inheritable_many(&["-v", "-q", "-c"]).unwrap();
// All subcommands will now have -v, -q, and -c options§Notes
- All options must exist before calling this method
- If any flag is invalid, the entire operation fails
- Subcommands created after this call will inherit all marked options
Source§impl Fli
impl Fli
Sourcepub fn new(name: &str, version: &str, description: &str) -> Self
pub fn new(name: &str, version: &str, description: &str) -> Self
Creates a new CLI application.
§Arguments
name- The name of the application (used in help text and error messages)version- The version string (e.g., “1.0.0”)description- A brief description of what the application does
§Returns
A new Fli instance with an empty root command
§Examples
use fli::Fli;
let app = Fli::new("git", "2.0.0", "Distributed version control system");Sourcepub fn run(&mut self)
pub fn run(&mut self)
Parses command-line arguments and executes the appropriate command.
This method:
- Collects arguments from
std::env::args() - Parses them into a command chain
- Executes the matched command’s callback
- Exits with error code 1 if parsing fails
§Panics
Exits the process with code 1 if argument parsing fails or an invalid command is specified.
§Examples
let mut app = Fli::new("myapp", "1.0.0", "Description");
// ... configure app ...
app.run(); // Never returns on error§Note
This method calls std::process::exit() on errors. For library usage,
consider using a run_with_args() variant that returns Result.
Source§impl Fli
impl Fli
Sourcepub fn with_debug(self) -> Self
pub fn with_debug(self) -> Self
Enable debug mode for the application
Sourcepub fn add_debug_option(&mut self)
pub fn add_debug_option(&mut self)
Add a debug flag to root command