4 stable releases

2.0.1 Nov 26, 2023
2.0.0 Apr 4, 2023
1.2.1 Mar 27, 2023
1.2.0 Mar 24, 2023

#764 in Parser implementations

Download history 5/week @ 2024-02-26 10/week @ 2024-03-11 71/week @ 2024-04-01

81 downloads per month

MIT license

27KB
476 lines

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
     };
  };

No runtime deps