pub struct CliParser<'a> { /* private fields */ }Expand description
CLI argument parser
Parses command-line arguments according to a CommandDefinition.
The parser handles both positional arguments and named options
with type conversion and validation.
§Lifetime
The parser holds a reference to a CommandDefinition and therefore
has a lifetime parameter 'a that must outlive the parser.
§Example
use dynamic_cli::parser::cli_parser::CliParser;
use dynamic_cli::config::schema::{
CommandDefinition, OptionDefinition, ArgumentType
};
let definition = CommandDefinition {
name: "test".to_string(),
aliases: vec![],
description: "Test command".to_string(),
required: false,
arguments: vec![],
options: vec![
OptionDefinition {
name: "verbose".to_string(),
short: Some("v".to_string()),
long: Some("verbose".to_string()),
option_type: ArgumentType::Bool,
required: false,
default: Some("false".to_string()),
description: "Verbose output".to_string(),
choices: vec![],
}
],
implementation: "handler".to_string(),
};
let parser = CliParser::new(&definition);
let args = vec!["-v".to_string()];
let parsed = parser.parse(&args).unwrap();
assert_eq!(parsed.get("verbose"), Some(&"true".to_string()));Implementations§
Source§impl<'a> CliParser<'a>
impl<'a> CliParser<'a>
Sourcepub fn new(definition: &'a CommandDefinition) -> Self
pub fn new(definition: &'a CommandDefinition) -> Self
Sourcepub fn parse(&self, args: &[String]) -> Result<HashMap<String, String>>
pub fn parse(&self, args: &[String]) -> Result<HashMap<String, String>>
Parse command-line arguments into a HashMap
Parses the provided arguments according to the command definition. Positional arguments are matched in order, and options are matched by their short or long forms.
§Arguments
args- Slice of argument strings (excluding the command name)
§Returns
A HashMap mapping argument/option names to their string values. All values are stored as strings after type validation.
§Errors
ParseError::MissingArgumentif required arguments are missingParseError::MissingOptionif required options are missingParseError::UnknownOptionif an unrecognized option is providedParseError::TypeParseErrorif a value cannot be converted to its expected typeParseError::TooManyArgumentsif more positional arguments than expected
§Example
use dynamic_cli::parser::cli_parser::CliParser;
use dynamic_cli::config::schema::{
CommandDefinition, ArgumentDefinition, ArgumentType
};
let definition = CommandDefinition {
name: "greet".to_string(),
aliases: vec![],
description: "Greet someone".to_string(),
required: false,
arguments: vec![
ArgumentDefinition {
name: "name".to_string(),
arg_type: ArgumentType::String,
required: true,
description: "Name".to_string(),
validation: vec![],
}
],
options: vec![],
implementation: "handler".to_string(),
};
let parser = CliParser::new(&definition);
let result = parser.parse(&["Alice".to_string()]).unwrap();
assert_eq!(result.get("name"), Some(&"Alice".to_string()));Auto Trait Implementations§
impl<'a> Freeze for CliParser<'a>
impl<'a> RefUnwindSafe for CliParser<'a>
impl<'a> Send for CliParser<'a>
impl<'a> Sync for CliParser<'a>
impl<'a> Unpin for CliParser<'a>
impl<'a> UnwindSafe for CliParser<'a>
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
Mutably borrows from an owned value. Read more