-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
71 lines (60 loc) · 2.18 KB
/
example.c
File metadata and controls
71 lines (60 loc) · 2.18 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "flagparser.h"
#include "stdio.h"
int main(int argc, const char *argv[])
{
/* Example of an integer */
int *amount = flg_int_arg(
"-a", /* short flag format */
"--amount", /* long flag format */
1, /* default value */
"Specify the amount" /* Help string */
);
/* Example of a boolean argument, also called a switch
* The defaul value of switches is always false */
bool *keep = flg_bool_arg(
"-k", /* short flag format */
"--keep", /* long flag format */
"Keep temp files" /* help string */
);
/* Example a string argument */
char **dns = flg_string_arg(
"-d", /* short flag format */
"--dns", /* long flag format */
"1.1.1.1", /* default value */
"Specify the target DNS server" /* help string */
);
/* You can optionally define a 'rest collection'.
* This will help the documentation string to name what you
* expect to be present after the [OPTIONS]. */
flg_define_rest_collection(
"FILE", /* Name of the collection.
* ./example [OPTIONS] [FILE]... */
0, /* minimum amount of args required */
"Files to process" /* Help string */
);
/* flg_parse_flags will populate the pointers
* It will also return an offset you can use to know when the
* [OPTIONS] and where the 'rest collection' starts. */
int offset = flg_parse_flags(argc, argv);
/* Optional demonstration on how to deal with the non-flag strings
* arguments. The _offset_ variable allows us to iterate over them.
*/
int i;
for (i = offset; i < argc; i++)
{
printf("Rest ARG: %i \"%s\"\n", i - offset, argv[i]);
}
/* Demonstration on flag usage */
if (*keep)
puts("Keep is enabled");
else
puts("Keep is not enabled");
printf("DNS server: \"%s\"\n", *dns);
printf("Amount: %d\n", *amount);
/* Clean-up */
free(amount);
free(keep);
free(*dns);
free(dns);
return 0;
}