cl_parse

Crates.iocl_parse
lib.rscl_parse
version0.2.3
sourcesrc
created_at2024-06-28 15:37:56.656853
updated_at2024-06-28 15:44:29.993202
descriptionA library for defining and parsing commandline options and arguments
homepage
repositoryhttps://github.com/exaxisllc/cl_parse
max_upload_size
id1286803
size48,704
(berryware)

documentation

https://docs.rs/cl_parse/

README

Command Line Parse

CI

cl_parse is a library that allows you to define commandline options and arguments and then parse the commandline arguments based on that definition.

Motivation

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.

  • option aliases. e.g. -f, --file
  • options with negative values. e.g. --increment -1
  • flag concatenation. i.e. -xvgf is equivalent to -x -v -g -f
  • Auto usage message generation
  • Auto help message generation
  • -h, --help output provided by default
  • missing value detection for options
  • ability to define required options
  • option and argument validation. i.e. only defined options and arguments can be used
  • unordered options and arguments
  • retrieving the option or argument in the target type. e.g. i32, String, etc.

Example

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_argument("arg-0")
.add_argument("arg-1")
.add_argument("arg-2")
.parse(env::args());

// The asserts assume the following commandline
// program arg1 --boolean arg2 -n -1 arg3
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);

assert_eq!(cl.arguments(), 3);

let arg0:String = cl.argument(0);
assert_eq!(arg0, "arg1");

let arg1:String = cl.argument(1);
assert_eq!(arg1, "arg2");

let arg2:String = cl.argument(2);
assert_eq!(arg2, "arg3");
Commit count: 64

cargo fmt