-
Notifications
You must be signed in to change notification settings - Fork 0
/
vrgtest.c
98 lines (80 loc) · 3.7 KB
/
vrgtest.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
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
** (C) by Remo Dentato ([email protected])
**
** This software is distributed under the terms of the MIT license:
** https://opensource.org/licenses/MIT
*/
// Define VRGCLI in one (and only one) of the source files that include vrg.h
#define VRGCLI
#include "vrg.h"
int main(int argc, char *argv[])
{
// vrgcli() {...} encloses the actions to perform when flags and arguments are encountered specified
// It takes as argument an optional informative message on the program (version, copyright ....)
// that will be printed by vrgusage()
vrgcli("vrg test program (c) 2022 by me") {
// Each flag or argument is a string divided in two parts seprated by tab (\t).
// The first part is the flag or argument name, the second one is the description.
// The `vrgarg()` macro encloses the code that must be executed when a flag is found
// Within the code, the `vrgarg` variable holds a pointer to the argument itself
// (or the flag option).
// -h takes no op
vrgarg("-h\tPrint help") {
// This code will be executed if the flag is specified.
printf("You specified the '-h' flag with option '%s'\n",vrgarg);
// This `vrgusage` function will print the list of flags and arguments in the same
// order they are specified within `vrgcli` and then exit with an error.
// You can optionally specify an error message to print before the Usage using
// the same syntx as `printf()`.
vrgusage();
}
// -o takes an optional option. You can tell it's optional because it is
// enclosed in brackets: [...]
vrgarg("-o [optional]\tYou may or may not specify an option for '-o'") {
// The variables vrglen and vrgargarg will help you get the flag argument.
printf("You specified the '-o' flag with option '%s'\n",vrgarg);
}
// -m takes a mandatory argument. No brackets here, if the argument is missing
// an error will be printed.
vrgarg("-m mandatory\tYou must specify an argument for '-m'") {
// You can specify the flag as `-m PIPPO` or `-mPIPPO`
printf("You specified the '-m' flag with option '%s'\n",vrgarg);
}
vrgarg("--\tEnd of vrgcli managed arguments") {
// A simple break allows you to exit the cli handling
// If you exit this way, no check on mandatory arguments
// will be performed.
break;
}
// Positional arguments are usually defined after the flags but
// you can specify them in whichever order you prefer on the command line.
// This is the first positional order and it is mandatory
// If a mandatory argument is not specified on the command line,
// an error will be printed.
vrgarg("inputfile\tThe name of the file to process") {
printf("File to be processed: `%s`\n",vrgarg);
}
// This is the second positional order and it is optional
vrgarg("[outputfile]\tThe name of the output file") {
printf("File to be created: `%s`\n",vrgarg);
}
// If no argment is passed, this code will be executed if none of the
// previous ones are. This is useful to detect unexpected flags or
// multiple positional arguments at the end.
// By default, unknown flag are just ignored
vrgarg() {
if (vrgarg[0] == '-') {
// This will print the error message, the usage and will exit with an error
vrgusage("ERROR: unknown flag '-%c'\n",vrgarg[1]);
}
fprintf(stderr,"Additional argument: '%s'\n",vrgarg);
}
}
// The variable vrgargn will tell you if there are other arguments to be
// processed in the command line.
if (vrgargn < argc) {
printf("You have additional arguments:\n");
for (int k = vrgargn; k < argc; k++)
printf(" %-2d '%s'\n", k, argv[k]);
}
}