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
- Supports MS-DOS formatted option strings (e.g.
/option:arg
) - Supports permuting command-line arguments to shift all non-option arguments as appropriate
- Supports first-longest-matching for options
- Supports the BSD
optreset
functionality - 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
Supported Systems
Known to work on Linux, {Net,Free}BSD, and Windows. Should work on any system (bug reports welcome).
Dependencies
- Depends on the
strcasecmp
andstrncasecmp
functions. For systems which provide_strnicmp
defineHAVE__STRICMP
andHAVE__STRNICMP
and these functions will be used as replacements. Otherwise, replacements will need to be included into the build system for projects UltraGetopt being targeted at systems lacking them.
Installing
To build into an existing project
- Include ultragetopt.c in the build system for your project
- 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
- Run
./configure
- Run
make
- Run
make install
with appropriate privelages
To create a library for UltraGetopt with Visual Studio
- Open ultragetopt.sln in Visual Studio
- Set the type to Release Static or Release Shared as appropriate
- 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
- The option permuting done by UltraGetopt differs from other getopt
implementations that permute arguments on the next call after they were
returned. UltraGetopt currently moves an option forward and then returns
it. Therefore, its values of
optind
will necessarily differ from other implementations, althoughargv[optind]
will be the same.
Planned Features
- More testing for incompatibilities with existing getopt implementations
- More features, as they are thought up