getopt3

Crates.iogetopt3
lib.rsgetopt3
version2.0.1
sourcesrc
created_at2023-03-24 17:59:21.846198
updated_at2023-11-26 14:03:06.740459
descriptionZero dependency command line argument parser
homepagehttps://gitlab.com/hsn10/getopt3
repositoryhttps://gitlab.com/hsn10/getopt3.git
max_upload_size
id819587
size28,901
Radim Kolar (hsn10)

documentation

README

getopt3 - cli parser with GNU extension for Rust

Version 2.0.0 MIT Licensed

License: MIT Crates.io dependency status Documentation Downloads Gitlab pipeline status

Features

  1. Rust ported well tested Scala code
  2. GNU argument parsing rules. Options can be anywhere in command line before --
  3. GNU -- extension. Everything after -- is not treated as options
  4. Multiple options not requiring argument can be grouped together. -abc is the same as -a -b -c
  5. Argument does not require space. -wfile is same as -w file
  6. Zero dependencies
  7. 41 unit tests + 7 integration tests + 1 doc test

Usage

Create getopt instance

let g = getopt3::new(arguments, optstring)

getopt3::new constructor arguments:

  1. arguments command line arguments as Vec<String>
  2. optstring is a &str containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument.

Check for results

getopt structure returned by constructor has following members:

  1. arguments : Vec <String> command line arguments with options removed
  2. options_map : HashMap <char, bool> map of recognized options. option -> have_argument
  3. options : HashMap <char, String> options parsed. If option do not have argument, it is mapped to "" String, otherwise it is mapped to its argument as string.

Optional - Check if options are parsed correctly

You can run strictness check by calling validate(getopt) function. Returns Result with getopt instance or error as String.

Example usage

  use std::env::args;
  let rc = getopt3::new(args(), "ab:c");
  if let Ok(g) = rc {
     // command line options parsed sucessfully
     if let Some(arg) = g.options.get(&'b') {
        // handle b argument stored in arg
     };
  };
Commit count: 89

cargo fmt