isl_arg_parse: support footer
authorSven Verdoolaege <skimo@kotnet.org>
Sun, 21 Nov 2010 09:09:05 +0000 (10:09 +0100)
committerSven Verdoolaege <skimo@kotnet.org>
Fri, 26 Nov 2010 14:59:44 +0000 (15:59 +0100)
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
include/isl/arg.h
isl_arg.c

index 86cac48..d37e495 100644 (file)
@@ -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,                                                \
index 1f2e63b..f4824e6 100644 (file)
--- 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: