| Crates.io | cl_parse |
| lib.rs | cl_parse |
| version | 0.3.2 |
| created_at | 2024-06-28 15:37:56.656853+00 |
| updated_at | 2025-12-18 19:43:24.52113+00 |
| description | A library for defining and parsing commandline options and arguments |
| homepage | |
| repository | https://github.com/exaxisllc/cl_parse |
| max_upload_size | |
| id | 1286803 |
| size | 67,160 |
cl_parse is a library that allows you to define commandline options and arguments and then
parse the commandline arguments based on that definition.
cl_parse was developed to allow the most common commandline options that are used in
modern commandline utilities. It was also designed for ease of use. The following are the features
implemented in cl_parse.
use std::env;
use cl_parse::CommandLineDef;
let cl = CommandLineDef::new()
.add_flag(vec!["-b","--boolean"], "A boolean value")
.add_flag(vec!["-f","--faux"], "Another boolean value")
.add_option(vec!["-n","--num"], Some("num"), None, "A required numeric value")
.add_option_with_values(vec!["--level"], Some("level"), Some("med"), "Operating Speed", vec!["low", "med", "high"])
.add_argument("arg1_name") // define the argument names
.add_argument("arg2_name")
.add_argument("arg3_name")
.parse(env::args());
// The asserts assume the following commandline
// program arg1_value --boolean arg2_value -n -1 arg3_value --level low
assert_eq!(cl.program_name(), "program");
// aliases are updated
let b:bool = cl.option("-b");
assert_eq!(b, true);
let boolean:bool = cl.option("--boolean");
assert_eq!(boolean, true);
// flags default to false
let f:bool = cl.option("-f");
assert_eq!(f, false);
// all aliased are updated
let faux:bool = cl.option("--faux");
assert_eq!(faux,false);
let n:i16 = cl.option("-n");
assert_eq!(n, -1);
let level:String = cl.option("--level");
assert_eq!(level, "low");
assert_eq!(cl.arguments(), 3);
let arg1:String = cl.argument("arg1_name"); // retrieve the argument value by the argument name
assert_eq!(arg1, "arg1_value");
let arg2:String = cl.argument("arg2_name");
assert_eq!(arg2, "arg2_value");
let arg3:String = cl.argument("arg3_name");
assert_eq!(arg3, "arg3_value");