2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
16 #if defined(__cplusplus)
20 struct isl_arg_choice
{
25 struct isl_arg_flags
{
52 enum isl_arg_type type
;
54 const char *long_name
;
55 const char *argument_name
;
58 #define ISL_ARG_SINGLE_DASH (1 << 0)
59 #define ISL_ARG_BOOL_ARG (1 << 1)
60 #define ISL_ARG_HIDDEN (1 << 2)
64 struct isl_arg_choice
*choice
;
65 unsigned default_value
;
66 unsigned default_selected
;
67 int (*set
)(void *opt
, unsigned val
);
70 struct isl_arg_flags
*flags
;
71 unsigned default_value
;
74 unsigned default_value
;
75 int (*set
)(void *opt
, unsigned val
);
82 long default_selected
;
83 int (*set
)(void *opt
, long val
);
86 unsigned long default_value
;
89 const char *default_value
;
95 struct isl_args
*child
;
98 void (*print_version
)(void);
102 void (*clear
)(void*);
109 struct isl_arg
*args
;
112 #define ISL_ARGS_START(s,name) \
113 struct isl_arg name ## LIST[]; \
114 struct isl_args name = { sizeof(s), name ## LIST }; \
115 struct isl_arg name ## LIST[] = {
116 #define ISL_ARGS_END \
119 #define ISL_ARG_ALIAS(l) { \
120 .type = isl_arg_alias, \
123 #define ISL_ARG_ARG(st,f,a,d) { \
124 .type = isl_arg_arg, \
125 .argument_name = a, \
126 .offset = offsetof(st, f), \
127 .u = { .str = { .default_value = d } } \
129 #define ISL_ARG_FOOTER(h) { \
130 .type = isl_arg_footer, \
133 #define ISL_ARG_CHOICE(st,f,s,l,c,d,h) { \
134 .type = isl_arg_choice, \
137 .offset = offsetof(st, f), \
139 .u = { .choice = { .choice = c, .default_value = d, \
140 .default_selected = d, .set = NULL } } \
142 #define ISL_ARG_OPT_CHOICE(st,f,s,l,c,d,ds,h) { \
143 .type = isl_arg_choice, \
146 .offset = offsetof(st, f), \
148 .u = { .choice = { .choice = c, .default_value = d, \
149 .default_selected = ds, .set = NULL } } \
151 #define ISL_ARG_PHANTOM_USER_CHOICE_F(s,l,c,setter,d,h,fl) { \
152 .type = isl_arg_choice, \
158 .u = { .choice = { .choice = c, .default_value = d, \
159 .default_selected = d, .set = setter } } \
161 #define ISL_ARG_USER_OPT_CHOICE(st,f,s,l,c,setter,d,ds,h) { \
162 .type = isl_arg_choice, \
165 .offset = offsetof(st, f), \
167 .u = { .choice = { .choice = c, .default_value = d, \
168 .default_selected = ds, .set = setter } } \
170 #define _ISL_ARG_BOOL_F(o,s,l,setter,d,h,fl) { \
171 .type = isl_arg_bool, \
177 .u = { .b = { .default_value = d, .set = setter } } \
179 #define ISL_ARG_BOOL_F(st,f,s,l,d,h,fl) \
180 _ISL_ARG_BOOL_F(offsetof(st, f),s,l,NULL,d,h,fl)
181 #define ISL_ARG_BOOL(st,f,s,l,d,h) \
182 ISL_ARG_BOOL_F(st,f,s,l,d,h,0)
183 #define ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,fl) \
184 _ISL_ARG_BOOL_F(-1,s,l,setter,0,h,fl)
185 #define ISL_ARG_PHANTOM_BOOL(s,l,setter,h) \
186 ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,0)
187 #define ISL_ARG_INT_F(st,f,s,l,a,d,h,fl) { \
188 .type = isl_arg_int, \
191 .argument_name = a, \
192 .offset = offsetof(st, f), \
195 .u = { .ul = { .default_value = d } } \
197 #define ISL_ARG_INT(st,f,s,l,a,d,h) \
198 ISL_ARG_INT_F(st,f,s,l,a,d,h,0)
199 #define ISL_ARG_LONG(st,f,s,lo,d,h) { \
200 .type = isl_arg_long, \
203 .offset = offsetof(st, f), \
205 .u = { .l = { .default_value = d, .default_selected = d, \
208 #define ISL_ARG_USER_LONG(st,f,s,lo,setter,d,h) { \
209 .type = isl_arg_long, \
212 .offset = offsetof(st, f), \
214 .u = { .l = { .default_value = d, .default_selected = d, \
217 #define ISL_ARG_OPT_LONG(st,f,s,lo,d,ds,h) { \
218 .type = isl_arg_long, \
221 .offset = offsetof(st, f), \
223 .u = { .l = { .default_value = d, .default_selected = ds, \
226 #define ISL_ARG_ULONG(st,f,s,l,d,h) { \
227 .type = isl_arg_ulong, \
230 .offset = offsetof(st, f), \
232 .u = { .ul = { .default_value = d } } \
234 #define ISL_ARG_STR_F(st,f,s,l,a,d,h,fl) { \
235 .type = isl_arg_str, \
238 .argument_name = a, \
239 .offset = offsetof(st, f), \
242 .u = { .str = { .default_value = d } } \
244 #define ISL_ARG_STR(st,f,s,l,a,d,h) \
245 ISL_ARG_STR_F(st,f,s,l,a,d,h,0)
246 #define ISL_ARG_STR_LIST(st,f_n,f_l,s,l,a,h) { \
247 .type = isl_arg_str_list, \
250 .argument_name = a, \
251 .offset = offsetof(st, f_l), \
253 .u = { .str_list = { .offset_n = offsetof(st, f_n) } } \
255 #define _ISL_ARG_CHILD(o,l,c,h,fl) { \
256 .type = isl_arg_child, \
261 .u = { .child = { .child = c } } \
263 #define ISL_ARG_CHILD(st,f,l,c,h) \
264 _ISL_ARG_CHILD(offsetof(st, f),l,c,h,0)
265 #define ISL_ARG_GROUP_F(l,c,h,fl) \
266 _ISL_ARG_CHILD(-1,l,c,h,fl)
267 #define ISL_ARG_GROUP(l,c,h) \
268 ISL_ARG_GROUP_F(l,c,h,0)
269 #define ISL_ARG_FLAGS(st,f,s,l,c,d,h) { \
270 .type = isl_arg_flags, \
273 .offset = offsetof(st, f), \
275 .u = { .flags = { .flags = c, .default_value = d } } \
277 #define ISL_ARG_USER(st,f,i,c) { \
278 .type = isl_arg_user, \
279 .offset = offsetof(st, f), \
280 .u = { .user = { .init = i, .clear = c} } \
282 #define ISL_ARG_VERSION(print) { \
283 .type = isl_arg_version, \
284 .u = { .version = { .print_version = print } } \
287 #define ISL_ARG_ALL (1 << 0)
288 #define ISL_ARG_SKIP_HELP (1 << 1)
290 void isl_args_set_defaults(struct isl_args
*args
, void *opt
);
291 void isl_args_free(struct isl_args
*args
, void *opt
);
292 int isl_args_parse(struct isl_args
*args
, int argc
, char **argv
, void *opt
,
295 #define ISL_ARG_DECL(prefix,st,args) \
296 extern struct isl_args args; \
297 st *prefix ## _new_with_defaults(void); \
298 void prefix ## _free(st *opt); \
299 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags);
301 #define ISL_ARG_DEF(prefix,st,args) \
302 st *prefix ## _new_with_defaults() \
304 st *opt = (st *)calloc(1, sizeof(st)); \
306 isl_args_set_defaults(&(args), opt); \
310 void prefix ## _free(st *opt) \
312 isl_args_free(&(args), opt); \
315 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags) \
317 return isl_args_parse(&(args), argc, argv, opt, flags); \
320 #if defined(__cplusplus)