new

Function new 

Source
pub fn new(
    arg: impl IntoIterator<Item = impl AsRef<str>>,
    optstring: impl AsRef<str>,
) -> Result<getopt>
Expand description

Parse command line arguments.

Parses command line arguments using GNU getopt(3) parsing rules with double dash “–” support. Long arguments starting with double dash “–” are not supported.

Parsing is done in non-strict and non-POSIX mode. Call validate on result to detect missing arguments and unknown options.

POSIX parsing mode is not yet supported.

§Arguments

  • arg - String Iterator with command line arguments. Can be empty.

  • optstring - List of legitimate alphanumeric plus ‘?’ option characters. If character is followed by colon, the option requires an argument. Must not be empty.

§Parsing rules

  1. GNU argument parsing rules. This means that options can be anywhere on the command line before –
  2. Double dash – support. Everything after – is not treated as options.
  3. Long options are not supported.
  4. Multiple options not requiring argument can be chained together. -abc is the same as -a -b -c
  5. Last chained option can have an argument. -abcpasta is the same as -a -b -c pasta
  6. Argument does not require space. -wfile is the same as -w file
  7. optional argument :: GNU optstring extension is not implemented.
  8. Strict POSIX parse mode where first non option stops option parsing is not supported. This mode is triggered in GNU getopt by setting POSIXLY_CORRECT variable or by optstring starting with a plus sign +.
  9. The POSIX-specified extension for the getopt function, which allows the optstring to start with a colon (:), is always supported. This extension enables the use of the ‘?’ character as a command-line option. We always allow use of the ‘?’ as option without need to manually activate it using optstring. Starting optstring with ‘:’ is possible and supported as valid syntax.

§Errors

  1. Parsing error only happens if optstring parameter is invalid or empty.
  2. If a required argument is missing function new() still returns succesfully and the missing argument will be replaced by an empty String.

§See also

  1. GNU libc getopt function.
  2. POSIX getopt function.