Fix parsing of opt. multiple options w/o parameter
[ctxopt.git] / examples / hello.c
blob9ecd02e67845e3e5aecf60d04dde4c9e05956450
1 #include "../ctxopt.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 /* Callback functions */
7 /* ****************** */
9 void
10 name_action(char * ctx_name, char * opt_name, char * param, int nb_values,
11 char ** values, int nb_opt_data, void ** opt_data, int nb_ctx_data,
12 void ** ctx_data)
14 int v;
16 printf("Hello %s", values[0]); /* First command line argument after name *
17 | (-n or -name). */
19 for (v = 1; v < nb_values; v++) /* Other command line arguments. */
20 printf(", %s", values[v]);
22 printf(".\n");
25 /* Program entry */
26 /* ************* */
28 int
29 main(int argc, char * argv[])
31 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
32 char ** rem_args = NULL; /* Remaining arguments string array. */
34 /* initialize cop */
35 /* """""""""""""" */
36 ctxopt_init(argv[0], "stop_if_non_option=Yes "
37 "allow_abbreviations=Yes ");
39 /* Create a new context with its allowed options. */
40 /* """"""""""""""""""""""""""""""""""""""""""""""" */
41 ctxopt_new_ctx("main", "[name... #<string>...]");
43 /* Attach parameters to the name option */
44 /* """""""""""""""""""""""""""""""""""" */
45 ctxopt_add_opt_settings(parameters, "name", "-n -name");
47 /* Attach a callback action to the name option */
48 /* """"""""""""""""""""""""""""""""""""""""""" */
49 ctxopt_add_opt_settings(actions, "name", name_action, NULL);
51 /* Parse and check the command line options */
52 /* """""""""""""""""""""""""""""""""""""""" */
53 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
55 /* Manage the remaining non options */
56 /* """""""""""""""""""""""""""""""" */
57 if (nb_rem_args > 0)
59 printf("Non-arguments are not allowed.\n");
60 exit(EXIT_FAILURE);
63 /* Execute the call back actions in sequence */
64 /* """"""""""""""""""""""""""""""""""""""""" */
65 ctxopt_evaluate();
67 /* prints the default messages when there is no command line argument. */
68 /* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
69 if (argc == 1)
70 printf("Hello world.\n");
72 exit(EXIT_SUCCESS);