isl_options_parse: print help message
[platform/upstream/isl.git] / include / isl_arg.h
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #ifndef ISL_ARG_H
11 #define ISL_ARG_H
12
13 #include <stddef.h>
14 #include <stdlib.h>
15
16 #if defined(__cplusplus)
17 extern "C" {
18 #endif
19
20 struct isl_arg_choice {
21         const char      *name;
22         unsigned         value;
23 };
24
25 enum isl_arg_type {
26         isl_arg_end,
27         isl_arg_choice,
28         isl_arg_bool,
29         isl_arg_child
30 };
31
32 struct isl_arg {
33         enum isl_arg_type        type;
34         char                     short_name;
35         const char              *long_name;
36         size_t                   offset;
37         const char              *help_msg;
38         union {
39         struct {
40                 struct isl_arg_choice   *choice;
41                 unsigned                 default_value;
42         } choice;
43         struct {
44                 unsigned                 default_value;
45         } b;
46         struct {
47                 struct isl_arg          *child;
48                 size_t                   size;
49         } child;
50         } u;
51 };
52
53 #define ISL_ARG_CHOICE(st,f,s,l,c,d,h)  {                               \
54         .type = isl_arg_choice,                                         \
55         .short_name = s,                                                \
56         .long_name = l,                                                 \
57         .offset = offsetof(st, f),                                      \
58         .help_msg = h,                                                  \
59         .u = { .choice = { .choice = c, .default_value = d } }          \
60 },
61 #define ISL_ARG_BOOL(st,f,s,l,d,h)      {                               \
62         .type = isl_arg_bool,                                           \
63         .short_name = s,                                                \
64         .long_name = l,                                                 \
65         .offset = offsetof(st, f),                                      \
66         .help_msg = h,                                                  \
67         .u = { .b = { .default_value = d } }                            \
68 },
69 #define ISL_ARG_CHILD(st,f,l,c,h)       {                               \
70         .type = isl_arg_child,                                          \
71         .long_name = l,                                                 \
72         .offset = offsetof(st, f),                                      \
73         .help_msg = h,                                                  \
74         .u = { .child = { .child = c, .size = sizeof(*((st *)NULL)->f) } }\
75 },
76 #define ISL_ARG_END     { isl_arg_end }
77
78 #define ISL_ARG_ALL     (1 << 0)
79
80 void isl_arg_set_defaults(struct isl_arg *arg, void *opt);
81 int isl_arg_parse(struct isl_arg *arg, int argc, char **argv, void *opt,
82         unsigned flags);
83
84 #define ISL_ARG_DECL(prefix,st,arg)                                     \
85 st *prefix ## _new_with_defaults();                                     \
86 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags);
87
88 #define ISL_ARG_DEF(prefix,st,arg)                                      \
89 st *prefix ## _new_with_defaults()                                      \
90 {                                                                       \
91         st *opt = (st *)calloc(1, sizeof(st));                          \
92         if (opt)                                                        \
93                 isl_arg_set_defaults(arg, opt);                         \
94         return opt;                                                     \
95 }                                                                       \
96                                                                         \
97 int prefix ## _parse(st *opt, int argc, char **argv, unsigned flags)    \
98 {                                                                       \
99         return isl_arg_parse(arg, argc, argv, opt, flags);              \
100 }
101
102 #if defined(__cplusplus)
103 }
104 #endif
105
106 #endif