download
概述
Optparse is a public domain, portable, reentrant, embeddable,
getopt-like option parser. It’s a single header file and can be
trivially dropped into any project. It supports POSIX getopt option
strings, GNU-style long options, argument permutation, and subcommand
processing.
使用
optparse_long替代getopt_long
example:
#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)
{
struct optparse_long longopts[] = {
{"amend", 'a', OPTPARSE_NONE},
{"brief", 'b', OPTPARSE_NONE},
{"color", 'c', OPTPARSE_REQUIRED},
{"delay", 'd', OPTPARSE_OPTIONAL},
{0}
};
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_long(&options, longopts, NULL)) != -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;
}
使用optparse替代getopt
example:
#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;
}