8 /* Utility functions */
9 /* ***************** */
12 hexdump(void * mem
, int len
)
14 uint8_t * data
= (uint8_t *)mem
;
24 for (int i
= 0; i
< 16; i
++)
29 printf(" %02x", data
[pos
+ i
]);
34 /* Print ASCII bytes */
35 /* """"""""""""""""" */
37 for (int i
= 0; i
< 16; i
++)
43 /* Print if printable */
44 if (isprint((int)data
[pos
+ i
]))
45 printf("%c", data
[pos
+ i
]);
54 /* Callback functions */
55 /* ****************** */
58 second_action(char * ctx_name
, direction status
, char * prev_ctx_name
,
59 int nb_data
, void ** data
)
62 puts("Context action");
63 puts("==============");
65 printf("ctx_name: %s\n", ctx_name
);
68 if (status
== entering
)
80 hexdump(*(int **)data
, 3 * sizeof(int));
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
,
92 puts("Option action");
93 puts("=============");
95 printf("ctx_name: %s\n", ctx_name
);
98 printf("opt_name: %s\n", opt_name
);
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
]);
110 /* Decrement the second member of the first context argument */
111 /* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""*/
113 (*(int **)ctx_data
)[1]--;
115 /* Display option data */
116 /* """"""""""""""""""" */
119 printf("opt_data (%2d): ", nb_opt_data
);
120 hexdump(*(char **)opt_data
, strlen(*(char **)opt_data
) + 1);
124 /* Display context data */
125 /* """""""""""""""""""" */
128 printf("ctx_data (%2d):\n", nb_ctx_data
);
129 hexdump(*(int **)ctx_data
, 3 * sizeof(int));
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";
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 /* """""""""""""""""""""""""""""""""""" */
181 /* Print remaining non-arguments if any */
182 /* """""""""""""""""""""""""""""""""""" */
187 puts("Remaining unparsed command line words");
188 puts("=====================================");
189 for (a
= 0; a
< nb_rem_args
; a
++)
190 printf("%s ", rem_args
[a
]);