aboutsummaryrefslogtreecommitdiff
path: root/src/program-arguments.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 09:07:29 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-10 09:11:50 +0000
commit6a7a6c4d82d26043c3fdf87b1a086ab4513a4062 (patch)
treea0e3aa049e39f38ad32a5e0448d97d36840fb294 /src/program-arguments.h
downloadlab-03-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/program-arguments.h')
-rw-r--r--src/program-arguments.h89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/program-arguments.h b/src/program-arguments.h
new file mode 100644
index 0000000..dc87f4b
--- /dev/null
+++ b/src/program-arguments.h
@@ -0,0 +1,89 @@
1#ifndef INCLUDED_PROGRAM_ARGUMENTS_H
2#define INCLUDED_PROGRAM_ARGUMENTS_H
3
4void
5print_help (const char *program_name)
6{
7 fprintf (stderr, "Usage: %s [OPTION...]\n", program_name);
8 fprintf (stderr, "\n");
9 fprintf (stderr, "Options:\n");
10#define PROGRAM_ARGUMENT_BOOL( sym, s, l, h, ...) fprintf (stderr, " -%c, --%s %*s\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h);
11#define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s <unsigned> %*s (default: %u)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d);
12#define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) fprintf (stderr, " -%c, --%s <string> %*s (default: %s)\n", s, l, (int) (10 + sizeof (h) - sizeof (l)), h, d);
13 PROGRAM_ARGUMENTS
14#undef PROGRAM_ARGUMENT_BOOL
15#undef PROGRAM_ARGUMENT_UINT
16#undef PROGRAM_ARGUMENT_STRING
17}
18
19#define PROGRAM_ARGUMENT_BOOL( sym, ...) bool sym;
20#define PROGRAM_ARGUMENT_UINT( sym, ...) unsigned int sym;
21#define PROGRAM_ARGUMENT_STRING(sym, ...) const char *sym;
22struct program_arguments {
23 const char *program_name;
24 PROGRAM_ARGUMENTS
25};
26#undef PROGRAM_ARGUMENT_BOOL
27#undef PROGRAM_ARGUMENT_UINT
28#undef PROGRAM_ARGUMENT_STRING
29
30struct program_arguments
31parse_program_arguments (int argc, char *argv[])
32{
33 UNUSED (argc);
34#define PROGRAM_ARGUMENT_BOOL(...)
35#define PROGRAM_ARGUMENT_UINT( sym, s, l, h, d, ...) .sym = d,
36#define PROGRAM_ARGUMENT_STRING(sym, s, l, h, d, ...) .sym = d,
37 struct program_arguments pa = {
38 .program_name = *argv++,
39 PROGRAM_ARGUMENTS
40 };
41#undef PROGRAM_ARGUMENT_BOOL
42#undef PROGRAM_ARGUMENT_UINT
43#undef PROGRAM_ARGUMENT_STRING
44
45#define PROGRAM_ARGUMENT_BOOL(sym, s, l, ...) MATCH (s, l) { \
46 pa.sym = true; \
47 continue; \
48 }
49#define PROGRAM_ARGUMENT_UINT(sym, s, l, ...) MATCH (s, l) { \
50 arg += 1; \
51 if (*arg == NULL) { \
52 fprintf (stderr, "error"); \
53 exit (EXIT_FAILURE); \
54 } \
55 if (sscanf (*arg, "%u", &pa.sym) == 0) { \
56 fprintf (stderr, "error: wrong format for %s\n", arg[-1]); \
57 exit (EXIT_FAILURE); \
58 } \
59 continue; \
60 }
61#define PROGRAM_ARGUMENT_STRING(sym, s, l, ...) MATCH (s, l) { \
62 arg += 1; \
63 pa.sym = *arg; \
64 continue; \
65 }
66 for (char **arg = argv; *arg; ++arg) {
67 if ((*arg)[0] == '-') {
68 if ((*arg)[1] == '-') {
69#define MATCH(s, l) if (!strcmp (*arg + 2, l))
70 PROGRAM_ARGUMENTS;
71 fprintf (stderr, "error: unknown option %s\n", *arg);
72 exit (EXIT_FAILURE);
73#undef MATCH
74 }
75#define MATCH(s, l) if ((*arg)[1] == s && (*arg)[2] == '\0')
76 PROGRAM_ARGUMENTS;
77 fprintf (stderr, "error: unknown option %s\n", *arg);
78 exit (EXIT_FAILURE);
79#undef MATCH
80 }
81 }
82#undef PROGRAM_ARGUMENT_BOOL
83#undef PROGRAM_ARGUMENT_UINT
84#undef PROGRAM_ARGUMENT_STRING
85
86 return pa;
87}
88
89#endif // INCLUDED_PROGRAM_ARGUMENTS_H