Add comments in the list code
[ctxopt.git] / examples / example1.c
blob0a5168895266136fa03ace9ef2d34cbaee7b7c19
1 #include "../ctxopt.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 /* Callback functions */
7 /* ****************** */
9 void
10 user_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 printf("User %s\n", values[0]);
17 void
18 groups_action(char * ctx_name, char * opt_name, char * param, int nb_values,
19 char ** values, int nb_opt_data, void ** opt_data,
20 int nb_ctx_data, void ** ctx_data)
22 int v;
24 printf("Groups %s", values[0]);
26 for (v = 1; v < nb_values; v++)
27 printf(", %s", values[v]);
29 printf(".\n");
32 /* Program entry */
33 /* ************* */
35 int
36 main(int argc, char * argv[])
38 int nb_rem_args = 0; /* Nb of remaining unprocessed arguments. */
39 char ** rem_args = NULL; /* Remaining arguments string array. */
41 /* initialize ctxopt */
42 /* """"""""""""""""" */
43 ctxopt_init(argv[0], "stop_if_non_option=Yes "
44 "allow_abbreviations=no ");
46 /* Create new contexts with their allowed options */
47 /* """""""""""""""""""""""""""""""""""""""""""""" */
48 ctxopt_new_ctx("main", "[user>ctx1... #<string>]");
49 ctxopt_new_ctx("ctx1", "group #<string>...");
51 /* Attach parameters to options */
52 /* """""""""""""""""""""""""""" */
53 ctxopt_add_opt_settings(parameters, "user", "-u -user");
54 ctxopt_add_opt_settings(parameters, "group", "-g -groups");
56 /* Attach a callback action to options */
57 /* """"""""""""""""""""""""""""""""""" */
58 ctxopt_add_opt_settings(actions, "user", user_action, NULL);
59 ctxopt_add_opt_settings(actions, "group", groups_action, NULL);
61 /* Parse and check the command line options */
62 /* """""""""""""""""""""""""""""""""""""""" */
63 ctxopt_analyze(argc - 1, argv + 1, &nb_rem_args, &rem_args);
65 /* Execute registered actions in sequence */
66 /* """""""""""""""""""""""""""""""""""""" */
67 ctxopt_evaluate();
69 exit(EXIT_SUCCESS);