1 /****************************************************************\
2 *                                                                *
3 *  Library for command line argument processing                  *
4 *                                                                *
5 *  Guy St.C. Slater..   mailto:guy@ebi.ac.uk                     *
6 *  Copyright (C) 2000-2009.  All Rights Reserved.                *
7 *                                                                *
8 *  This source code is distributed under the terms of the        *
9 *  GNU General Public License, version 3. See the file COPYING   *
10 *  or http://www.gnu.org/licenses/gpl.txt for details            *
11 *                                                                *
12 *  If you use this code, please keep this notice intact.         *
13 *                                                                *
14 \****************************************************************/
15 
16 #ifndef INCLUDED_ARGUMENT_H
17 #define INCLUDED_ARGUMENT_H
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif /* __cplusplus */
22 
23 #include <stdio.h>
24 #include <limits.h> /* For CHAR_BIT */
25 
26 #include <glib.h>
27 
28 #ifndef ALPHABETSIZE
29 #define ALPHABETSIZE (1<<CHAR_BIT)
30 #endif /* ALPHABETSIZE */
31 
32 typedef gchar *(*ArgumentHandler)(gchar *arg_string,
33                                   gpointer data);
34 
35 typedef void (*Argument_Cleanup_Func)(gpointer user_data);
36 
37 typedef struct {
38         gchar *desc;       /* Description */
39     GPtrArray *arg_option; /* Sets of argument options */
40 } ArgumentSet;
41 
42 typedef enum {
43     ArgumentSource_NOT_SET = 0,
44     ArgumentSource_FROM_LONG_ARGUMENT,
45     ArgumentSource_FROM_SHORT_ARGUMENT,
46     ArgumentSource_FROM_ENV_VAR
47 } ArgumentSource;
48 
49 typedef struct {
50         ArgumentSet *as;
51               gchar  symbol;
52               gchar *option;
53               gchar *type; /* NULL for booleans */
54               gchar *desc;
55               gchar *default_string; /* NULL for mandatory arguments */
56     ArgumentHandler  handler; /* NULL for lists */
57            gpointer  handler_data;
58               gchar *env_var;
59      ArgumentSource  source;
60               gchar *arg_value;
61           GPtrArray *arg_list; /* Only used when a list argument */
62 } ArgumentOption;
63 
64 typedef struct {
65               gint   argc;
66              gchar **argv;
67              gchar  *name; /* Program name */
68              gchar  *desc; /* Description  */
69          GPtrArray  *arg_set; /* Set of argument sets */
70          GPtrArray  *mandatory_set; /* Set of mandatory arguments */
71     ArgumentOption  *symbol_registry[ALPHABETSIZE];
72              GTree  *option_registry;
73           gboolean   show_short_help;
74           gboolean   show_long_help;
75          GPtrArray  *cleanup_list;
76 } Argument;
77 
78 gint Argument_main(Argument *arg);
79 void Argument_process(Argument *arg, gchar *name, gchar *desc,
80                       gchar *synopsis);
81 void Argument_info(Argument *arg);
82 
83 ArgumentSet *ArgumentSet_create(gchar *desc);
84        void  Argument_absorb_ArgumentSet(Argument *arg,
85                                  ArgumentSet *as);
86 /* ArgumentSet does not need to be freed after absorption */
87 
88 void ArgumentSet_add_option(ArgumentSet *as,
89               gchar symbol,
90               gchar *option,
91               gchar *type,
92               gchar *desc,
93               gchar *default_string,
94               ArgumentHandler handler,
95               gpointer handler_data);
96 /* Set <symbol> to '\0' if a single-letter flag is not required.
97  *
98  * If no handler is supplied, the argument is assumed to be a list.
99  */
100 
101 /* Some common handlers */
102 gchar *Argument_parse_string(gchar *arg_string, gpointer data);
103 gchar *Argument_parse_char(gchar *arg_string, gpointer data);
104 gchar *Argument_parse_int(gchar *arg_string, gpointer data);
105 gchar *Argument_parse_float(gchar *arg_string, gpointer data);
106 gchar *Argument_parse_boolean(gchar *arg_string, gpointer data);
107 
108 void Argument_add_cleanup(Argument *arg,
109                           Argument_Cleanup_Func cleanup_func,
110                           gpointer user_data);
111 
112 #ifdef __cplusplus
113 }
114 #endif /* __cplusplus */
115 
116 #endif /* INCLUDED_ARGUMENT_H */
117 
118