Parse short options and remaining arguments
To parse command-line arguments, you first initialize a struct optparse with your argv array. The optparse_init function prepares the parser state. Note that the argv array must be writable and NULL-terminated.
After initialization, you can process short options by repeatedly calling the optparse function. It takes an option string (similar to getopt) and returns the parsed option character. When it has processed all options (arguments starting with -), it returns -1.
Once all options have been handled, you can retrieve the remaining positional arguments by calling optparse_arg. This function returns a pointer to the next argument string, or NULL when all positional arguments have been consumed.
The following example demonstrates initializing the parser, reading one short option -a, and then reading one positional argument.
#include <assert.h>
#include <string.h>
#include "optparse.h"
int main(void)
{
char *argv[] = {"program", "-a", "argument", NULL};
struct optparse options;
optparse_init(&options, argv);
assert(optparse(&options, "a") == 'a');
assert(optparse(&options, "a") == -1);
char *arg = optparse_arg(&options);
assert(arg != NULL && strcmp(arg, "argument") == 0);
assert(optparse_arg(&options) == NULL);
return 0;
}