summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Verdoolaege <skimo@kotnet.org>2010-11-12 17:09:53 +0100
committerSven Verdoolaege <skimo@kotnet.org>2010-11-26 15:59:44 +0100
commitbb85e51e01db2d878bed82e60de32106ac413e8f (patch)
tree635ad120c70f6396870f126b81c0b9d15f7084b7
parent8ab4820699516aabfe3507a759e2a12cf31dbc78 (diff)
downloadisl-bb85e51e01db2d878bed82e60de32106ac413e8f.tar.gz
isl-bb85e51e01db2d878bed82e60de32106ac413e8f.tar.bz2
isl-bb85e51e01db2d878bed82e60de32106ac413e8f.zip
isl_arg_parse: support int options
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
-rw-r--r--include/isl/arg.h16
-rw-r--r--isl_arg.c55
2 files changed, 71 insertions, 0 deletions
diff --git a/include/isl/arg.h b/include/isl/arg.h
index ad095981..d34a1116 100644
--- a/include/isl/arg.h
+++ b/include/isl/arg.h
@@ -36,6 +36,7 @@ enum isl_arg_type {
isl_arg_child,
isl_arg_choice,
isl_arg_flags,
+ isl_arg_int,
isl_arg_user,
isl_arg_long,
isl_arg_ulong,
@@ -70,6 +71,9 @@ struct isl_arg {
int (*set)(void *opt, unsigned val);
} b;
struct {
+ int default_value;
+ } i;
+ struct {
long default_value;
long default_selected;
int (*set)(void *opt, long val);
@@ -148,6 +152,18 @@ struct isl_arg {
_ISL_ARG_BOOL_F(-1,s,l,setter,0,h,fl)
#define ISL_ARG_PHANTOM_BOOL(s,l,setter,h) \
ISL_ARG_PHANTOM_BOOL_F(s,l,setter,h,0)
+#define ISL_ARG_INT_F(st,f,s,l,a,d,h,fl) { \
+ .type = isl_arg_int, \
+ .short_name = s, \
+ .long_name = l, \
+ .argument_name = a, \
+ .offset = offsetof(st, f), \
+ .help_msg = h, \
+ .flags = fl, \
+ .u = { .ul = { .default_value = d } } \
+},
+#define ISL_ARG_INT(st,f,s,l,a,d,h) \
+ ISL_ARG_INT_F(st,f,s,l,a,d,h,0)
#define ISL_ARG_LONG(st,f,s,lo,d,h) { \
.type = isl_arg_long, \
.short_name = s, \
diff --git a/isl_arg.c b/isl_arg.c
index b19778ae..8df787b3 100644
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -51,6 +51,11 @@ static void set_default_user(struct isl_arg *arg, void *opt)
arg->u.user.init(((char *)opt) + arg->offset);
}
+static void set_default_int(struct isl_arg *arg, void *opt)
+{
+ *(int *)(((char *)opt) + arg->offset) = arg->u.i.default_value;
+}
+
static void set_default_long(struct isl_arg *arg, void *opt)
{
*(long *)(((char *)opt) + arg->offset) = arg->u.l.default_value;
@@ -90,6 +95,9 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
case isl_arg_user:
set_default_user(&arg[i], opt);
break;
+ case isl_arg_int:
+ set_default_int(&arg[i], opt);
+ break;
case isl_arg_long:
set_default_long(&arg[i], opt);
break;
@@ -133,6 +141,7 @@ void isl_arg_free(struct isl_arg *arg, void *opt)
case isl_arg_bool:
case isl_arg_choice:
case isl_arg_flags:
+ case isl_arg_int:
case isl_arg_long:
case isl_arg_ulong:
case isl_arg_version:
@@ -382,6 +391,18 @@ static int print_argument_name(struct isl_arg *decl, const char *name, int pos)
return pos + 3 + strlen(name);
}
+static void print_int_help(struct isl_arg *decl, const char *prefix)
+{
+ int pos;
+ char val[20];
+ pos = print_arg_help(decl, prefix, 0);
+ pos = print_argument_name(decl, decl->argument_name, pos);
+ pos = print_help_msg(decl, pos);
+ snprintf(val, sizeof(val), "%d", decl->u.i.default_value);
+ print_default(decl, val, pos);
+ printf("\n");
+}
+
static void print_long_help(struct isl_arg *decl, const char *prefix)
{
int pos;
@@ -439,6 +460,9 @@ static void print_help(struct isl_arg *arg, const char *prefix)
case isl_arg_bool:
print_bool_help(&arg[i], prefix);
break;
+ case isl_arg_int:
+ print_int_help(&arg[i], prefix);
+ break;
case isl_arg_long:
print_long_help(&arg[i], prefix);
break;
@@ -772,6 +796,34 @@ static int parse_str_option(struct isl_arg *decl, char **arg,
return 0;
}
+static int parse_int_option(struct isl_arg *decl, char **arg,
+ const char *prefix, void *opt)
+{
+ int has_argument;
+ const char *val;
+ char *endptr;
+ int *p = (int *)(((char *)opt) + decl->offset);
+
+ val = skip_name(decl, arg[0], prefix, 0, &has_argument);
+ if (!val)
+ return 0;
+
+ if (has_argument) {
+ *p = atoi(val);
+ return 1;
+ }
+
+ if (arg[1]) {
+ int i = strtol(arg[1], &endptr, 0);
+ if (*endptr == '\0') {
+ *p = i;
+ return 2;
+ }
+ }
+
+ return 0;
+}
+
static int parse_long_option(struct isl_arg *decl, char **arg,
const char *prefix, void *opt)
{
@@ -866,6 +918,9 @@ static int parse_option(struct isl_arg *decl, char **arg,
case isl_arg_flags:
parsed = parse_flags_option(&decl[i], arg, prefix, opt);
break;
+ case isl_arg_int:
+ parsed = parse_int_option(&decl[i], arg, prefix, opt);
+ break;
case isl_arg_long:
parsed = parse_long_option(&decl[i], arg, prefix, opt);
break;