-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
65 lines (54 loc) · 1.76 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define FLAG_IMPLEMENTATION
#include "flag.h"
void usage(FILE *stream)
{
fprintf(stream, "Usage: example [OPTIONS] FILE\n");
fprintf(stream, "Options:\n");
flag_print_options(stream);
printf("\n");
}
int main(int argc, char *argv[])
{
// name, short, default, description
bool *bool_flag = flag_bool("bool", "b", false, "This is a boolean flag of the form '--bool' or '-b'");
char **str_flag = flag_str ("str", "s", 0, "This is a string flag of the form '--str <str>' or '-s <str>'");
int64_t *int_flag = flag_int64("int", "i", 69, "This is a int flag of the form '--int <number>' or '-i <number>'");
bool *help = flag_bool("help", "h", false, "Show help menu.");
bool ok = flag_parse(argc, argv);
if (!ok) {
flag_print_error(stderr);
exit(1);
}
if (*help) {
usage(stdout);
exit(0);
}
// Read flag value directly by pointer
if (*bool_flag) {
printf("log: Got bool: '%s'\n", *bool_flag ? "true" : "false");
}
if (*str_flag) {
printf("log: Got string: '%s'\n", *str_flag);
}
// Get additional information about the flag
struct flag *int_flag_info = flag_info(int_flag);
if (int_flag_info->set_by_user) {
printf("log: argument to flag '%s' explicitly given by user: '%ld'\n", int_flag_info->name, *int_flag);
}
// Handle positional argument
if (flag_pargs_n() < 1) {
fprintf(stderr, "Error: Expected FILE argument.\n");
exit(1);
}
char *file_path = flag_pargs(0);
printf("log: positional FILE is %s\n", file_path);
for (int i = 1; i < flag_pargs_n(); i += 1) {
printf("log: positional argument %d is %s\n", i, flag_pargs(i));
}
putchar('\n');
return 0;
}