Fix parsing of opt. multiple options w/o parameter
[ctxopt.git] / examples / example2.c
blob8b2c8e26bf46c3d503aa05a371aa137906093e27
1 #include "../ctxopt.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <ctype.h>
6 #include <stdint.h>
8 /* Utility functions */
9 /* ***************** */
11 static void
12 hexdump(void * mem, int len)
14 uint8_t * data = (uint8_t *)mem;
15 int pos = 0;
16 while (pos < len)
18 /* Print address */
19 /* """"""""""""" */
20 printf("%08x", pos);
22 /* Print hex bytes */
23 /* """"""""""""""" */
24 for (int i = 0; i < 16; i++)
26 if ((i & 7) == 0)
27 printf(" ");
28 if (pos + i < len)
29 printf(" %02x", data[pos + i]);
30 else
31 printf(" ");
34 /* Print ASCII bytes */
35 /* """"""""""""""""" */
36 printf(" |");
37 for (int i = 0; i < 16; i++)
39 /* Abort if at end */
40 if (pos + i >= len)
41 break;
43 /* Print if printable */
44 if (isprint((int)data[pos + i]))
45 printf("%c", data[pos + i]);
46 else
47 printf(".");
49 printf("|\n");
50 pos += 16;
54 /* Callback functions */
55 /* ****************** */
57 int
58 second_action(char * ctx_name, direction status, char * prev_ctx_name,
59 int nb_data, void ** data)
62 puts("Context action");
63 puts("==============");
64 if (ctx_name != NULL)
65 printf("ctx_name: %s\n", ctx_name);
67 printf("status: ");
68 if (status == entering)
70 puts("entering");
71 (*(int **)data)[1]++;
73 else
75 puts("exiting");
76 (*(int **)data)[1]++;
79 if (nb_data > 0)
80 hexdump(*(int **)data, 3 * sizeof(int));
82 puts("");
85 void
86 opt_action(char * ctx_name, char * opt_name, char * param, int nb_values,
87 char ** values, int nb_opt_data, void ** opt_data, int nb_ctx_data,
88 void ** ctx_data)
90 int v;
92 puts("Option action");
93 puts("=============");
94 if (ctx_name != NULL)
95 printf("ctx_name: %s\n", ctx_name);
97 if (opt_name != NULL)
98 printf("opt_name: %s\n", opt_name);
100 if (param != NULL)
101 printf("param: %s\n", param);
103 /* Display arguments */
104 /* """"""""""""""""" */
105 printf("Values (%2d): ", nb_values);
106 for (v = 0; v < nb_values; v++)
107 printf("%s ", values[v]);
108 puts("");
110 /* Decrement the second member of the first context argument */
111 /* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""*/
112 if (nb_ctx_data > 0)
113 (*(int **)ctx_data)[1]--;
115 /* Display option data */
116 /* """"""""""""""""""" */
117 if (nb_opt_data > 0)
119 printf("opt_data (%2d): ", nb_opt_data);
120 hexdump(*(char **)opt_data, strlen(*(char **)opt_data) + 1);
121 puts("");
124 /* Display context data */
125 /* """""""""""""""""""" */
126 if (nb_ctx_data > 0)
128 printf("ctx_data (%2d):\n", nb_ctx_data);
129 hexdump(*(int **)ctx_data, 3 * sizeof(int));
130 puts("");
134 /* Program entry */
135 /* ************* */
138 main(int argc, char * argv[])
140 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
141 char ** rem_args = NULL; /* Remaining arguments string array. */
143 int ctx_data[] = { 1, 2, 3 };
144 char opt_data[] = "test";
146 /* initialize cop */
147 /* """""""""""""" */
148 ctxopt_init(argv[0], "stop_if_non_option=0 "
149 "allow_abbreviations=1 ");
151 /* Create new contexts with their allowed options */
152 /* """""""""""""""""""""""""""""""""""""""""""""" */
153 ctxopt_new_ctx("first", "[a>second... #<string>...]");
154 ctxopt_new_ctx("second", "[b>third [#<string>]]");
155 ctxopt_new_ctx("third", "[c #<string>]");
157 /* Attach parameters to options */
158 /* """""""""""""""""""""""""""" */
159 ctxopt_add_opt_settings(parameters, "a", "-a");
160 ctxopt_add_opt_settings(parameters, "b", "-b");
161 ctxopt_add_opt_settings(parameters, "c", "-c");
163 /* Attach a callback action to the second context */
164 /* """""""""""""""""""""""""""""""""""""""""""""" */
165 ctxopt_add_ctx_settings(actions, "second", second_action, &ctx_data, NULL);
167 /* Attach a callback action to options */
168 /* """"""""""""""""""""""""""""""""""" */
169 ctxopt_add_opt_settings(actions, "a", opt_action, NULL);
170 ctxopt_add_opt_settings(actions, "b", opt_action, &opt_data, NULL);
171 ctxopt_add_opt_settings(actions, "c", opt_action, NULL);
173 /* Parse and check the command line options */
174 /* """""""""""""""""""""""""""""""""""""""" */
175 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
177 /* Execute callback actions in sequence */
178 /* """""""""""""""""""""""""""""""""""" */
179 ctxopt_evaluate();
181 /* Print remaining non-arguments if any */
182 /* """""""""""""""""""""""""""""""""""" */
183 if (nb_rem_args > 0)
185 int a;
187 puts("Remaining unparsed command line words");
188 puts("=====================================");
189 for (a = 0; a < nb_rem_args; a++)
190 printf("%s ", rem_args[a]);
191 puts("");
194 exit(EXIT_SUCCESS);