-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.c
41 lines (37 loc) · 1.06 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdlib.h>
#include <stdio.h>
#include "opthandler.h"
enum options_order {
o_OUTPUT_FILE,
o_VERBOSE,
o_NO_COLORS,
};
struct opthandler_option options[] = {
[o_VERBOSE] = {
"enable verbose output",
'v', "verbose", NULL, arg_flag},
[o_OUTPUT_FILE] = {
"set the output file",
'o', "output-file", "filename", arg_default("-")},
[o_NO_COLORS] = {
"disable colored output in console",
'\0', "no-colors", NULL, arg_flag},
};
#define array_count(a) ( sizeof(a) / sizeof(*(a)) )
int main (int argc, char * argv[])
{
opthandler_init(array_count(options), options, "Example program");
opthandler_handle_opts(&argc, &argv);
printf("Remaining %d args: '", argc);
for (size_t i = 0; argv[i]; ++i)
printf("%s%s", (i ? " " : ""), argv[i]);
printf ("'\n"
"output-file: '%s'\n"
"verbose = %s\n"
"colors = %s\n",
options[o_OUTPUT_FILE].value.string,
options[o_VERBOSE].value.flag ? "yes" : "no",
options[o_NO_COLORS].value.flag ? "no" : "yes");
opthandler_free();
return 0;
}