#cli-parser #parser #arguments #simple-cli-parser #add-arg

simple-cli-parser

A crate for parsing commands and arguemnts passed to the console

2 releases

Uses new Rust 2024

0.1.1 Mar 29, 2025
0.1.0 Mar 29, 2025

#692 in Command-line interface

Download history 206/week @ 2025-03-26 37/week @ 2025-04-02

243 downloads per month

MIT license

20KB
302 lines

A crate for parsing commands and arguemnts passed to the console.

This can parse commands with various arguments. It currently only supports option arguments (or args beginning with a "-"), but an update for regular params and argument rules (like ordering) will be coming soon.

Getting Started

To start, create a new Parser struct and add a couple of [Arg]s to it using the [Parser::add_arg()] or [Parser::add_args()] methods. Then, call the [Parser::parse()] method with std::env::Args passed in.

use cli_parser::{ Parser, Arg };
use std::env;

fn main() {
    let parser = Parser::new();
    let my_arg = Arg::new().flag("help").short('h');
    parser.add_arg(my_arg);
    
    let mut args = env::args();

    // Don't include the first argument
    args.next();
    
    let hashmap = parser.parse(&mut args).unwrap();

    if hashmap.contains_key("help") {
        println!("Help argument called!");
    }
}

Simple_CLI_Parser

A crate for parsing commands and arguemnts passed to the console.

This can parse commands from std::env::args() with various arguments. It currently supports optional arguments (both short and long) as well as required parameters. Note that order is important for required parameters, but not for optional arguments.

Installation

Run the following Cargo command in your project directory:

cargo add simple-cli-parser

Or add the following line to your Cargo.toml:

simple-cli-parser = "0.1.0"

Example Usage

use simple_cli_parser::*;

fn main() {
    let parser = Parser::new();
    let arg = Arg::new().param("num");
    parser.add_arg(arg);

    let mut args = std::env::args();
    args.next();

    let hashmap = parser.parse(&mut args).unwrap();
    println!("{}", hashmap.get("num"));
}

No runtime deps