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
impl CommandOptionsParser
Sourcepub fn mark_inheritable(&mut self, flag: &str) -> Result<()>
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 inheritableErr(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
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 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 inheritableErr(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
Sourcepub fn inheritable_options_builder(&self) -> CommandOptionsParserBuilder
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
Sourcepub fn update_option_value(
&mut self,
flag: &str,
value: ValueTypes,
) -> Result<()>
pub fn update_option_value( &mut self, flag: &str, value: ValueTypes, ) -> Result<()>
Sourcepub fn add_option(&mut self, option: SingleOption)
pub fn add_option(&mut self, option: SingleOption)
Sourcepub fn get_option_by_short_flag(&self, flag: &str) -> Option<&SingleOption>
pub fn get_option_by_short_flag(&self, flag: &str) -> Option<&SingleOption>
Sourcepub fn get_option_by_long_flag(&self, flag: &str) -> Option<&SingleOption>
pub fn get_option_by_long_flag(&self, flag: &str) -> Option<&SingleOption>
Sourcepub fn has_option(&self, flag: &str) -> bool
pub fn has_option(&self, flag: &str) -> bool
Sourcepub fn get_options(&self) -> &Vec<SingleOption>
pub fn get_options(&self) -> &Vec<SingleOption>
Returns all registered options.
Sourcepub fn get_option_expected_value_type(&self, flag: &str) -> Option<&ValueTypes>
pub fn get_option_expected_value_type(&self, flag: &str) -> Option<&ValueTypes>
Trait Implementations§
Source§impl Clone for CommandOptionsParser
impl Clone for CommandOptionsParser
Source§fn clone(&self) -> CommandOptionsParser
fn clone(&self) -> CommandOptionsParser
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more