Fli

Struct Fli 

Source
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: FliCommand

Implementations§

Source§

impl Fli

Source

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

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 configured FliCommand instance
§Examples
let mut serve_cmd = FliCommand::new("serve", "Start server");
serve_cmd.add_option(/* ... */);
app.add_command(serve_cmd);
Source

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, receives FliCallbackData with parsed args
§Examples
app.set_callback(|data| {
    println!("Running default action");
});
Source

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 option
  • description - Help text shown to users
  • short_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));
Source

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
Source

pub fn mark_inheritable_many<I, S>(&mut self, flags: I) -> Result<()>
where I: IntoIterator<Item = S>, S: AsRef<str>,

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

Source

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");
Source

pub fn run(&mut self)

Parses command-line arguments and executes the appropriate command.

This method:

  1. Collects arguments from std::env::args()
  2. Parses them into a command chain
  3. Executes the matched command’s callback
  4. 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

Source

pub fn with_debug(self) -> Self

Enable debug mode for the application

Source

pub fn add_debug_option(&mut self)

Add a debug flag to root command

Auto Trait Implementations§

§

impl Freeze for Fli

§

impl RefUnwindSafe for Fli

§

impl Send for Fli

§

impl Sync for Fli

§

impl Unpin for Fli

§

impl UnwindSafe for Fli

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.