summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Verdoolaege <skimo@kotnet.org>2010-11-12 16:55:41 +0100
committerSven Verdoolaege <skimo@kotnet.org>2010-11-26 15:55:25 +0100
commit3c552a6fa671bf12cce230fe368d9a785f55b6a8 (patch)
tree3b4e55e1f9398d70246088e55bb984d379e69523
parent43811040ef78e306f72a2eb63596cf7d212026d9 (diff)
downloadisl-3c552a6fa671bf12cce230fe368d9a785f55b6a8.tar.gz
isl-3c552a6fa671bf12cce230fe368d9a785f55b6a8.tar.bz2
isl-3c552a6fa671bf12cce230fe368d9a785f55b6a8.zip
isl_arg_parse: support aliases
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
-rw-r--r--include/isl/arg.h5
-rw-r--r--isl_arg.c32
2 files changed, 34 insertions, 3 deletions
diff --git a/include/isl/arg.h b/include/isl/arg.h
index f8e49db1..d64b3db5 100644
--- a/include/isl/arg.h
+++ b/include/isl/arg.h
@@ -30,6 +30,7 @@ struct isl_arg_flags {
enum isl_arg_type {
isl_arg_end,
+ isl_arg_alias,
isl_arg_arg,
isl_arg_bool,
isl_arg_child,
@@ -90,6 +91,10 @@ struct isl_arg {
} u;
};
+#define ISL_ARG_ALIAS(l) { \
+ .type = isl_arg_alias, \
+ .long_name = l, \
+},
#define ISL_ARG_ARG(st,f,a,d) { \
.type = isl_arg_arg, \
.argument_name = a, \
diff --git a/isl_arg.c b/isl_arg.c
index 5f1950f5..eef54a6c 100644
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -100,6 +100,7 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
case isl_arg_str:
set_default_str(&arg[i], opt);
break;
+ case isl_arg_alias:
case isl_arg_version:
case isl_arg_end:
break;
@@ -128,6 +129,7 @@ void isl_arg_free(struct isl_arg *arg, void *opt)
if (arg[i].u.user.clear)
arg[i].u.user.clear(((char *)opt) + arg[i].offset);
break;
+ case isl_arg_alias:
case isl_arg_bool:
case isl_arg_choice:
case isl_arg_flags:
@@ -168,6 +170,17 @@ static int print_arg_help(struct isl_arg *decl, const char *prefix, int no)
printf("%s", decl->long_name);
len += strlen(decl->long_name);
+ while ((++decl)->type == isl_arg_alias) {
+ printf(", --");
+ len += 4;
+ if (no) {
+ printf("no-");
+ len += 3;
+ }
+ printf("%s", decl->long_name);
+ len += strlen(decl->long_name);
+ }
+
return len;
}
@@ -428,6 +441,7 @@ static void print_help(struct isl_arg *arg, const char *prefix)
case isl_arg_str:
print_str_help(&arg[i], prefix);
break;
+ case isl_arg_alias:
case isl_arg_version:
case isl_arg_arg:
case isl_arg_child:
@@ -502,6 +516,18 @@ static void print_help_and_exit(struct isl_arg *arg, const char *prog)
exit(0);
}
+static int match_long_name(struct isl_arg *decl,
+ const char *start, const char *end)
+{
+ do {
+ if (end - start == strlen(decl->long_name) &&
+ !strncmp(start, decl->long_name, end - start))
+ return 1;
+ } while ((++decl)->type == isl_arg_alias);
+
+ return 0;
+}
+
static const char *skip_name(struct isl_arg *decl, const char *arg,
const char *prefix, int need_argument, int *has_argument)
{
@@ -538,8 +564,7 @@ static const char *skip_name(struct isl_arg *decl, const char *arg,
name += prefix_len + 1;
}
- if (end - name != strlen(decl->long_name) ||
- strncmp(name, decl->long_name, end - name))
+ if (!match_long_name(decl, name, end))
return NULL;
return equal ? equal + 1 : end;
@@ -679,7 +704,7 @@ static int parse_bool_option(struct isl_arg *decl, const char *arg,
arg += prefix_len + 1;
}
- if (!strcmp(arg, decl->long_name)) {
+ if (match_long_name(decl, arg, arg + strlen(arg))) {
if (decl->u.b.set)
decl->u.b.set(opt, 0);
else if (decl->offset != (size_t) -1)
@@ -826,6 +851,7 @@ static int parse_option(struct isl_arg *decl, char **arg,
case isl_arg_child:
parsed = parse_child_option(&decl[i], arg, opt);
break;
+ case isl_arg_alias:
case isl_arg_arg:
case isl_arg_user:
case isl_arg_version: