argument parsing: add ISL_ARG_USER_OPT_CHOICE
authorSven Verdoolaege <skimo@kotnet.org>
Thu, 26 Aug 2010 15:29:35 +0000 (17:29 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Thu, 26 Aug 2010 15:29:35 +0000 (17:29 +0200)
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
include/isl_arg.h
isl_arg.c

index c7bbc41..336a3c2 100644 (file)
@@ -54,6 +54,7 @@ struct isl_arg {
                struct isl_arg_choice   *choice;
                unsigned                 default_value;
                unsigned                 default_selected;
+               int (*set)(void *opt, unsigned val);
        } choice;
        struct {
                struct isl_arg_flags    *flags;
@@ -100,7 +101,7 @@ struct isl_arg {
        .offset = offsetof(st, f),                                      \
        .help_msg = h,                                                  \
        .u = { .choice = { .choice = c, .default_value = d,             \
-                                       .default_selected = d } }       \
+                           .default_selected = d, .set = NULL } }      \
 },
 #define ISL_ARG_OPT_CHOICE(st,f,s,l,c,d,ds,h)  {                       \
        .type = isl_arg_choice,                                         \
@@ -109,7 +110,16 @@ struct isl_arg {
        .offset = offsetof(st, f),                                      \
        .help_msg = h,                                                  \
        .u = { .choice = { .choice = c, .default_value = d,             \
-                                       .default_selected = ds } }      \
+                           .default_selected = ds, .set = NULL } }     \
+},
+#define ISL_ARG_USER_OPT_CHOICE(st,f,s,l,c,setter,d,ds,h)      {       \
+       .type = isl_arg_choice,                                         \
+       .short_name = s,                                                \
+       .long_name = l,                                                 \
+       .offset = offsetof(st, f),                                      \
+       .help_msg = h,                                                  \
+       .u = { .choice = { .choice = c, .default_value = d,             \
+                           .default_selected = ds, .set = setter } }   \
 },
 #define ISL_ARG_BOOL(st,f,s,l,d,h)     {                               \
        .type = isl_arg_bool,                                           \
index a9529b5..17c67ff 100644 (file)
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -509,8 +509,11 @@ static int parse_choice_option(struct isl_arg *decl, char **arg,
                return 0;
 
        if (!has_argument && (!arg[1] || arg[1][0] == '-')) {
-               *(unsigned *)(((char *)opt) + decl->offset) =
-                       decl->u.choice.default_selected;
+               unsigned u = decl->u.choice.default_selected;
+               if (decl->u.choice.set)
+                       decl->u.choice.set(opt, u);
+               else
+                       *(unsigned *)(((char *)opt) + decl->offset) = u;
 
                return 1;
        }
@@ -519,11 +522,16 @@ static int parse_choice_option(struct isl_arg *decl, char **arg,
                choice = arg[1];
 
        for (i = 0; decl->u.choice.choice[i].name; ++i) {
+               unsigned u;
+
                if (strcmp(choice, decl->u.choice.choice[i].name))
                        continue;
 
-               *(unsigned *)(((char *)opt) + decl->offset) =
-                       decl->u.choice.choice[i].value;
+               u = decl->u.choice.choice[i].value;
+               if (decl->u.choice.set)
+                       decl->u.choice.set(opt, u);
+               else
+                       *(unsigned *)(((char *)opt) + decl->offset) = u;
 
                return has_argument ? 1 : 2;
        }