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
81 downloads per month
27KB
476 lines
getopt3 - cli parser with GNU extension for Rust
Version 2.0.0 MIT Licensed
Features
- Rust ported well tested Scala code
- GNU argument parsing rules. Options can be anywhere in command line before --
- GNU -- extension. Everything after -- is not treated as options
- Multiple options not requiring argument can be grouped together. -abc is the same as -a -b -c
- Argument does not require space. -wfile is same as -w file
- Zero dependencies
- 41 unit tests + 7 integration tests + 1 doc test
Usage
Create getopt instance
let g = getopt3::new(arguments, optstring)
getopt3::new constructor arguments:
- arguments command line arguments as Vec<String>
- 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:
- arguments : Vec <String> command line arguments with options removed
- options_map : HashMap <char, bool> map of recognized options. option -> have_argument
- 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
};
};