2
// gcc -o ecore_getopt_example ecore_getopt_example.c `pkg-config --libs --cflags ecore eina`
5
#include <Ecore_Getopt.h>
8
/* if defined will end the positional arguments with the special
9
* action ECORE_GETOPT_ACTION_APPEND that will require at least one
10
* trailing argument and will also consume the remaining arguments
13
* if not defined unhandled positional arguments start at the index
14
* returned by ecore_getopt_parse_positional(), that will be less or
17
#define END_WITH_POS_APPEND 1
19
static const char * available_choices[] = {
23
NULL /* must be null terminated! */
26
static const Ecore_Getopt options = {
27
/* program name, usually a macro PACKAGE_NAME */
28
"ecore_getopt_example",
29
/* usage line, leave empty to generate one with positional arguments */
31
/* program version, usually a macro PACKAGE_VERSION */
33
/* copyright string */
34
"(C) 2013 Enlightenment Project",
37
/* long description, may be multiline and contain \n */
38
"Example of Ecore_Getopt usage.\n"
40
"This usage may span over multiple lines of text, with automatic line "
41
"break based on $COLUMNS environment variable that is usually defined by "
43
"You can have %%prog (expands to \"%prog\") or %%version (expands to \""
44
"%version\") in the description to get the program name "
45
"or version. Use double %% to get the percentage symbol.\n"
46
"OneCanHaveVeryLongWorksInDescriptionLinesSuchAsThisOneAndItWillBeBrokenWhenTheyGoPast${COLUMNS}Characters.\n"
47
"\tTab (\\t) is supported, like in the beginning of this line. They "
48
"will work as tabulation to columns multiple of 8 spaces, so you can do "
50
"1\tsomething\tsome description\n"
51
"23\totherthing\tsome description\n"
52
"456\tyetanother\tsome description\n"
53
"12345678\tthis is off\tthis is off\n",
54
/* we want strict parsing (any error aborts) */
56
/* an array of argument descriptions (must terminate with sentinel) */
59
/* block of options that store a single value in a variable of type */
60
ECORE_GETOPT_STORE_STR(0, "string", "Store a single string."),
61
ECORE_GETOPT_STORE_BOOL(0, "bool", "Store a single boolean."),
62
ECORE_GETOPT_STORE_SHORT(0, "short", "Store a single short."),
63
ECORE_GETOPT_STORE_INT(0, "int", "Store a single integer."),
64
ECORE_GETOPT_STORE_LONG(0, "long", "Store a single long integer."),
65
ECORE_GETOPT_STORE_USHORT(0, "unsigned-short",
66
"Store a single unsigned short integer."),
67
ECORE_GETOPT_STORE_UINT(0, "unsigned-int",
68
"Store a single unsigned integer."),
69
ECORE_GETOPT_STORE_ULONG(0, "unsigned-long",
70
"Store a single unsigned long integer."),
71
ECORE_GETOPT_STORE_DOUBLE(0, "double", "Store a single double."),
73
/* block of options that store a single value in a variable of type
74
* and use a default value if option IS SPECIFIED but no value is given
76
* If option -o has default value of X, then the command lines produce:
77
* <empty>: nothing is set.
78
* -o: value is set to X.
79
* -o=Y: value is set to Y.
81
ECORE_GETOPT_STORE_DEF_STR(0, "default-string", "Store a single string.",
82
"default-string-value"),
83
ECORE_GETOPT_STORE_DEF_BOOL(0, "default-bool", "Store a single boolean.",
85
ECORE_GETOPT_STORE_DEF_SHORT(0, "default-short", "Store a single short.",
87
ECORE_GETOPT_STORE_DEF_INT(0, "default-int", "Store a single integer.",
89
ECORE_GETOPT_STORE_DEF_LONG(0, "default-long",
90
"Store a single long integer.", 12345),
91
ECORE_GETOPT_STORE_DEF_USHORT(0, "default-unsigned-short",
92
"Store a single unsigned short integer.",
94
ECORE_GETOPT_STORE_DEF_UINT(0, "default-unsigned-int",
95
"Store a single unsigned integer.",
97
ECORE_GETOPT_STORE_DEF_ULONG(0, "default-unsigned-long",
98
"Store a single unsigned long integer.",
100
ECORE_GETOPT_STORE_DEF_DOUBLE(0, "default-double",
101
"Store a single double.",
104
/* you can specify the metavar so the --help will be more meaningful */
105
ECORE_GETOPT_STORE_METAVAR_STR(0, "output", "Specify output file.",
108
/* Other than storing a given value (or default), it is common to
109
* have boolean options (ie: --debug, --daemon), those that set a
110
* constant value, counters (ie: --verbose) or option from a fixed
113
ECORE_GETOPT_STORE_TRUE(0, "enable-x", "Enables X."),
114
ECORE_GETOPT_STORE_FALSE(0, "disable-y", "Disables Y."),
115
ECORE_GETOPT_STORE_CONST(0, "set-z", "Set z to constant XPTO.", "XPTO"),
116
ECORE_GETOPT_COUNT(0, "countme",
117
"Counts number of times this option is given."),
118
ECORE_GETOPT_CHOICE(0, "choose", "Choose from one of the options",
121
/* one can create a list of given values of a certain type */
122
ECORE_GETOPT_APPEND(0, "append-string", "Store multiple strings.",
123
ECORE_GETOPT_TYPE_STR),
124
ECORE_GETOPT_APPEND(0, "append-int", "Store multiple integers.",
125
ECORE_GETOPT_TYPE_INT),
127
/* break options will force everything that goes after it to be ignored
128
* by the option parser and they will go as arguments. This is the case
129
* for xterm's -e. Example:
130
* program --string=A: stores "A" into str_value.
131
* program --string=A --break: still stores "A" into str_value.
132
* program --break --string=A: str_value is untouched, --string=a
133
* is avaiable in argv[args], with
134
* args being the index returned by
135
* ecore_getopt_parse()
137
* Note that ecore_getopt will follow GNU and stop parsing arguments
138
* once -- is found, similar to "rm -- -fr /". In this case the
139
* return of ecore_getopt_parse() is to the index of "--" element in
142
ECORE_GETOPT_BREAK(0, "break"),
144
/* standard block to provide version, copyright, license and help */
145
ECORE_GETOPT_VERSION('V', "version"),
146
ECORE_GETOPT_COPYRIGHT('C', "copyright"),
147
ECORE_GETOPT_LICENSE('L', "license"),
148
ECORE_GETOPT_HELP('h', "help"),
150
/* positional arguments can be handled as well, add their
151
* description after the last option was specified. They should
152
* have empty short and long options, but have the metavar
155
ECORE_GETOPT_STORE_METAVAR_STR(0, NULL, "Positional string.", "STRING"),
156
ECORE_GETOPT_STORE_METAVAR_INT(0, NULL, "Positional integer.", "INT"),
157
ECORE_GETOPT_CHOICE_METAVAR(0, NULL, "Positional choice.", "CHOICE",
160
#ifdef END_WITH_POS_APPEND
161
/* this will consume until the end of the command line, forcing
162
* ecore_getopt_parse() to return args == argc on succes.
163
* It will require at least one argument in the end of the command line.
165
ECORE_GETOPT_APPEND_METAVAR(0, NULL, "Extra options.", "ARG",
166
ECORE_GETOPT_TYPE_STR),
169
/* the sentinel is required to notify end of descriptions */
170
ECORE_GETOPT_SENTINEL
175
main(int argc, char **argv)
177
char *str_value = NULL;
178
Eina_Bool bool_value = EINA_FALSE;
179
short short_value = 0;
182
unsigned short ushort_value = 0;
183
unsigned int uint_value = 0;
184
unsigned long ulong_value = 0;
185
double double_value = 0;
186
char *def_str_value = NULL;
187
Eina_Bool def_bool_value = EINA_FALSE;
188
short def_short_value = 0;
189
int def_int_value = 0;
190
long def_long_value = 0;
191
unsigned short def_ushort_value = 0;
192
unsigned int def_uint_value = 0;
193
unsigned long def_ulong_value = 0;
194
double def_double_value = 0;
195
char *output_value = NULL;
196
Eina_Bool use_x = EINA_FALSE; /* if stores true, then start with false */
197
Eina_Bool use_y = EINA_TRUE; /* if stores false, then start with true */
198
char *use_z = NULL; /* stores a pointer here */
201
Eina_List *lst_strs = NULL;
202
Eina_List *lst_ints = NULL;
203
Eina_Bool break_given = EINA_FALSE;
204
Eina_Bool quit_option = EINA_FALSE;
205
char *pos_str = NULL;
207
char *pos_choice = NULL;
208
#ifdef END_WITH_POS_APPEND
209
Eina_List *pos_args = NULL;
211
Ecore_Getopt_Value values[] = {
212
/* block of options that store a single value in a variable of type */
213
ECORE_GETOPT_VALUE_STR(str_value),
214
ECORE_GETOPT_VALUE_BOOL(bool_value),
215
ECORE_GETOPT_VALUE_SHORT(short_value),
216
ECORE_GETOPT_VALUE_INT(int_value),
217
ECORE_GETOPT_VALUE_LONG(long_value),
218
ECORE_GETOPT_VALUE_USHORT(ushort_value),
219
ECORE_GETOPT_VALUE_UINT(uint_value),
220
ECORE_GETOPT_VALUE_ULONG(ulong_value),
221
ECORE_GETOPT_VALUE_DOUBLE(double_value),
223
/* you can use options with default value (if =VALUE is omitted) */
224
ECORE_GETOPT_VALUE_STR(def_str_value),
225
ECORE_GETOPT_VALUE_BOOL(def_bool_value),
226
ECORE_GETOPT_VALUE_SHORT(def_short_value),
227
ECORE_GETOPT_VALUE_INT(def_int_value),
228
ECORE_GETOPT_VALUE_LONG(def_long_value),
229
ECORE_GETOPT_VALUE_USHORT(def_ushort_value),
230
ECORE_GETOPT_VALUE_UINT(def_uint_value),
231
ECORE_GETOPT_VALUE_ULONG(def_ulong_value),
232
ECORE_GETOPT_VALUE_DOUBLE(def_double_value),
234
/* example of metavar usage */
235
ECORE_GETOPT_VALUE_STR(output_value),
237
/* example of store true, false, const */
238
ECORE_GETOPT_VALUE_BOOL(use_x),
239
ECORE_GETOPT_VALUE_BOOL(use_y),
240
ECORE_GETOPT_VALUE_STR(use_z),
241
ECORE_GETOPT_VALUE_INT(count),
242
ECORE_GETOPT_VALUE_STR(choice),
244
/* example of append multiple options */
245
ECORE_GETOPT_VALUE_LIST(lst_strs),
246
ECORE_GETOPT_VALUE_LIST(lst_ints),
248
/* example of break option */
249
ECORE_GETOPT_VALUE_BOOL(break_given),
251
/* standard block to provide version, copyright, license and help */
252
ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */
253
ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */
254
ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
255
ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */
257
/* example of positiona argument */
258
ECORE_GETOPT_VALUE_STR(pos_str),
259
ECORE_GETOPT_VALUE_INT(pos_int),
260
ECORE_GETOPT_VALUE_STR(pos_choice),
262
#ifdef END_WITH_POS_APPEND
263
ECORE_GETOPT_VALUE_LIST(pos_args),
266
ECORE_GETOPT_VALUE_NONE /* sentinel */
268
int args, retval = EXIT_SUCCESS;
272
/* during development is recommended to use the following to check
273
* for duplicated options.
275
assert(ecore_getopt_parser_has_duplicates(&options) == EINA_FALSE);
277
args = ecore_getopt_parse(&options, values, argc, argv);
280
fputs("ERROR: Could not parse command line options.\n", stderr);
281
retval = EXIT_FAILURE;
285
/* options that set 'quit_option' to true requires us to exit. */
286
if (quit_option) goto end;
288
args = ecore_getopt_parse_positional(&options, values, argc, argv, args);
291
fputs("ERROR: Could not parse positional arguments.\n", stderr);
292
retval = EXIT_FAILURE;
296
printf("given values:\n"
302
"unsigned-short = %hu\n"
303
"unsigned-int = %u\n"
304
"unsigned-long = %lu\n"
307
"default-string = %s\n"
308
"default-bool = %s\n"
309
"default-short = %hd\n"
311
"default-long = %ld\n"
312
"default-unsigned-short = %hu\n"
313
"default-unsigned-int = %u\n"
314
"default-unsigned-long = %lu\n"
315
"default-double = %f\n"
318
"use-x = %s (disabled by default)\n"
319
"use-y = %s (enabled by default)\n"
321
"counted = %d --countme\n"
325
"\nDeclared Positional:\n"
331
bool_value ? "true" : "false",
340
def_bool_value ? "true" : "false",
349
use_x ? "enabled" : "disabled",
350
use_y ? "enabled" : "disabled",
354
break_given ? "given" : "omitted",
360
puts("no --append-string=VALUE was given.");
364
printf("%u strings given with --append-string=VALUE:\n",
365
eina_list_count(lst_strs));
366
EINA_LIST_FREE(lst_strs, str)
368
printf("\t%s\n", str);
374
puts("no --append-int=VALUE was given.");
378
printf("%u integers given with --append-int=VALUE:\n",
379
eina_list_count(lst_ints));
380
EINA_LIST_FREE(lst_ints, pi)
382
printf("\t%d\n", *pi);
387
#ifdef END_WITH_POS_APPEND
388
assert(pos_args != NULL);
389
assert(args == argc);
393
printf("%u extra arguments:\n",
394
eina_list_count(pos_args));
395
EINA_LIST_FREE(pos_args, str)
397
printf("\t%s\n", str);
403
puts("no extra positional arguments were given.");
406
printf("%d positional arguments were given:\n", argc - args);
407
for (; args < argc; args++)
408
printf("\t%s\n", argv[args]);