Crates.io | getopt2 |
lib.rs | getopt2 |
version | 0.1.0-alpha.1 |
created_at | 2025-09-17 00:48:32.000252+00 |
updated_at | 2025-09-17 00:48:32.000252+00 |
description | Zero dependency strict command line argument parser |
homepage | https://gitlab.com/hsn10/getopt2 |
repository | https://gitlab.com/hsn10/getopt2.git |
max_upload_size | |
id | 1842622 |
size | 62,198 |
Version 0.1.0 MIT Licensed
unsafe
Rust.let g = getopt2::new(arguments, optstring)
getopt2::new constructor arguments:
std::env::args()
but you need to skip first
argument because its executable name. It can be done manually or by calling hideBin
utility function which strips its first argument.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.Result <getopt>
.Result
wraps parsing errors and getopt structure.getopt structure returned by constructor has following public members:
Vec <String>
command line arguments with options removedHashMap <char, bool>
map of recognized options. option -> have_argumentHashMap <char, String>
options parsed. If option do not have argument, it is mapped to "" String
,
otherwise it is mapped to its argument as String
.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
over pub field arguments without consuming itself.
getopt.len() returns number of command line arguments. It's convience shortcut for getopt.arguments.len().
getopt.get() returns value of command line option. It's convience shortcut for getopt.options.get().
getopt.has() returns if option got supplied on command line. It's convience shortcut for getopt.options.contains_key().
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.
use std::env::args;
use getopt2::hideBin;
let rc = getopt2::new(hideBin(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
};
};
Some code for these features exists, but development is paused. Partially implemented code is not compiled during normal build and can't be enabled in stock crate using features.
posix
strict mode. First non option stops option parsing. This is needed for parsing nested command lines.