diff options
author | Marcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de> | 2017-07-18 18:08:25 +0200 |
---|---|---|
committer | Marcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de> | 2017-07-18 18:08:25 +0200 |
commit | f17862d47d184d7f6ef883778cf63801365599a0 (patch) | |
tree | aaa3c994a5b616e113ab1037142dcf9a07382aef | |
parent | gpgscm,w32: Fix testing for absolute paths. (diff) | |
download | gnupg2-f17862d47d184d7f6ef883778cf63801365599a0.tar.xz gnupg2-f17862d47d184d7f6ef883778cf63801365599a0.zip |
common: Allow abbreviations of standard options.
* argparse.h (ARGPARSE_SHORTOPT_HELP, ARGPARSE_SHORTOPT_VERSION,
ARGPARSE_SHORTOPT_WARRANTY, ARGPARSE_SHORTOPT_DUMP_OPTIONS): New
macros.
(ARGPARSE_end): Add some placeholders for standard options.
* argparse.c (arg_parse): Fill in missing standard options so
default machinery works. Check for standard options in new way.
Do not write out standard options for --dump-options.
Signed-off-by: Marcus Brinkmann <mb@g10code.com>
GnuPG-bug-id: 1747
-rw-r--r-- | common/argparse.c | 45 | ||||
-rw-r--r-- | common/argparse.h | 14 |
2 files changed, 52 insertions, 7 deletions
diff --git a/common/argparse.c b/common/argparse.c index 2540894f0..590e6e944 100644 --- a/common/argparse.c +++ b/common/argparse.c @@ -918,6 +918,41 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) char *s, *s2; int i; + /* Fill in missing standard options: help, version, warranty and dump-options. */ + ARGPARSE_OPTS help_opt = ARGPARSE_s_n(ARGPARSE_SHORTOPT_HELP, "help", "@"); + ARGPARSE_OPTS version_opt = ARGPARSE_s_n(ARGPARSE_SHORTOPT_VERSION, "version", "@"); + ARGPARSE_OPTS warranty_opt = ARGPARSE_s_n(ARGPARSE_SHORTOPT_WARRANTY, "warranty", "@"); + ARGPARSE_OPTS dump_options_opt = ARGPARSE_s_n(ARGPARSE_SHORTOPT_DUMP_OPTIONS, "dump-options", "@"); + int seen_help = 0; + int seen_version = 0; + int seen_warranty = 0; + int seen_dump_options = 0; + + i = 0; + while (opts[i].short_opt) + { + if (opts[i].long_opt) + { + if (!strcmp(opts[i].long_opt, help_opt.long_opt)) + seen_help = 1; + else if (!strcmp(opts[i].long_opt, version_opt.long_opt)) + seen_version = 1; + else if (!strcmp(opts[i].long_opt, warranty_opt.long_opt)) + seen_warranty = 1; + else if (!strcmp(opts[i].long_opt, dump_options_opt.long_opt)) + seen_dump_options = 1; + } + i++; + } + if (! seen_help) + opts[i++] = help_opt; + if (! seen_version) + opts[i++] = version_opt; + if (! seen_warranty) + opts[i++] = warranty_opt; + if (! seen_dump_options) + opts[i++] = dump_options_opt; + initialize( arg, NULL, NULL ); argc = *arg->argc; argv = *arg->argv; @@ -974,9 +1009,9 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) if ( argpos ) *argpos = '='; - if ( i < 0 && !strcmp ( "help", s+2) ) + if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_HELP) show_help (opts, arg->flags); - else if ( i < 0 && !strcmp ( "version", s+2) ) + else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_VERSION) { if (!(arg->flags & ARGPARSE_FLAG_NOVERSION)) { @@ -984,20 +1019,18 @@ arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) exit(0); } } - else if ( i < 0 && !strcmp( "warranty", s+2)) + else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_WARRANTY) { writestrings (0, strusage (16), "\n", NULL); exit (0); } - else if ( i < 0 && !strcmp( "dump-options", s+2) ) + else if (i > 0 && opts[i].short_opt == ARGPARSE_SHORTOPT_DUMP_OPTIONS) { for (i=0; opts[i].short_opt; i++ ) { if (opts[i].long_opt && !(opts[i].flags & ARGPARSE_OPT_IGNORE)) writestrings (0, "--", opts[i].long_opt, "\n", NULL); } - writestrings (0, "--dump-options\n--help\n--version\n--warranty\n", - NULL); exit (0); } diff --git a/common/argparse.h b/common/argparse.h index d75b49fbd..cdd18d9a2 100644 --- a/common/argparse.h +++ b/common/argparse.h @@ -71,6 +71,12 @@ typedef struct const char *description; /* Optional option description. */ } ARGPARSE_OPTS; +/* Short options. */ +#define ARGPARSE_SHORTOPT_HELP 32768 +#define ARGPARSE_SHORTOPT_VERSION 32769 +#define ARGPARSE_SHORTOPT_WARRANTY 32770 +#define ARGPARSE_SHORTOPT_DUMP_OPTIONS 32771 + /* Global flags (ARGPARSE_ARGS). */ #define ARGPARSE_FLAG_KEEP 1 /* Do not remove options form argv. */ @@ -169,7 +175,13 @@ typedef struct #define ARGPARSE_group(s,d) \ { (s), NULL, 0, (d) } -#define ARGPARSE_end() { 0, NULL, 0, NULL } +/* Placeholder options for help, version, warranty and dump-options. See arg_parse(). */ +#define ARGPARSE_end() \ + { 0, NULL, 0, NULL }, \ + { 0, NULL, 0, NULL }, \ + { 0, NULL, 0, NULL }, \ + { 0, NULL, 0, NULL }, \ + { 0, NULL, 0, NULL } /* Other constants. */ |