Kevin Locke's Homepage



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.


  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.



Supported Systems

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



  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 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.


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

Behave like BSD getopt()
Behave like Darwin (Mac OS) getopt()
Behave like GNU getopt()
Behave like POSIX definition of getopt()

Error Message Options

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

Print error messages matching BSD getopt
Print error messages matching Darwin getopt
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).

Parse "-o value" as "value" rather than " value"
Note: Only applicable when argv[x] == "-o value", not for argv[x] == "-o" [x+1] == "value"
Do not support GNU "::" optional argument.
Note: Always supported in *_long*() functions.
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}().

Set optopt to this value by default on each call to getopt()
Accept -option -arg as -option with argument "-arg" rather than -option missing argument
Accept adjacent arguments to long options (e.g. --optionarg) based on first longest-match
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
Support -o=file syntax for short options
Accept separated optional arguments. Parse -o file as -o with argument file rather than -o without an argument and non-option argument "file"
Support - and -- options in ultragetopt*_dos() functions
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