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
This commit is contained in:
Marcus Brinkmann 2017-07-18 18:08:25 +02:00
parent 2e1342b78b
commit f17862d47d
2 changed files with 52 additions and 7 deletions

View File

@ -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);
}

View File

@ -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. */