Skip to main content

Crate getopt2

Crate getopt2 

Source
Expand description

§getopt2 command line parser

§Main features

  1. Unknown / incomplete options are turned into positional arguments.
  2. GNU getopt flexible argument parsing rules. Options and positional arguments can be mixed.
  3. Double dash -- support for options / arguments separation.
  4. POSIX mode parsing when optstring starts with “+”. First non option stops options parsing, rest is parsed as positional arguments.
  5. Optional argument support “::”. Optional argument is separated by space.
  6. Allow use of “?” as option character. Always active, supported compatibility with getopt extension when optstring starts with “:”.
  7. Parsing is not strict. If optstring is correct, parsing never fails.

getopt2::new parses the command line elements and isolates arguments from options. It returns a getopt structure where you can query options and use isolated arguments.

A 2 characters long element that starts with - (and is not exactly --) is an option element. The character following the initial - is an option character.

A double dash -- can be used to indicate the end of options; any arguments following it are treated as positional arguments.

Option values are separated from option element with spaces.

§See also

Alternative getopt3 parser have almost identical API but with different feature set. Main difference is that getopt3 doesn’t turn unrecognised / incomplete options into positional arguments.

§Example
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(str) = g.options.get(&'b') {
      // handle b argument
      println!("option -b have {} argument", str);
   };
};
§Reference
  1. POSIX getopt function.
  2. GNU libc getopt function.
  3. FreeBSD getopt function.
  4. getopt3 parser crate.

Structs§

getopt
Parsed command line options.

Enums§

argument
Information if option have an argument.

Functions§

hideBin
Removes first element from the IntoIterator.
new
Parse command line arguments.
validate
Validate parsed options in strict mode.