diff options
author | Jon Spillett <jon.spillett@oracle.com> | 2019-09-19 21:14:21 +1000 |
---|---|---|
committer | Shane Lontis <shane.lontis@oracle.com> | 2019-09-19 21:14:21 +1000 |
commit | 4ed838915be263b3ebf847b6ada2ab2d3debec4c (patch) | |
tree | c6b311ad2ed74f58538c8276efe6564144474f61 /apps/lib/opt.c | |
parent | 3837c202b5e91f009d1508a8f3608c94515ca776 (diff) | |
download | openssl-new-4ed838915be263b3ebf847b6ada2ab2d3debec4c.tar.gz |
Add option grouping capability to apps
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9920)
Diffstat (limited to 'apps/lib/opt.c')
-rw-r--r-- | apps/lib/opt.c | 72 |
1 files changed, 42 insertions, 30 deletions
diff --git a/apps/lib/opt.c b/apps/lib/opt.c index c2a5878ef6..44d2570ae7 100644 --- a/apps/lib/opt.c +++ b/apps/lib/opt.c @@ -28,6 +28,7 @@ #define MAX_OPT_HELP_WIDTH 30 const char OPT_HELP_STR[] = "--"; const char OPT_MORE_STR[] = "---"; +const char OPT_SECTION_STR[] = "----"; /* Our state */ static char **argv; @@ -133,7 +134,8 @@ char *opt_init(int ac, char **av, const OPTIONS *o) int duplicated, i; #endif - if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR) + if (o->name == OPT_HELP_STR || o->name == OPT_MORE_STR || + o->name == OPT_SECTION_STR) continue; #ifndef NDEBUG i = o->valtype; @@ -832,40 +834,16 @@ static const char *valtype2param(const OPTIONS *o) return "parm"; } -void opt_help(const OPTIONS *list) +void opt_print(const OPTIONS *o, int width) { - const OPTIONS *o; - int i; - int standard_prolog; - int width = 5; + const char* help; char start[80 + 1]; char *p; - const char *help; - - /* Starts with its own help message? */ - standard_prolog = list[0].name != OPT_HELP_STR; - - /* Find the widest help. */ - for (o = list; o->name; o++) { - if (o->name == OPT_MORE_STR) - continue; - i = 2 + (int)strlen(o->name); - if (o->valtype != '-') - i += 1 + strlen(valtype2param(o)); - if (i < MAX_OPT_HELP_WIDTH && i > width) - width = i; - OPENSSL_assert(i < (int)sizeof(start)); - } - - if (standard_prolog) - opt_printf_stderr("Usage: %s [options]\nValid options are:\n", prog); - /* Now let's print. */ - for (o = list; o->name; o++) { help = o->helpstr ? o->helpstr : "(No additional info)"; - if (o->name == OPT_HELP_STR) { + if (o->name == OPT_HELP_STR || o->name == OPT_SECTION_STR) { opt_printf_stderr(help, prog); - continue; + return; } /* Pad out prefix */ @@ -876,7 +854,7 @@ void opt_help(const OPTIONS *list) /* Continuation of previous line; pad and print. */ start[width] = '\0'; opt_printf_stderr("%s %s\n", start, help); - continue; + return; } /* Build up the "-flag [param]" part. */ @@ -899,6 +877,40 @@ void opt_help(const OPTIONS *list) } start[width] = '\0'; opt_printf_stderr("%s %s\n", start, help); +} + +void opt_help(const OPTIONS *list) +{ + const OPTIONS *o; + int i; + int standard_prolog; + int width = 5; + char start[80 + 1]; + + /* Starts with its own help message? */ + standard_prolog = list[0].name != OPT_HELP_STR; + + /* Find the widest help. */ + for (o = list; o->name; o++) { + if (o->name == OPT_MORE_STR) + continue; + i = 2 + (int)strlen(o->name); + if (o->valtype != '-') + i += 1 + strlen(valtype2param(o)); + if (i < MAX_OPT_HELP_WIDTH && i > width) + width = i; + OPENSSL_assert(i < (int)sizeof(start)); + } + + if (standard_prolog) { + opt_printf_stderr("Usage: %s [options]\n", prog); + if (list[0].name != OPT_SECTION_STR) + opt_printf_stderr("Valid options are:\n", prog); + } + + /* Now let's print. */ + for (o = list; o->name; o++) { + opt_print(o, width); } } |