summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-12-17 21:06:53 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-12-17 21:06:53 -0200
commit0b86e5119f328b6fa979bcbff6391a6458c6efe8 (patch)
treefeeaf8fdd1b446a2699ae65050d258d4260ed41d /src/examples
parent1c1f9ea0e1a2c4014d5cb5ca0f749164dcd384e5 (diff)
getopt: add positional argument handling.
positional arguments must appear at the end of the description array (after the last option) and should have a metavar set and not have shortname or longname. Simple, elegant and fit :-) There is a new function to parse the positional arguments, ecore_getopt_parse_positional() because we may want to not try to parse them in the case of a quit-option such as --help, --license, --copyright, --version or some user-defined action. This avoids us producing errors of missing positional arguments when printing help and adds some flexibility as well. This should make Tasn happy :-)
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/ecore/ecore_getopt_example.c84
1 files changed, 80 insertions, 4 deletions
diff --git a/src/examples/ecore/ecore_getopt_example.c b/src/examples/ecore/ecore_getopt_example.c
index dc91f0c237..f299266e17 100644
--- a/src/examples/ecore/ecore_getopt_example.c
+++ b/src/examples/ecore/ecore_getopt_example.c
@@ -5,6 +5,17 @@
5#include <Ecore_Getopt.h> 5#include <Ecore_Getopt.h>
6#include <assert.h> 6#include <assert.h>
7 7
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
11 * until the end.
12 *
13 * if not defined unhandled positional arguments start at the index
14 * returned by ecore_getopt_parse_positional(), that will be less or
15 * equal to argc.
16 */
17#define END_WITH_POS_APPEND 1
18
8static const char * available_choices[] = { 19static const char * available_choices[] = {
9 "banana", 20 "banana",
10 "apple", 21 "apple",
@@ -15,8 +26,8 @@ static const char * available_choices[] = {
15static const Ecore_Getopt options = { 26static const Ecore_Getopt options = {
16 /* program name, usually a macro PACKAGE_NAME */ 27 /* program name, usually a macro PACKAGE_NAME */
17 "ecore_getopt_example", 28 "ecore_getopt_example",
18 /* usage line */ 29 /* usage line, leave empty to generate one with positional arguments */
19 "%prog [options]", 30 NULL,
20 /* program version, usually a macro PACKAGE_VERSION */ 31 /* program version, usually a macro PACKAGE_VERSION */
21 "0.1", 32 "0.1",
22 /* copyright string */ 33 /* copyright string */
@@ -136,6 +147,25 @@ static const Ecore_Getopt options = {
136 ECORE_GETOPT_LICENSE('L', "license"), 147 ECORE_GETOPT_LICENSE('L', "license"),
137 ECORE_GETOPT_HELP('h', "help"), 148 ECORE_GETOPT_HELP('h', "help"),
138 149
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
153 * defined.
154 */
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",
158 available_choices),
159
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.
164 */
165 ECORE_GETOPT_APPEND_METAVAR(0, NULL, "Extra options.", "ARG",
166 ECORE_GETOPT_TYPE_STR),
167#endif
168
139 /* the sentinel is required to notify end of descriptions */ 169 /* the sentinel is required to notify end of descriptions */
140 ECORE_GETOPT_SENTINEL 170 ECORE_GETOPT_SENTINEL
141 } 171 }
@@ -172,6 +202,12 @@ main(int argc, char **argv)
172 Eina_List *lst_ints = NULL; 202 Eina_List *lst_ints = NULL;
173 Eina_Bool break_given = EINA_FALSE; 203 Eina_Bool break_given = EINA_FALSE;
174 Eina_Bool quit_option = EINA_FALSE; 204 Eina_Bool quit_option = EINA_FALSE;
205 char *pos_str = NULL;
206 int pos_int = 0;
207 char *pos_choice = NULL;
208#ifdef END_WITH_POS_APPEND
209 Eina_List *pos_args = NULL;
210#endif
175 Ecore_Getopt_Value values[] = { 211 Ecore_Getopt_Value values[] = {
176 /* block of options that store a single value in a variable of type */ 212 /* block of options that store a single value in a variable of type */
177 ECORE_GETOPT_VALUE_STR(str_value), 213 ECORE_GETOPT_VALUE_STR(str_value),
@@ -218,6 +254,15 @@ main(int argc, char **argv)
218 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */ 254 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
219 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */ 255 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */
220 256
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),
261
262#ifdef END_WITH_POS_APPEND
263 ECORE_GETOPT_VALUE_LIST(pos_args),
264#endif
265
221 ECORE_GETOPT_VALUE_NONE /* sentinel */ 266 ECORE_GETOPT_VALUE_NONE /* sentinel */
222 }; 267 };
223 int args, retval = EXIT_SUCCESS; 268 int args, retval = EXIT_SUCCESS;
@@ -240,6 +285,14 @@ main(int argc, char **argv)
240 /* options that set 'quit_option' to true requires us to exit. */ 285 /* options that set 'quit_option' to true requires us to exit. */
241 if (quit_option) goto end; 286 if (quit_option) goto end;
242 287
288 args = ecore_getopt_parse_positional(&options, values, argc, argv, args);
289 if (args < 0)
290 {
291 fputs("ERROR: Could not parse positional arguments.\n", stderr);
292 retval = EXIT_FAILURE;
293 goto end;
294 }
295
243 printf("given values:\n" 296 printf("given values:\n"
244 "string = %s\n" 297 "string = %s\n"
245 "bool = %s\n" 298 "bool = %s\n"
@@ -269,6 +322,10 @@ main(int argc, char **argv)
269 "choice = %s\n" 322 "choice = %s\n"
270 "\n" 323 "\n"
271 "--break = %s\n" 324 "--break = %s\n"
325 "\nDeclared Positional:\n"
326 "STRING = %s\n"
327 "INT = %d\n"
328 "CHOICE = %s\n"
272 "\n", 329 "\n",
273 str_value, 330 str_value,
274 bool_value ? "true" : "false", 331 bool_value ? "true" : "false",
@@ -294,7 +351,10 @@ main(int argc, char **argv)
294 use_z, 351 use_z,
295 count, 352 count,
296 choice, 353 choice,
297 break_given ? "given" : "omitted"); 354 break_given ? "given" : "omitted",
355 pos_str,
356 pos_int,
357 pos_choice);
298 358
299 if (!lst_strs) 359 if (!lst_strs)
300 puts("no --append-string=VALUE was given."); 360 puts("no --append-string=VALUE was given.");
@@ -324,14 +384,30 @@ main(int argc, char **argv)
324 } 384 }
325 } 385 }
326 386
387#ifdef END_WITH_POS_APPEND
388 assert(pos_args != NULL);
389 assert(args == argc);
390 if (1)
391 {
392 char *str;
393 printf("%u extra arguments:\n",
394 eina_list_count(pos_args));
395 EINA_LIST_FREE(pos_args, str)
396 {
397 printf("\t%s\n", str);
398 free(str);
399 }
400 }
401#else
327 if (args == argc) 402 if (args == argc)
328 puts("no positional arguments were given."); 403 puts("no extra positional arguments were given.");
329 else 404 else
330 { 405 {
331 printf("%d positional arguments were given:\n", argc - args); 406 printf("%d positional arguments were given:\n", argc - args);
332 for (; args < argc; args++) 407 for (; args < argc; args++)
333 printf("\t%s\n", argv[args]); 408 printf("\t%s\n", argv[args]);
334 } 409 }
410#endif
335 411
336 end: 412 end:
337 ecore_shutdown(); 413 ecore_shutdown();