Do not compile not yet used code
[ctxopt.git] / examples / hello.c
blob8afd9ae43c45d9b120b41a05295c31513d6f8023
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]);
38 /* Create a new context with its allowed options. */
39 /* """"""""""""""""""""""""""""""""""""""""""""""" */
40 ctxopt_new_ctx("main", "[name... #<string>...]");
42 /* Attach parameters to the name option */
43 /* """""""""""""""""""""""""""""""""""" */
44 ctxopt_add_opt_settings(parameters, "name", "-n -name");
46 /* Attach a callback action to the name option */
47 /* """"""""""""""""""""""""""""""""""""""""""" */
48 ctxopt_add_opt_settings(actions, "name", name_action, NULL);
50 /* Parse and check the command line options */
51 /* """""""""""""""""""""""""""""""""""""""" */
52 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
54 /* Manage the remaining non options */
55 /* """""""""""""""""""""""""""""""" */
56 if (nb_rem_args > 0)
58 printf("Non-arguments are not allowed.\n");
59 exit(EXIT_FAILURE);
62 /* Execute the call back actions in sequence */
63 /* """"""""""""""""""""""""""""""""""""""""" */
64 ctxopt_evaluate();
66 /* prints the default messages when there is no command line argument. */
67 /* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
68 if (argc == 1)
69 printf("Hello world.\n");
71 exit(EXIT_SUCCESS);