summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-12-17 18:08:27 -0200
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2013-12-17 18:08:27 -0200
commit1c1f9ea0e1a2c4014d5cb5ca0f749164dcd384e5 (patch)
tree1a45360e5ed52b8c9d81be3ac3c9d2caa8867950 /src/examples
parent99ba5822c84c6716ee51af93115a0bb0984464ef (diff)
add great ecore_getopt example.
should cover and explain getopt usage and serve as base for applications that want to use it (all efl should!)
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/ecore/.gitignore1
-rw-r--r--src/examples/ecore/Makefile.am9
-rw-r--r--src/examples/ecore/Makefile.examples3
-rw-r--r--src/examples/ecore/ecore_getopt_example.c339
4 files changed, 349 insertions, 3 deletions
diff --git a/src/examples/ecore/.gitignore b/src/examples/ecore/.gitignore
index a04eae9802..d77145eaf5 100644
--- a/src/examples/ecore/.gitignore
+++ b/src/examples/ecore/.gitignore
@@ -37,3 +37,4 @@
37/ecore_file_download_example 37/ecore_file_download_example
38/ecore_imf_example 38/ecore_imf_example
39/ecore_pipe_gstreamer_example 39/ecore_pipe_gstreamer_example
40/ecore_getopt_example
diff --git a/src/examples/ecore/Makefile.am b/src/examples/ecore/Makefile.am
index 129601eb76..9edeb1fc3f 100644
--- a/src/examples/ecore/Makefile.am
+++ b/src/examples/ecore/Makefile.am
@@ -64,7 +64,8 @@ ecore_poller_example \
64ecore_server_bench \ 64ecore_server_bench \
65ecore_thread_example \ 65ecore_thread_example \
66ecore_time_functions_example \ 66ecore_time_functions_example \
67ecore_timer_example 67ecore_timer_example \
68ecore_getopt_example
68 69
69ECORE_COMMON_LDADD = \ 70ECORE_COMMON_LDADD = \
70$(top_builddir)/src/lib/ecore/libecore.la \ 71$(top_builddir)/src/lib/ecore/libecore.la \
@@ -223,6 +224,9 @@ ecore_time_functions_example_LDADD = $(ECORE_COMMON_LDADD)
223ecore_timer_example_SOURCES = ecore_timer_example.c 224ecore_timer_example_SOURCES = ecore_timer_example.c
224ecore_timer_example_LDADD = $(ECORE_COMMON_LDADD) 225ecore_timer_example_LDADD = $(ECORE_COMMON_LDADD)
225 226
227ecore_getopt_example_SOURCES = ecore_getopt_example.c
228ecore_getopt_example_LDADD = $(ECORE_COMMON_LDADD)
229
226SRCS = \ 230SRCS = \
227ecore_animator_example.c \ 231ecore_animator_example.c \
228ecore_audio_custom.c \ 232ecore_audio_custom.c \
@@ -263,7 +267,8 @@ ecore_poller_example.c \
263ecore_server_bench.c \ 267ecore_server_bench.c \
264ecore_thread_example.c \ 268ecore_thread_example.c \
265ecore_time_functions_example.c \ 269ecore_time_functions_example.c \
266ecore_timer_example.c 270ecore_timer_example.c \
271ecore_getopt_example.c
267 272
268DATA_FILES = red.png Makefile.examples 273DATA_FILES = red.png Makefile.examples
269 274
diff --git a/src/examples/ecore/Makefile.examples b/src/examples/ecore/Makefile.examples
index 30a9521861..f8ac82d50f 100644
--- a/src/examples/ecore/Makefile.examples
+++ b/src/examples/ecore/Makefile.examples
@@ -39,7 +39,8 @@ EXAMPLES= ecore_animator_example \
39 ecore_server_bench \ 39 ecore_server_bench \
40 ecore_thread_example \ 40 ecore_thread_example \
41 ecore_time_functions_example \ 41 ecore_time_functions_example \
42 ecore_timer_example 42 ecore_timer_example \
43 ecore_getopt_example
43 44
44all: examples 45all: examples
45examples: $(EXAMPLES) 46examples: $(EXAMPLES)
diff --git a/src/examples/ecore/ecore_getopt_example.c b/src/examples/ecore/ecore_getopt_example.c
new file mode 100644
index 0000000000..dc91f0c237
--- /dev/null
+++ b/src/examples/ecore/ecore_getopt_example.c
@@ -0,0 +1,339 @@
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
8static const char * available_choices[] = {
9 "banana",
10 "apple",
11 "orange",
12 NULL /* must be null terminated! */
13};
14
15static const Ecore_Getopt options = {
16 /* program name, usually a macro PACKAGE_NAME */
17 "ecore_getopt_example",
18 /* usage line */
19 "%prog [options]",
20 /* program version, usually a macro PACKAGE_VERSION */
21 "0.1",
22 /* copyright string */
23 "(C) 2013 Enlightenment Project",
24 /* license string */
25 "BSD 2-Clause",
26 /* long description, may be multiline and contain \n */
27 "Example of Ecore_Getopt usage.\n"
28 "\n"
29 "This usage may span over multiple lines of text, with automatic line "
30 "break based on $COLUMNS environment variable that is usually defined by "
31 "your shell.\n"
32 "You can have %%prog (expands to \"%prog\") or %%version (expands to \""
33 "%version\") in the description to get the program name "
34 "or version. Use double %% to get the percentage symbol.\n"
35 "OneCanHaveVeryLongWorksInDescriptionLinesSuchAsThisOneAndItWillBeBrokenWhenTheyGoPast${COLUMNS}Characters.\n"
36 "\tTab (\\t) is supported, like in the beginning of this line. They "
37 "will work as tabulation to columns multiple of 8 spaces, so you can do "
38 "tables such as:\n"
39 "1\tsomething\tsome description\n"
40 "23\totherthing\tsome description\n"
41 "456\tyetanother\tsome description\n"
42 "12345678\tthis is off\tthis is off\n",
43 /* we want strict parsing (any error aborts) */
44 EINA_TRUE,
45 /* an array of argument descriptions (must terminate with sentinel) */
46 {
47
48 /* block of options that store a single value in a variable of type */
49 ECORE_GETOPT_STORE_STR(0, "string", "Store a single string."),
50 ECORE_GETOPT_STORE_BOOL(0, "bool", "Store a single boolean."),
51 ECORE_GETOPT_STORE_SHORT(0, "short", "Store a single short."),
52 ECORE_GETOPT_STORE_INT(0, "int", "Store a single integer."),
53 ECORE_GETOPT_STORE_LONG(0, "long", "Store a single long integer."),
54 ECORE_GETOPT_STORE_USHORT(0, "unsigned-short",
55 "Store a single unsigned short integer."),
56 ECORE_GETOPT_STORE_UINT(0, "unsigned-int",
57 "Store a single unsigned integer."),
58 ECORE_GETOPT_STORE_ULONG(0, "unsigned-long",
59 "Store a single unsigned long integer."),
60 ECORE_GETOPT_STORE_DOUBLE(0, "double", "Store a single double."),
61
62 /* block of options that store a single value in a variable of type
63 * and use a default value if option IS SPECIFIED but no value is given
64 * using =VALUE.
65 * If option -o has default value of X, then the command lines produce:
66 * <empty>: nothing is set.
67 * -o: value is set to X.
68 * -o=Y: value is set to Y.
69 */
70 ECORE_GETOPT_STORE_DEF_STR(0, "default-string", "Store a single string.",
71 "default-string-value"),
72 ECORE_GETOPT_STORE_DEF_BOOL(0, "default-bool", "Store a single boolean.",
73 EINA_TRUE),
74 ECORE_GETOPT_STORE_DEF_SHORT(0, "default-short", "Store a single short.",
75 123),
76 ECORE_GETOPT_STORE_DEF_INT(0, "default-int", "Store a single integer.",
77 1234),
78 ECORE_GETOPT_STORE_DEF_LONG(0, "default-long",
79 "Store a single long integer.", 12345),
80 ECORE_GETOPT_STORE_DEF_USHORT(0, "default-unsigned-short",
81 "Store a single unsigned short integer.",
82 123),
83 ECORE_GETOPT_STORE_DEF_UINT(0, "default-unsigned-int",
84 "Store a single unsigned integer.",
85 1234),
86 ECORE_GETOPT_STORE_DEF_ULONG(0, "default-unsigned-long",
87 "Store a single unsigned long integer.",
88 12345),
89 ECORE_GETOPT_STORE_DEF_DOUBLE(0, "default-double",
90 "Store a single double.",
91 12.345),
92
93 /* you can specify the metavar so the --help will be more meaningful */
94 ECORE_GETOPT_STORE_METAVAR_STR(0, "output", "Specify output file.",
95 "FILENAME"),
96
97 /* Other than storing a given value (or default), it is common to
98 * have boolean options (ie: --debug, --daemon), those that set a
99 * constant value, counters (ie: --verbose) or option from a fixed
100 * set of choices.
101 */
102 ECORE_GETOPT_STORE_TRUE(0, "enable-x", "Enables X."),
103 ECORE_GETOPT_STORE_FALSE(0, "disable-y", "Disables Y."),
104 ECORE_GETOPT_STORE_CONST(0, "set-z", "Set z to constant XPTO.", "XPTO"),
105 ECORE_GETOPT_COUNT(0, "countme",
106 "Counts number of times this option is given."),
107 ECORE_GETOPT_CHOICE(0, "choose", "Choose from one of the options",
108 available_choices),
109
110 /* one can create a list of given values of a certain type */
111 ECORE_GETOPT_APPEND(0, "append-string", "Store multiple strings.",
112 ECORE_GETOPT_TYPE_STR),
113 ECORE_GETOPT_APPEND(0, "append-int", "Store multiple integers.",
114 ECORE_GETOPT_TYPE_INT),
115
116 /* break options will force everything that goes after it to be ignored
117 * by the option parser and they will go as arguments. This is the case
118 * for xterm's -e. Example:
119 * program --string=A: stores "A" into str_value.
120 * program --string=A --break: still stores "A" into str_value.
121 * program --break --string=A: str_value is untouched, --string=a
122 * is avaiable in argv[args], with
123 * args being the index returned by
124 * ecore_getopt_parse()
125 *
126 * Note that ecore_getopt will follow GNU and stop parsing arguments
127 * once -- is found, similar to "rm -- -fr /". In this case the
128 * return of ecore_getopt_parse() is to the index of "--" element in
129 * argv[].
130 */
131 ECORE_GETOPT_BREAK(0, "break"),
132
133 /* standard block to provide version, copyright, license and help */
134 ECORE_GETOPT_VERSION('V', "version"),
135 ECORE_GETOPT_COPYRIGHT('C', "copyright"),
136 ECORE_GETOPT_LICENSE('L', "license"),
137 ECORE_GETOPT_HELP('h', "help"),
138
139 /* the sentinel is required to notify end of descriptions */
140 ECORE_GETOPT_SENTINEL
141 }
142};
143
144int
145main(int argc, char **argv)
146{
147 char *str_value = NULL;
148 Eina_Bool bool_value = EINA_FALSE;
149 short short_value = 0;
150 int int_value = 0;
151 long long_value = 0;
152 unsigned short ushort_value = 0;
153 unsigned int uint_value = 0;
154 unsigned long ulong_value = 0;
155 double double_value = 0;
156 char *def_str_value = NULL;
157 Eina_Bool def_bool_value = EINA_FALSE;
158 short def_short_value = 0;
159 int def_int_value = 0;
160 long def_long_value = 0;
161 unsigned short def_ushort_value = 0;
162 unsigned int def_uint_value = 0;
163 unsigned long def_ulong_value = 0;
164 double def_double_value = 0;
165 char *output_value = NULL;
166 Eina_Bool use_x = EINA_FALSE; /* if stores true, then start with false */
167 Eina_Bool use_y = EINA_TRUE; /* if stores false, then start with true */
168 char *use_z = NULL; /* stores a pointer here */
169 int count = 0;
170 char *choice = NULL;
171 Eina_List *lst_strs = NULL;
172 Eina_List *lst_ints = NULL;
173 Eina_Bool break_given = EINA_FALSE;
174 Eina_Bool quit_option = EINA_FALSE;
175 Ecore_Getopt_Value values[] = {
176 /* block of options that store a single value in a variable of type */
177 ECORE_GETOPT_VALUE_STR(str_value),
178 ECORE_GETOPT_VALUE_BOOL(bool_value),
179 ECORE_GETOPT_VALUE_SHORT(short_value),
180 ECORE_GETOPT_VALUE_INT(int_value),
181 ECORE_GETOPT_VALUE_LONG(long_value),
182 ECORE_GETOPT_VALUE_USHORT(ushort_value),
183 ECORE_GETOPT_VALUE_UINT(uint_value),
184 ECORE_GETOPT_VALUE_ULONG(ulong_value),
185 ECORE_GETOPT_VALUE_DOUBLE(double_value),
186
187 /* you can use options with default value (if =VALUE is omitted) */
188 ECORE_GETOPT_VALUE_STR(def_str_value),
189 ECORE_GETOPT_VALUE_BOOL(def_bool_value),
190 ECORE_GETOPT_VALUE_SHORT(def_short_value),
191 ECORE_GETOPT_VALUE_INT(def_int_value),
192 ECORE_GETOPT_VALUE_LONG(def_long_value),
193 ECORE_GETOPT_VALUE_USHORT(def_ushort_value),
194 ECORE_GETOPT_VALUE_UINT(def_uint_value),
195 ECORE_GETOPT_VALUE_ULONG(def_ulong_value),
196 ECORE_GETOPT_VALUE_DOUBLE(def_double_value),
197
198 /* example of metavar usage */
199 ECORE_GETOPT_VALUE_STR(output_value),
200
201 /* example of store true, false, const */
202 ECORE_GETOPT_VALUE_BOOL(use_x),
203 ECORE_GETOPT_VALUE_BOOL(use_y),
204 ECORE_GETOPT_VALUE_STR(use_z),
205 ECORE_GETOPT_VALUE_INT(count),
206 ECORE_GETOPT_VALUE_STR(choice),
207
208 /* example of append multiple options */
209 ECORE_GETOPT_VALUE_LIST(lst_strs),
210 ECORE_GETOPT_VALUE_LIST(lst_ints),
211
212 /* example of break option */
213 ECORE_GETOPT_VALUE_BOOL(break_given),
214
215 /* standard block to provide version, copyright, license and help */
216 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */
217 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */
218 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */
219 ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */
220
221 ECORE_GETOPT_VALUE_NONE /* sentinel */
222 };
223 int args, retval = EXIT_SUCCESS;
224
225 ecore_init();
226
227 /* during development is recommended to use the following to check
228 * for duplicated options.
229 */
230 assert(ecore_getopt_parser_has_duplicates(&options) == EINA_FALSE);
231
232 args = ecore_getopt_parse(&options, values, argc, argv);
233 if (args < 0)
234 {
235 fputs("ERROR: Could not parse command line options.\n", stderr);
236 retval = EXIT_FAILURE;
237 goto end;
238 }
239
240 /* options that set 'quit_option' to true requires us to exit. */
241 if (quit_option) goto end;
242
243 printf("given values:\n"
244 "string = %s\n"
245 "bool = %s\n"
246 "short = %hd\n"
247 "int = %d\n"
248 "long = %ld\n"
249 "unsigned-short = %hu\n"
250 "unsigned-int = %u\n"
251 "unsigned-long = %lu\n"
252 "double = %f\n"
253 "\n"
254 "default-string = %s\n"
255 "default-bool = %s\n"
256 "default-short = %hd\n"
257 "default-int = %d\n"
258 "default-long = %ld\n"
259 "default-unsigned-short = %hu\n"
260 "default-unsigned-int = %u\n"
261 "default-unsigned-long = %lu\n"
262 "default-double = %f\n"
263 "\n"
264 "output = %s\n"
265 "use-x = %s (disabled by default)\n"
266 "use-y = %s (enabled by default)\n"
267 "use-it = %s\n"
268 "counted = %d --countme\n"
269 "choice = %s\n"
270 "\n"
271 "--break = %s\n"
272 "\n",
273 str_value,
274 bool_value ? "true" : "false",
275 short_value,
276 int_value,
277 long_value,
278 ushort_value,
279 uint_value,
280 ulong_value,
281 double_value,
282 def_str_value,
283 def_bool_value ? "true" : "false",
284 def_short_value,
285 def_int_value,
286 def_long_value,
287 def_ushort_value,
288 def_uint_value,
289 def_ulong_value,
290 def_double_value,
291 output_value,
292 use_x ? "enabled" : "disabled",
293 use_y ? "enabled" : "disabled",
294 use_z,
295 count,
296 choice,
297 break_given ? "given" : "omitted");
298
299 if (!lst_strs)
300 puts("no --append-string=VALUE was given.");
301 else
302 {
303 char *str;
304 printf("%u strings given with --append-string=VALUE:\n",
305 eina_list_count(lst_strs));
306 EINA_LIST_FREE(lst_strs, str)
307 {
308 printf("\t%s\n", str);
309 free(str);
310 }
311 }
312
313 if (!lst_ints)
314 puts("no --append-int=VALUE was given.");
315 else
316 {
317 int *pi;
318 printf("%u integers given with --append-int=VALUE:\n",
319 eina_list_count(lst_ints));
320 EINA_LIST_FREE(lst_ints, pi)
321 {
322 printf("\t%d\n", *pi);
323 free(pi);
324 }
325 }
326
327 if (args == argc)
328 puts("no positional arguments were given.");
329 else
330 {
331 printf("%d positional arguments were given:\n", argc - args);
332 for (; args < argc; args++)
333 printf("\t%s\n", argv[args]);
334 }
335
336 end:
337 ecore_shutdown();
338 return retval;
339}