From: Sven Verdoolaege Date: Thu, 26 Aug 2010 15:29:35 +0000 (+0200) Subject: argument parsing: add ISL_ARG_USER_OPT_CHOICE X-Git-Tag: isl-0.04~10 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c1dbabe09b2ea06c17f5dbbcb615d0ab5a3143d0;p=platform%2Fupstream%2Fisl.git argument parsing: add ISL_ARG_USER_OPT_CHOICE Signed-off-by: Sven Verdoolaege --- diff --git a/include/isl_arg.h b/include/isl_arg.h index c7bbc41..336a3c2 100644 --- a/include/isl_arg.h +++ b/include/isl_arg.h @@ -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, \ diff --git a/isl_arg.c b/isl_arg.c index a9529b5..17c67ff 100644 --- 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; }