getopt3 - command line argument parser for Rust
Version 2.6.0 MIT Licensed
Features
- GNU argument parsing rules. Options can be anywhere on the command line before --
- Double dash -- support. Anything after -- is not treated as options.
- Multiple options not requiring argument can be chained together. -abc is the same as -a -b -c
- Last chained option can have an argument. -abcpasta is the same as -a -b -c pasta
- Argument does not require space. -wfile is the same as -w file
- Parsing fails only if optstring is invalid.
Code quality
- MSRV 1.56, compatible with the entire Rust 2021 Edition.
- Rust port of the well tested Scala getopt code.
- Small code footprint: only 226 LOC.
- Zero dependencies.
- No
unsafeRust. - Runs on entire stable 2021 Edition Rust.
- 69 unit tests, 7 integration tests, and 8 doc tests.
Usage
1. Create getopt instance
let g = getopt3::new(arguments, optstring)
getopt3::new constructor arguments:
- arguments command line arguments. Can be anything that can be converted to
Iterator over String. You can use
std::env::args()but you need to skip the first argument because it's the executable name. It can be done manually by .skip(1) or by callinghideBinutility function which strips the first argument. - optstring is anything providing
AsRef <str>. optstring is containing the legitimate option characters. Valid option characters are alphanumeric, plus '?'. If such a character is followed by a colon, the option requires an argument.
Returned value:
- Function returns
Result <getopt>. Resultwraps parsing errors and getopt structure.- Parsing can fail only if optstring is invalid.
- If required argument is missing function new() won't fail. Call validate() on parsed result for optional argument checking.
2. Check parsed options
getopt structure returned by constructor has following public members:
- arguments :
Vec <String>command line arguments with options removed - options_map :
HashMap <char, bool>map of recognized options. option -> has_argument - options :
HashMap <char, String>options parsed. If option does not have an argument, it is mapped to an empty ""String, otherwise it is mapped to its argument asString.
Structure getopt supports IntoIterator and can transform itself into Iterator
over supplied command line arguments.
It supports consuming itself or use self immutable reference for enumerating arguments.
getopt.iter() returns Iterator command line arguments without consuming itself.
It is a convenience shortcut for getopt.arguments.iter().
getopt.len() returns number of command line arguments. It is a convenience shortcut for getopt.arguments.len().
getopt.get() returns value of command line option. It is a convenience shortcut for getopt.options.get().
getopt.has() returns if option got supplied on command line. It is a convenience shortcut for getopt.options.contains_key().
getopt[usize] returns nth command line argument. It is a convenience shortcut for getopt.arguments.index().
getopt.is_empty() returns if any arguments were supplied on command line. It is a convenience shortcut for getopt.arguments.is_empty().
3. Optional - Check if correct options were provided
You can run strict parse checks by calling validate(getopt) function.
This function returns back Result with supplied getopt instance on success or
error as String. It can detect if unknown options were encountered or
required arguments are missing.
Example
use args;
use hideBin;
let rc = new;
if let Ok = rc ;
Reference
- POSIX getopt function.
- GNU libc getopt function.
- OpenBSD getopt
- FreeBSD getopt
Known getopt extensions
Fully implemented
- GNU getopt parsing rules.
- Options can be anywhere in command line before --
- double dash -- support. Anything after -- is not treated as options.
- POSIX extension in the getopt function where the argument string
specification can start with a colon (:). When the option string begins with
a colon, it modifies the behavior of getopt to handle error cases differently.
Specifically, it suppresses the default error messages that getopt prints
when it encounters an unrecognized option or a missing argument,
and it returns : instead of ? for these error cases. This allows using ? as
an option because it's possible to spot the difference between an unknown option and the -? option.
- The extension is implemented in a way that '?' as option is always supported. ':' at start of option string is fully optional and have no effect on activating this extension.
Not implemented
- Two colons in optstring indicates that the argument is optional. This is an extension not covered by POSIX. This will change only validate function because we do not report errors in getopt3::new if required argument is missing.
- In GNU getopt, if the option string (optstring) begins with a + character, it triggers POSIX-compatible parsing. This means that the first non-option argument will stop option parsing, similar to how setting the POSIXLY_CORRECT environment variable behaves.
Possible future developments
Some code for these features exists, but development is paused.
Partially implemented code is not compiled in during normal build and can't be enabled in stock crate using features.
no_stdRust version in development.posixstrict mode. First non option stops option parsing. This is needed for parsing nested command lines.