Kevin Locke's Homepage

UltraGetopt

Description

UltraGetopt is a versatile and customizable implementation of getopt() with support for many common extensions, MS-DOS formatted option strings, and much more. It can function as a drop-in replacement for getopt() on systems with or without existing vendor-provided implementations and also as a separate co-existing function.

Features

  1. Supports MS-DOS formatted option strings (e.g. /option:arg)
  2. Supports permuting command-line arguments to shift all non-option arguments as appropriate
  3. Supports first-longest-matching for options
  4. Supports the BSD optreset functionality
  5. Supports many runtime-configurable behaviors (described below)

Development Status

Actively being developed. UltraGetopt supports nearly all of the functionality that I am looking for in a getopt implementation, so development is mostly minor bug fixes, tweaks, and minor additions. If you have suggestions for a feature (or the desire to implement it), don't hesitate to let me know about it.

License

MIT

Supported Systems

Known to work on Linux, {Net,Free}BSD, and Windows. Should work on any system (bug reports welcome).

Dependencies

Installing

  1. Download it

To build into an existing project

  1. Include ultragetopt.c in the build system for your project
  2. Include ultragetopt.h after any vendor-provided getopt headers and define ULTRAGETOPT_REPLACE_GETOPT if you would like the ultragetopt*() functions to replace vendor-provided getopt*() functions.

To create a library for UltraGetopt with *nix build tools

  1. Run ./configure
  2. Run make
  3. Run make install with appropriate privelages

To create a library for UltraGetopt with Visual Studio

  1. Open ultragetopt.sln in Visual Studio
  2. Set the type to Release Static or Release Shared as appropriate
  3. Run Build Solution

Configuration

Configuration of UltraGetopt is accomplished by both compiletime defines when building ultragetopt.c and by runtime options passed to the ultragetopt_tunable() function. These options are documented below.

Meta-options

These options will define the options below to appropriate values to mimic the functionality of other existing getopt suites.

ULTRAGETOPT_LIKE_BSD
Behave like BSD getopt()
ULTRAGETOPT_LIKE_DARWIN
Behave like Darwin (Mac OS) getopt()
ULTRAGETOPT_LIKE_GNU
Behave like GNU getopt()
ULTRAGETOPT_LIKE_POSIX
Behave like POSIX definition of getopt()

Error Message Options

These options change the formatting of the error messages produced by ultragetopt.

ULTRAGETOPT_BSD_ERRORS
Print error messages matching BSD getopt
ULTRAGETOPT_DARWIN_ERRORS
Print error messages matching Darwin getopt
ULTRAGETOPT_GNU_ERRORS
Print error messages matching GNU getopt

Compiletime-only Behavior Options

These options change the default behavior of getopt() and do not have a corresponding runtime flag (although they may be affected by other arguments).

ULTRAGETOPT_ASSIGNSPACE
Parse "-o value" as "value" rather than " value"
Note: Only applicable when argv[x] == "-o value", not for argv[x] == "-o" [x+1] == "value"
ULTRAGETOPT_NO_OPTIONALARG
Do not support GNU "::" optional argument.
Note: Always supported in *_long*() functions.
ULTRAGETOPT_NO_OPTIONASSIGN
Do not support --option=value syntax

Runtime-selectable Options

These options can all be selected by passing their value as a flag to the ultragetopt_tunable() function, where ULTRAGETOPT_ is replaced by UGO_ for compactness of the source. Defining these values sets the default state of the flag when invoked from ultragetopt{_long,_dos}().

ULTRAGETOPT_DEFAULTOPTOPT
Set optopt to this value by default on each call to getopt()
ULTRAGETOPT_HYPHENARG
Accept -option -arg as -option with argument "-arg" rather than -option missing argument
ULTRAGETOPT_LONGOPTADJACENT
Accept adjacent arguments to long options (e.g. --optionarg) based on first longest-match
ULTRAGETOPT_OPTIONPERMUTE
Permute options, do not stop at first non-option. A leading '+' in shortopts or when the $POSIXLY_CORRECT environmental variable are set, permuting will be stopped @ runtime
ULTRAGETOPT_SHORTOPTASSIGN
Support -o=file syntax for short options
ULTRAGETOPT_SEPARATEDOPTIONAL
Accept separated optional arguments. Parse -o file as -o with argument file rather than -o without an argument and non-option argument "file"
ULTRAGETOPT_DOS_DASH
Support - and -- options in ultragetopt*_dos() functions
ULTRAGETOPT_NO_EATDASHDASH
Do not increment optind when argv[optind] is -- as required by SUS/POSIX (results in "--" being one of the non-option arguments)

Known Bugs

Planned Features