-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments_parser.c
85 lines (74 loc) · 1.89 KB
/
arguments_parser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* See: http://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html
*/
#include <argp.h>
#include <stdbool.h>
#include <stdlib.h>
#include "arguments_parser.h"
const char *argp_program_version = "Hashmap 0.0.1";
const char *argp_program_bug_address = "<[email protected]>";
static char doc[] = "A generic hashmap implementation in C.";
static char args_doc[] = "[FILENAME]...";
static struct argp_option options[] = {
{
"max-items", 'i',
"COUNT", 0,
"Max number of key-value pairs in the hashmapl. Defaults to 100000"
},
{
"max-slots", 's',
"COUNT", 0,
"Max number of key-value pairs that resolve to a slot. Defaults to 10"
},
{
"verbose", 'v',
0, 0,
"Produce verbose output"
},
{
"debug", 'd',
0, 0,
"Produce debug output"
},
{
0
}
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
case 'i':
arguments->max_items = arg ? atoi(arg) : 100000;
break;
case 's':
arguments->max_slots = arg ? atoi(arg) : 10;
break;
case 'v':
arguments->verbose = 1;
break;
case 'd':
arguments->debug = 1;
break;
case ARGP_KEY_ARG:
return 0;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
struct arguments parse_arguments(int argc, char *argv[])
{
struct arguments arguments;
arguments.debug = 0;
arguments.verbose = 0;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
if (arguments.verbose) {
printf(
"Max items: %d\nMax bucket size: %d\n",
arguments.max_items,
arguments.max_slots
);
}
return arguments;
}