summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven Verdoolaege <skimo@kotnet.org>2010-11-21 10:09:05 +0100
committerSven Verdoolaege <skimo@kotnet.org>2010-11-26 15:59:44 +0100
commit43729fde44c138fd03d198dd1121763d5f786531 (patch)
treea1ba9e4edfbd9a57ee4b793ccec3da29b1d2477a
parentaac1178df81966524577a3cd2790ebb7f8ada205 (diff)
downloadisl-43729fde44c138fd03d198dd1121763d5f786531.tar.gz
isl-43729fde44c138fd03d198dd1121763d5f786531.tar.bz2
isl-43729fde44c138fd03d198dd1121763d5f786531.zip
isl_arg_parse: support footer
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
-rw-r--r--include/isl/arg.h5
-rw-r--r--isl_arg.c41
2 files changed, 33 insertions, 13 deletions
diff --git a/include/isl/arg.h b/include/isl/arg.h
index 86cac481..d37e4959 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_footer,
isl_arg_int,
isl_arg_user,
isl_arg_long,
@@ -108,6 +109,10 @@ struct isl_arg {
.offset = offsetof(st, f), \
.u = { .str = { .default_value = d } } \
},
+#define ISL_ARG_FOOTER(h) { \
+ .type = isl_arg_footer, \
+ .help_msg = h, \
+},
#define ISL_ARG_CHOICE(st,f,s,l,c,d,h) { \
.type = isl_arg_choice, \
.short_name = s, \
diff --git a/isl_arg.c b/isl_arg.c
index 1f2e63b7..f4824e63 100644
--- a/isl_arg.c
+++ b/isl_arg.c
@@ -114,6 +114,7 @@ void isl_arg_set_defaults(struct isl_arg *arg, void *opt)
set_default_str(&arg[i], opt);
break;
case isl_arg_alias:
+ case isl_arg_footer:
case isl_arg_version:
case isl_arg_end:
break;
@@ -150,6 +151,7 @@ static void free_args(struct isl_arg *arg, void *opt)
case isl_arg_long:
case isl_arg_ulong:
case isl_arg_version:
+ case isl_arg_footer:
case isl_arg_end:
break;
}
@@ -220,40 +222,44 @@ const void *isl_memrchr(const void *s, int c, size_t n)
return NULL;
}
-static int print_help_msg(struct isl_arg *decl, int pos)
+static int wrap_msg(const char *s, int indent, int pos)
{
int len;
- const char *s;
-
- if (!decl->help_msg)
- return pos;
+ int wrap_len = 75 - indent;
- if (pos >= 29)
- printf("\n%30s", "");
+ if (pos + 1 >= indent)
+ printf("\n%*s", indent, "");
else
- printf("%*s", 30 - pos, "");
+ printf("%*s", indent - pos, "");
- s = decl->help_msg;
len = strlen(s);
- while (len > 45) {
- const char *space = isl_memrchr(s, ' ', 45);
+ while (len > wrap_len) {
+ const char *space = isl_memrchr(s, ' ', wrap_len);
int l;
if (!space)
- space = strchr(s + 45, ' ');
+ space = strchr(s + wrap_len, ' ');
if (!space)
break;
l = space - s;
printf("%.*s", l, s);
s = space + 1;
len -= l + 1;
- printf("\n%30s", "");
+ printf("\n%*s", indent, "");
}
printf("%s", s);
return len;
}
+static int print_help_msg(struct isl_arg *decl, int pos)
+{
+ if (!decl->help_msg)
+ return pos;
+
+ return wrap_msg(decl->help_msg, 30, pos);
+}
+
static void print_default(struct isl_arg *decl, const char *def, int pos)
{
int i;
@@ -496,6 +502,7 @@ static void print_help(struct isl_arg *arg, const char *prefix)
case isl_arg_alias:
case isl_arg_version:
case isl_arg_arg:
+ case isl_arg_footer:
case isl_arg_child:
case isl_arg_user:
case isl_arg_end:
@@ -569,6 +576,13 @@ static void print_help_and_exit(struct isl_arg *arg, const char *prog)
printf(" -V, --version\n");
print_bool_help(help_arg, NULL);
+ for (i = 0; arg[i].type != isl_arg_end; ++i) {
+ if (arg[i].type != isl_arg_footer)
+ continue;
+ wrap_msg(arg[i].help_msg, 0, 0);
+ printf("\n");
+ }
+
exit(0);
}
@@ -969,6 +983,7 @@ static int parse_option(struct isl_arg *decl, char **arg,
break;
case isl_arg_alias:
case isl_arg_arg:
+ case isl_arg_footer:
case isl_arg_user:
case isl_arg_version:
case isl_arg_end: