rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <stdlib.h>
|
nuclear@1
|
3 #include <optcfg.h>
|
nuclear@1
|
4
|
nuclear@1
|
5 enum {
|
nuclear@1
|
6 OPT_FOO,
|
nuclear@1
|
7 OPT_BAR,
|
nuclear@1
|
8 OPT_XYZZY,
|
nuclear@1
|
9 OPT_HELP
|
nuclear@1
|
10 };
|
nuclear@1
|
11
|
nuclear@1
|
12 struct optcfg_option options[] = {
|
nuclear@1
|
13 /* short, long, id, description */
|
nuclear@1
|
14 {'f', "foo", OPT_FOO, "foo does nothing really"},
|
nuclear@1
|
15 {'b', "bar", OPT_BAR, "bar also does nothing"},
|
nuclear@1
|
16 {0, "xyzzy", OPT_XYZZY, "xyzzy doesn't have a short option"},
|
nuclear@1
|
17 {'h', "help", OPT_HELP, "print usage and exit"},
|
nuclear@3
|
18 OPTCFG_OPTIONS_END
|
nuclear@1
|
19 };
|
nuclear@1
|
20
|
nuclear@1
|
21 int foo, bar;
|
nuclear@1
|
22 int xyzzy;
|
nuclear@1
|
23
|
nuclear@1
|
24 int opt_handler(struct optcfg *oc, int opt, void *cls);
|
nuclear@1
|
25
|
nuclear@1
|
26 int main(int argc, char **argv)
|
nuclear@1
|
27 {
|
nuclear@1
|
28 /* pass the options array to initialize the optcfg object */
|
nuclear@1
|
29 struct optcfg *oc = optcfg_init(options);
|
nuclear@1
|
30 /* set the option callback function */
|
nuclear@1
|
31 optcfg_set_opt_callback(oc, opt_handler, 0);
|
nuclear@1
|
32 /* first let's load options from a config file */
|
nuclear@1
|
33 printf("parsing config file: example.conf\n");
|
nuclear@1
|
34 if(optcfg_parse_config_file(oc, "example.conf") == -1) {
|
nuclear@1
|
35 return 1;
|
nuclear@1
|
36 }
|
nuclear@1
|
37 /* then let's override those with commandline options */
|
nuclear@1
|
38 printf("parsing commandline arguments\n");
|
nuclear@1
|
39 if(optcfg_parse_args(oc, argc, argv) == -1) {
|
nuclear@1
|
40 return 1;
|
nuclear@1
|
41 }
|
nuclear@1
|
42
|
nuclear@1
|
43 printf("\nfoo: %s\n", foo ? "true" : "false");
|
nuclear@1
|
44 printf("bar: %s\n", bar ? "true" : "false");
|
nuclear@1
|
45 printf("xyzzy: %d\n", xyzzy);
|
nuclear@1
|
46
|
nuclear@1
|
47 optcfg_destroy(oc);
|
nuclear@1
|
48 return 0;
|
nuclear@1
|
49 }
|
nuclear@1
|
50
|
nuclear@1
|
51 int opt_handler(struct optcfg *oc, int opt, void *cls)
|
nuclear@1
|
52 {
|
nuclear@1
|
53 char *val;
|
nuclear@1
|
54
|
nuclear@1
|
55 switch(opt) {
|
nuclear@1
|
56 case OPT_FOO:
|
nuclear@2
|
57 optcfg_enabled_value(oc, &foo);
|
nuclear@1
|
58 break;
|
nuclear@1
|
59 case OPT_BAR:
|
nuclear@2
|
60 optcfg_enabled_value(oc, &bar);
|
nuclear@1
|
61 break;
|
nuclear@1
|
62 case OPT_XYZZY:
|
nuclear@1
|
63 /* this option needs an integer value */
|
nuclear@1
|
64 if(!(val = optcfg_next_value(oc)) || optcfg_int_value(val, &xyzzy) == -1) {
|
nuclear@1
|
65 fprintf(stderr, "xyzzy must be followed by a number\n");
|
nuclear@1
|
66 return -1;
|
nuclear@1
|
67 }
|
nuclear@1
|
68 break;
|
nuclear@1
|
69 case OPT_HELP:
|
nuclear@1
|
70 /* print usage and exit */
|
nuclear@1
|
71 printf("Usage: example [options]\n");
|
nuclear@1
|
72 printf("Options:\n");
|
nuclear@1
|
73 optcfg_print_options(oc);
|
nuclear@1
|
74 exit(0);
|
nuclear@1
|
75 }
|
nuclear@1
|
76 return 0;
|
nuclear@1
|
77 }
|