3 unstable releases

Uses old Rust 2015

0.2.0 Dec 2, 2017
0.1.1 Dec 1, 2017
0.1.0 Nov 30, 2017

#4 in #args-parser

Download history 9/week @ 2023-08-03 14/week @ 2023-08-10 15/week @ 2023-08-17 11/week @ 2023-08-24 10/week @ 2023-08-31 21/week @ 2023-09-07 9/week @ 2023-09-14 6/week @ 2023-09-21 12/week @ 2023-09-28 10/week @ 2023-10-05 11/week @ 2023-10-12 14/week @ 2023-10-19 21/week @ 2023-10-26 15/week @ 2023-11-02 16/week @ 2023-11-09 18/week @ 2023-11-16

72 downloads per month
Used in kubeauth

WTFPL license

60 lines

Command line parser for rust (clpr)

A bare-bones rust command line parser.


This project has no external dependencies

What's new

The most recent version (0.2.0) contains the following:

  • The return value is now a Vector
  • Under the hood improvements


Add the crate to your Cargo.toml file and you can use it to parse incoming arguments and flags.

cmdparser = "0.2.0"

And in the file that will consume the crate:

extern crate cmdparser;

Parse incoming arguments and flags by calling "parse()" and receiving a touple.

let (arguments, flags) = Parser::new().parse();

First element of the touple is a HashMap<String, Vec> that contains all of the key value arguments received from the command line. By default, "-" and "--" prefixes are parsed and accepted.

If the user supplies multiple arguments under different positions they will be overwritten by default. To disable this behavior use


When merge_values is set to true under a parser instance, and the user passes the following arguments

$ ./executable -i foo.jpg -i bar.jpg

The parser will merge "foo.jpg" and "bar.jpg" values under the "i" argument. If merge_values is set to false (which is the default behavior) the "i" argument will hold a single value of "bar.jpg".

If you want to limit the user to a certain prefix(es) use the strict_prefix(prefixes: Vec<String>) method.

let prefixes = vec!["~".to_owned()];
let (arguments, flags) = Parser::new().strict_prefix(prefixes).parse();

The created parser will only accept arguments prefixed with a tilda (~) character.


Running your program with the following arguments:

$ cargo run -- -i foo.jpg bar.png -zero true -flag -param 100 -o
$ ./executable -i foo.jpg -o bar.txt -p -r 10 -t -z -l 100

Yields the following:

ArgumentList: {"param": ["100"], "zero": ["true"], "i": ["foo.jpg", "bar.png"]}
Flags: ["flag", "o"]

To consume them the simplest patterns would be, for an argument:

let images: Vec<String>= arguments.get("i").or(arguments.get("images")).expect("No image argument provided");

For a flag:

if flags.contains(&"opaque".to_owned()) || flags.contains(&"o".to_owned()) {

The given code would look for -images or -i and in case they're not found panic with the erro message.

Comparison to clap-rs

While clap is a robust, feature rich and tested package, it has 886 reverse dependencies and it's simply an overkill for most usages.


The parser will parse 100 flags and 100 single value arguments in ~1ms on a 4c4t i5.


As this is a very basic parser, and I don't have a lot of Rust experience, if you want to contribute and improve something, submit a pull request.


Nenad Lukic - lnenad



No runtime deps