CommandOptionsParser

Struct CommandOptionsParser 

Source
pub struct CommandOptionsParser {
    pub options: Vec<SingleOption>,
    /* private fields */
}
Expand description

Parser for command options that maps flags to their configurations.

Maintains internal HashMaps for O(1) lookups by short or long flags.

§Examples

let mut parser = CommandOptionsParser::new();
parser.add_option(SingleOption {
    name: "verbose".to_string(),
    short_flag: "-v".to_string(),
    long_flag: "--verbose".to_string(),
    value: ValueTypes::None,
    description: "Enable verbose output".to_string(),
});

Fields§

§options: Vec<SingleOption>

Implementations§

Source§

impl CommandOptionsParser

Source

pub fn new() -> Self

Creates a new empty option parser.

Source

pub fn mark_inheritable(&mut self, flag: &str) -> Result<()>

Marks a single option as inheritable by its flag.

Inheritable options are automatically passed down to subcommands, eliminating the need to redefine common options (like --verbose, --quiet, --color) for every subcommand.

§Arguments
  • flag - The short or long flag of the option to mark as inheritable (e.g., “-v” or “–verbose”)
§Returns
  • Ok(()) - If the flag was found and successfully marked as inheritable
  • Err(FliError::OptionNotFound) - If the flag doesn’t correspond to any registered option
§Examples
use fli::option_parser::{CommandOptionsParser, ValueTypes};

let mut parser = CommandOptionsParser::new();
parser.add_option("verbose", "Enable verbose output", "-v", "--verbose", ValueTypes::None);
parser.mark_inheritable("-v").unwrap();

// Now the -v flag will be automatically available to all subcommands
§Notes
  • If an option is already marked as inheritable, calling this again has no effect
  • The option must exist before it can be marked as inheritable
  • Only the flag is needed, not the option name
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 in a single call.

This is a convenience method for marking several options as inheritable at once. It accepts any iterable of items that can be referenced as strings (short or long flags).

§Arguments
  • flags - An iterable of flag strings (e.g., &["-v", "--quiet", "--color"])
§Returns
  • Ok(()) - If all flags were found and successfully marked as inheritable
  • Err(FliError::OptionNotFound) - If any flag is not found (processing stops at first error)
§Examples
use fli::option_parser::{CommandOptionsParser, ValueTypes};

let mut parser = CommandOptionsParser::new();
parser.add_option("verbose", "Enable verbose output", "-v", "--verbose", ValueTypes::None);
parser.add_option("quiet", "Suppress output", "-q", "--quiet", ValueTypes::None);
parser.add_option("color", "Enable colors", "-c", "--color", ValueTypes::None);

// Mark all three as inheritable at once
parser.mark_inheritable_many(&["-v", "-q", "-c"]).unwrap();
§Notes
  • Processing stops at the first error encountered
  • No partial marking occurs - if one flag fails, previously processed flags remain marked
  • You can mix short and long flags in the same call
Source

pub fn inheritable_options_builder(&self) -> CommandOptionsParserBuilder

Creates a builder containing only the options marked as inheritable.

This method is primarily used internally to propagate inheritable options to subcommands. It returns a CommandOptionsParserBuilder pre-populated with clones of all options that have been marked as inheritable.

§Returns

A CommandOptionsParserBuilder containing clones of all inheritable options

§Examples
use fli::option_parser::{CommandOptionsParser, ValueTypes};

let mut parser = CommandOptionsParser::new();
parser.add_option("verbose", "Enable verbose output", "-v", "--verbose", ValueTypes::None);
parser.add_option("quiet", "Suppress output", "-q", "--quiet", ValueTypes::None);
parser.mark_inheritable("-v").unwrap();

// Get a builder with only the inheritable options
let builder = parser.inheritable_options_builder();
// This builder contains only the --verbose option
§Notes
  • The returned builder is independent - modifying it doesn’t affect the parent parser
  • Options are cloned, so changes to the parent won’t affect already-created builders
  • If no options are marked as inheritable, returns an empty builder
  • This is typically used when creating subcommands to inherit parent options
Source

pub fn update_option_value( &mut self, flag: &str, value: ValueTypes, ) -> Result<()>

Updates the value of an existing option.

§Arguments
  • flag - The flag identifying the option
  • value - The new value to set
§Returns
  • Ok(()) - If update succeeded
  • Err(String) - If option not found
§Errors

Returns an error if the flag doesn’t match any registered option.

Source

pub fn add_option(&mut self, option: SingleOption)

Registers a new option with the parser.

§Arguments
  • option - The option to add
§Note

Overwrites existing options with the same flags without warning.

Source

pub fn get_option_by_short_flag(&self, flag: &str) -> Option<&SingleOption>

Retrieves an option by its short flag.

§Arguments
  • flag - The short flag (e.g., “-v”)
§Returns
  • Some(&SingleOption) - If found
  • None - If not found
Source

pub fn get_option_by_long_flag(&self, flag: &str) -> Option<&SingleOption>

Retrieves an option by its long flag.

§Arguments
  • flag - The long flag (e.g., “–verbose”)
§Returns
  • Some(&SingleOption) - If found
  • None - If not found
Source

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

Checks if an option with the given flag exists.

§Arguments
  • flag - Either short or long flag
§Returns

true if the option exists, false otherwise

Source

pub fn get_options(&self) -> &Vec<SingleOption>

Returns all registered options.

Source

pub fn get_option_expected_value_type(&self, flag: &str) -> Option<&ValueTypes>

Gets the expected value type for an option.

Useful for knowing whether an option expects values before parsing.

§Arguments
  • flag - Either short or long flag
§Returns
  • Some(&ValueTypes) - The expected value type
  • None - If option doesn’t exist

Trait Implementations§

Source§

impl Clone for CommandOptionsParser

Source§

fn clone(&self) -> CommandOptionsParser

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CommandOptionsParser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.