CliParser

Struct CliParser 

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

Source

pub fn new(definition: &'a CommandDefinition) -> Self

Create a new CLI parser for the given command definition

§Arguments
  • definition - The command definition specifying expected arguments
§Example
use dynamic_cli::parser::cli_parser::CliParser;
use dynamic_cli::config::schema::CommandDefinition;

let parser = CliParser::new(&definition);
Source

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
§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> 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.