An open-source entry in TopGit's GitHub warehouse: skeeto/optparse, 380 stars, C. Portable, reentrant, getopt-like option parser
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Optparse is a public domain, portable, reentrant, embeddable, getopt-like
option parser. As a single header file, it's trivially dropped into any
project. It supports POSIX getopt option strings, GNU-style long options,
argument permutation, and subcommand processing.
To get the implementation, define OPTPARSE_IMPLEMENTATION before
including optparse.h.
Optionally define OPTPARSE_API to control the API's visibility
and/or linkage (static, __attribute__, __declspec).
#define OPTPARSE_API static
#include "optparse.h"
Why not getopt()?
The POSIX getopt option parser has three fatal flaws. These flaws are
solved by Optparse.
The getopt parser state is stored entirely in global variables,
some of which are static and inaccessible. This means only one thread
can use getopt. It also means it's not possible to recursively parse
nested sub-arguments while in the middle of argument parsing. Optparse
fixes this by storing all state on a local struct.
The POSIX standard provides no way to properly reset the parser.
For portable code this means getopt is only good for one run, over one
argv with one option string. It also means subcommand options cannot
be reliably processed with getopt. Most implementations provide an
implementation-specific method to reset the parser, but this is not
portable. Optparse provides an optparse_arg() function for stepping
through non-option arguments, and parsing of options can continue
again at any time with a different option string. The Optparse struct
itself could be passed around to subcommand handlers for additional
subcommand option parsing. If a full parser reset is needed,
optparse_init() can be called again.
In getopt, error messages are printed to stderr. This can be
disabled with opterr, but the messages themselves are still
inaccessible. Optparse solves this by writing the error message to its
errmsg field, which can be printed to anywhere. The downside to
Optparse is that this error message will always be in English rather
than the current locale.
Permutation
By default, argv is permuted as it is parsed, moving non-option
arguments to the end of the array. This can be disabled by setting the
permute field to 0 after initialization.
Optparse's interface should be familiar with anyone accustomed to
getopt. It's nearly a drop-in replacement. The option string has the
same format and the parser struct fields have the same names as the
getopt global variables (optarg, optind, optopt).
The long option parser optparse_long() API is very similar to GNU's
getopt_long() and can serve as a portable, embedded replacement.
Optparse does not allocate memory. Furthermore, Optparse has no
dependencies, including libc itself, so it can be used in situations
where the standard C library cannot.
See optparse.h for full API documentation.
Example Usage
Here's a traditional getopt setup.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
int main(int argc, char **argv)
{
bool amend = false;
bool brief = false;
const char *color = "white";
int delay = 0;
int option;
while ((option = getopt(argc, argv, "abc:d::")) != -1) {
switch (option) {
case 'a':
amend = true;
break;
case 'b':
brief = true;
break;
case 'c':
color = optarg;
break;
case 'd':
delay = optarg ? atoi(optarg) : 1;
break;
case '?':
exit(EXIT_FAILURE);
}
}
/* Print remaining arguments. */
for (; optind < argc; optind++)
printf("%s\n", argv[optind]);
return 0;
}
Here's the same thing translated to Optparse.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "optparse.h"
int main(int argc, char **argv)
{
bool amend = false;
bool brief = false;
const char *color = "white";
int delay = 0;
char *arg;
int option;
struct optparse options;
optparse_init(&options, argv);
while ((option = optparse(&options, "abc:d::")) != -1) {
switch (option) {
case 'a':
amend = true;
break;
case 'b':
brief = true;
break;
case 'c':
color = options.optarg;
break;
case 'd':
delay = options.optarg ? atoi(options.optarg) : 1;
break;
case '?':
fprintf(stderr, "%s: %s\n", argv[0], options.errmsg);
exit(EXIT_FAILURE);
}
}
/* Print remaining arguments. */
while ((arg = optparse_arg(&options)))
printf("%s\n", arg);
return 0;
}
To parse subcommands, first parse options with permutation disabled. These
are the "global" options that come before the subcommand. Then parse the
remainder, optionally permuting, as a new option array.
See examples/subcommands.c for a complete, working example.
Yes — skeeto/optparse ships under the Unlicense license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/skeeto/optparse.
What is skeeto/optparse?
skeeto/optparse (skeeto/optparse) is a C project on GitHub. From the project's own README: Portable, reentrant, getopt-like option parser
Where do I read more about skeeto/optparse?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/skeeto/optparse is the definitive source.
Read full README in the tab above.
Still deciding about optparse?
One click hands the question to an AI along with this page — see what it says about optparse.