efl

Форк
0
/
ecore_getopt_example.c 
415 строк · 14.6 Кб
1
//Compile with:
2
// gcc -o ecore_getopt_example ecore_getopt_example.c `pkg-config --libs --cflags ecore eina`
3

4
#include <Ecore.h>
5
#include <Ecore_Getopt.h>
6
#include <assert.h>
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

19
static const char * available_choices[] = {
20
  "banana",
21
  "apple",
22
  "orange",
23
  NULL /* must be null terminated! */
24
};
25

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 */
30
  NULL,
31
  /* program version, usually a macro PACKAGE_VERSION */
32
  "0.1",
33
  /* copyright string */
34
  "(C) 2013 Enlightenment Project",
35
  /* license string */
36
  "BSD 2-Clause",
37
  /* long description, may be multiline and contain \n */
38
  "Example of Ecore_Getopt usage.\n"
39
  "\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 "
42
  "your shell.\n"
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 "
49
  "tables such as:\n"
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) */
55
  EINA_TRUE,
56
  /* an array of argument descriptions (must terminate with sentinel) */
57
  {
58

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."),
72

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
75
     * using =VALUE.
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.
80
     */
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.",
84
                                EINA_TRUE),
85
    ECORE_GETOPT_STORE_DEF_SHORT(0, "default-short", "Store a single short.",
86
                                 123),
87
    ECORE_GETOPT_STORE_DEF_INT(0, "default-int", "Store a single integer.",
88
                               1234),
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.",
93
                                  123),
94
    ECORE_GETOPT_STORE_DEF_UINT(0, "default-unsigned-int",
95
                                "Store a single unsigned integer.",
96
                                1234),
97
    ECORE_GETOPT_STORE_DEF_ULONG(0, "default-unsigned-long",
98
                                 "Store a single unsigned long integer.",
99
                                 12345),
100
    ECORE_GETOPT_STORE_DEF_DOUBLE(0, "default-double",
101
                                  "Store a single double.",
102
                                  12.345),
103

104
    /* you can specify the metavar so the --help will be more meaningful */
105
    ECORE_GETOPT_STORE_METAVAR_STR(0, "output", "Specify output file.",
106
                                   "FILENAME"),
107

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
111
     * set of choices.
112
     */
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",
119
                        available_choices),
120

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),
126

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()
136
     *
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
140
     * argv[].
141
     */
142
    ECORE_GETOPT_BREAK(0, "break"),
143

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"),
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

169
    /* the sentinel is required to notify end of descriptions */
170
    ECORE_GETOPT_SENTINEL
171
  }
172
};
173

174
int
175
main(int argc, char **argv)
176
{
177
   char *str_value = NULL;
178
   Eina_Bool bool_value = EINA_FALSE;
179
   short short_value = 0;
180
   int int_value = 0;
181
   long long_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 */
199
   int count = 0;
200
   char *choice = NULL;
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;
206
   int pos_int = 0;
207
   char *pos_choice = NULL;
208
#ifdef END_WITH_POS_APPEND
209
   Eina_List *pos_args = NULL;
210
#endif
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),
222

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),
233

234
     /* example of metavar usage */
235
     ECORE_GETOPT_VALUE_STR(output_value),
236

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),
243

244
     /* example of append multiple options */
245
     ECORE_GETOPT_VALUE_LIST(lst_strs),
246
     ECORE_GETOPT_VALUE_LIST(lst_ints),
247

248
     /* example of break option */
249
     ECORE_GETOPT_VALUE_BOOL(break_given),
250

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 */
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

266
     ECORE_GETOPT_VALUE_NONE /* sentinel */
267
   };
268
   int args, retval = EXIT_SUCCESS;
269

270
   ecore_init();
271

272
   /* during development is recommended to use the following to check
273
    * for duplicated options.
274
    */
275
   assert(ecore_getopt_parser_has_duplicates(&options) == EINA_FALSE);
276

277
   args = ecore_getopt_parse(&options, values, argc, argv);
278
   if (args < 0)
279
     {
280
        fputs("ERROR: Could not parse command line options.\n", stderr);
281
        retval = EXIT_FAILURE;
282
        goto end;
283
     }
284

285
   /* options that set 'quit_option' to true requires us to exit. */
286
   if (quit_option) goto end;
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

296
   printf("given values:\n"
297
          "string = %s\n"
298
          "bool = %s\n"
299
          "short = %hd\n"
300
          "int = %d\n"
301
          "long = %ld\n"
302
          "unsigned-short = %hu\n"
303
          "unsigned-int = %u\n"
304
          "unsigned-long = %lu\n"
305
          "double = %f\n"
306
          "\n"
307
          "default-string = %s\n"
308
          "default-bool = %s\n"
309
          "default-short = %hd\n"
310
          "default-int = %d\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"
316
          "\n"
317
          "output = %s\n"
318
          "use-x = %s (disabled by default)\n"
319
          "use-y = %s (enabled by default)\n"
320
          "use-it = %s\n"
321
          "counted = %d --countme\n"
322
          "choice = %s\n"
323
          "\n"
324
          "--break = %s\n"
325
          "\nDeclared Positional:\n"
326
          "STRING = %s\n"
327
          "INT = %d\n"
328
          "CHOICE = %s\n"
329
          "\n",
330
          str_value,
331
          bool_value ? "true" : "false",
332
          short_value,
333
          int_value,
334
          long_value,
335
          ushort_value,
336
          uint_value,
337
          ulong_value,
338
          double_value,
339
          def_str_value,
340
          def_bool_value ? "true" : "false",
341
          def_short_value,
342
          def_int_value,
343
          def_long_value,
344
          def_ushort_value,
345
          def_uint_value,
346
          def_ulong_value,
347
          def_double_value,
348
          output_value,
349
          use_x ? "enabled" : "disabled",
350
          use_y ? "enabled" : "disabled",
351
          use_z,
352
          count,
353
          choice,
354
          break_given ? "given" : "omitted",
355
          pos_str,
356
          pos_int,
357
          pos_choice);
358

359
   if (!lst_strs)
360
     puts("no --append-string=VALUE was given.");
361
   else
362
     {
363
        char *str;
364
        printf("%u strings given with --append-string=VALUE:\n",
365
               eina_list_count(lst_strs));
366
        EINA_LIST_FREE(lst_strs, str)
367
          {
368
             printf("\t%s\n", str);
369
             free(str);
370
          }
371
     }
372

373
   if (!lst_ints)
374
     puts("no --append-int=VALUE was given.");
375
   else
376
     {
377
        int *pi;
378
        printf("%u integers given with --append-int=VALUE:\n",
379
               eina_list_count(lst_ints));
380
        EINA_LIST_FREE(lst_ints, pi)
381
          {
382
             printf("\t%d\n", *pi);
383
             free(pi);
384
          }
385
     }
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
402
   if (args == argc)
403
     puts("no extra positional arguments were given.");
404
   else
405
     {
406
        printf("%d positional arguments were given:\n", argc - args);
407
        for (; args < argc; args++)
408
          printf("\t%s\n", argv[args]);
409
     }
410
#endif
411

412
 end:
413
   ecore_shutdown();
414
   return retval;
415
}
416

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.