-
Notifications
You must be signed in to change notification settings - Fork 7
/
dropt_example.c
171 lines (154 loc) · 5.34 KB
/
dropt_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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/** dropt_example.c
*
* A simple dropt example.
*
* Written by James D. Lin and assigned to the public domain.
*
* The latest version of this file can be downloaded from:
* <http://www.taenarum.com/software/dropt/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "dropt.h"
/* dropt is not limited to built-in types. Let's use it with a custom type. */
typedef enum { unknown, heads, tails } face_type;
/* Function prototype for our custom function to parse a string to a
* `face_type`.
*/
static dropt_option_handler_decl handle_face;
int
main(int argc, char** argv)
{
/* The `static` keyword is used here for compatibility with strict
* C89 compilers.
*
* (C89 requires that array initializers be constant, so they need to
* have static storage duration if their addresses are to be used as
* initialization values. This restriction is relaxed in C99, and many
* compilers ignored it anyway.)
*/
static dropt_bool showHelp = 0;
static dropt_bool showVersion = 0;
static int i = 0;
static face_type face = unknown;
int exitCode = EXIT_SUCCESS;
/* Each option is defined by a row in a table, containing properties
* such as the option's short name (e.g. -h), its long name (e.g.
* --help), its help text, its handler callback, and its callback data
* (for typical handlers, this data is usually the address of a variable
* for the handler to modify).
*
* See the dropt_option documentation in dropt.h for a complete list
* of option properties.
*/
dropt_option options[] = {
{ 'h', "help", "Shows help.", NULL, dropt_handle_bool, &showHelp, dropt_attr_halt },
{ '?', NULL, NULL, NULL, dropt_handle_bool, &showHelp, dropt_attr_halt | dropt_attr_hidden },
{ '\0', "version", "Shows version information.", NULL, dropt_handle_bool, &showVersion, dropt_attr_halt },
{ 'i', "int", "Sample integer option.", "value", dropt_handle_int, &i },
{ 'f', "face", "Sample custom option.", "{heads, tails}", handle_face, &face },
{ 0 } /* Required sentinel value. */
};
dropt_context* droptContext = dropt_new_context(options);
if (droptContext == NULL)
{
/* We failed to create the dropt context, possibly due to memory
* allocation failure.
*
* This also can happen due to logical errors (e.g. if the options
* array is malformed). Logical errors will trigger `DROPT_MISUSE()`
* and will terminate the program in debug builds.
*/
exitCode = EXIT_FAILURE;
}
else if (argc == 0)
{
/* This check is here only for pedantic completeness. Hosted C
* environments are not required to supply command-line arguments,
* although obviously any environment that doesn't supply arguments
* wouldn't have any use for dropt.
*/
}
else
{
/* Parse the arguments from `argv`.
*
* `argv[1]` is always safe to access since we've established that
* `argc` > 0 and since `argv[argc]` is guaranteed to be a null
* pointer.
*/
char** rest = dropt_parse(droptContext, -1, &argv[1]);
if (dropt_get_error(droptContext) != dropt_error_none)
{
fprintf(stderr, "dropt_example: %s\n", dropt_get_error_message(droptContext));
exitCode = EXIT_FAILURE;
}
else if (showHelp)
{
printf("Usage: dropt_example [options] [--] [operands]\n\n"
"Options:\n");
dropt_print_help(stdout, droptContext, NULL);
}
else if (showVersion)
{
printf("dropt_example 1.0\n");
}
else
{
printf("int value: %d\n", i);
printf("face value: %d\n", face);
printf("Operands: ");
while (*rest != NULL)
{
printf("%s ", *rest);
rest++;
}
printf("\n");
}
}
dropt_free_context(droptContext);
return exitCode;
}
/** handle_face
*
* Usually the stock callbacks (e.g. `dropt_handle_bool`,
* `dropt_handle_int`, `dropt_handle_string`, etc.) should be sufficient
* for most purposes, but this is an example of an option handler for a
* custom type.
*
* For more information, see the comments to `dropt_option_handler_decl`.
*/
static dropt_error
handle_face(dropt_context* context,
const dropt_option* option,
const char* optionArgument,
void* dest)
{
dropt_error err = dropt_error_none;
face_type* face = dest;
assert(face != NULL);
/* Option handlers should handle `optionArgument` being `NULL` (if the
* option's argument is optional and wasn't supplied) or being the empty
* string (if a user explicitly passed an empty string (e.g. --face="").
*/
if (optionArgument == NULL || optionArgument[0] == '\0')
{
err = dropt_error_insufficient_arguments;
}
else if (strcmp(optionArgument, "heads") == 0)
{
*face = heads;
}
else if (strcmp(optionArgument, "tails") == 0)
{
*face = tails;
}
else
{
/* Reject the value as being inappropriate for this handler. */
err = dropt_error_mismatch;
}
return err;
}