diff options
author | Werner Koch <[email protected]> | 2020-02-18 15:47:05 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2020-02-18 15:50:46 +0000 |
commit | 933eb9346a84c87f83f77d990be2f66e2f7b62e7 (patch) | |
tree | 4560fa9440d0e98f8662eb737c56d40d4499dab9 /tests/t-argparse.c | |
parent | core: Add gpgrt_fnameconcat and gpgrt_absfnameconcat. (diff) | |
download | libgpg-error-933eb9346a84c87f83f77d990be2f66e2f7b62e7.tar.gz libgpg-error-933eb9346a84c87f83f77d990be2f66e2f7b62e7.zip |
core: Add a high level option/argument parser.
* gpg-error.h.in (GPGRT_CONFDIR_USER, GPGRT_CONFDIR_SYS): New consts.
(ARGPARSE_FLAG_SYS, ARGPARSE_FLAG_USER, ARGPARSE_FLAG_VERBOSE)
(ARGPARSE_NO_CONFFILE, ARGPARSE_CONFFILE, ARGPARSE_OPT_CONFFILE): New
consts.
(ARGPARSE_conffile, ARGPARSE_noconffile): New macros.
(gpgrt_set_confdir): New func.
(gpgrt_argparser): New func.
* src/argparse.c (confdir): New var.
(enum argparser_states): New.
(struct _gpgrt_argparse_internal_s): Add a couple of new fields.
(initialize): Init them.
(any_opt_conffile): New.
(_gpgrt_argparser): New.
(_gpgrt_set_confdir): New.
* src/visibility.c (gpgrt_argparser): New.
(gpgrt_set_confdir): New.
* src/gpg-error.def.in, src/gpg-error.vers: Add those functions.
* tests/t-argparse.c (main): Reworked.
* tests/etc/t-argparse.conf: New file.
* tests/t-argparse.conf: New file.
--
gpgrt_argparser is a high level version of gpgrt_argparse. It handles
reading of configuration files internally and allows allows for a
global configuration file. The design is so that it minimizes the
work to replace the existing option parsing in gpg and friends by this
one and to allow global configuration files for them.
This is the just the basic code which should allow replacement of the
parsers. A forthcoming patch will implement flags for options given
in the global config file.
GnuPG-bug-id: 4788
Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'tests/t-argparse.c')
-rw-r--r-- | tests/t-argparse.c | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/tests/t-argparse.c b/tests/t-argparse.c index 277d860..b2b6e51 100644 --- a/tests/t-argparse.c +++ b/tests/t-argparse.c @@ -1,5 +1,5 @@ /* t-argparse.c - Check the argparse API - * Copyright (C) 2018 g10 Code GmbH + * Copyright (C) 2018, 2020 g10 Code GmbH * * This file is part of Libgpg-error. * @@ -27,7 +27,8 @@ #include <string.h> #include <assert.h> -#include "../src/gpg-error.h" +#define PGM "t-argparse" +#include "t-common.h" static struct { @@ -49,7 +50,7 @@ my_strusage (int level) switch (level) { - case 9: p = "GPL-2.0-or-later"; break; + case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "t-argparse"; break; @@ -74,26 +75,47 @@ main (int argc, char **argv) ARGPARSE_s_n('s', "street","|Straße|set the name of the street to Straße"), ARGPARSE_o_i('m', "my-option", 0), ARGPARSE_s_n(500, "a-long-option", 0 ), + ARGPARSE_conffile(501, "options", "|FILE|read options from FILE"), + ARGPARSE_noconffile(502, "no-options", "Ignore conf files"), ARGPARSE_end() }; gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL | ARGPARSE_FLAG_MIXED - | ARGPARSE_FLAG_ONEDASH) }; + | ARGPARSE_FLAG_ONEDASH + | ARGPARSE_FLAG_SYS + | ARGPARSE_FLAG_USER + /* | ARGPARSE_FLAG_VERBOSE */ + ) }; int i; + const char *srcdir; gpgrt_set_strusage (my_strusage); - - - while (gpgrt_argparse (NULL, &pargs, opts)) + srcdir = getenv ("srcdir"); + if (!srcdir) + srcdir = "."; + gpgrt_set_confdir (GPGRT_CONFDIR_USER, srcdir); + { + char *p = gpgrt_fnameconcat (srcdir, "etc", NULL); + gpgrt_set_confdir (GPGRT_CONFDIR_SYS, p); + xfree (p); + } + + while (gpgrt_argparser (&pargs, opts, PGM".conf")) { + /* printf ("got option %d\n", pargs.r_opt); */ switch (pargs.r_opt) { + case ARGPARSE_CONFFILE: + printf ("current conffile='%s'\n", + pargs.r.ret_str? pargs.r.ret_str: "[cmdline]"); + break; case ARGPARSE_IS_ARG : printf ("arg='%s'\n", pargs.r.ret_str); break; + case 'v': opt.verbose++; break; case 'e': opt.echo++; break; - case 'd': opt.debug++; break; + case 'd': opt.debug++; debug=1;break; case 'o': opt.outfile = pargs.r.ret_str; break; case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; @@ -122,5 +144,9 @@ main (int argc, char **argv) gpgrt_argparse (NULL, &pargs, NULL); + (void)show; + (void)fail; + (void)die; + return 0; } |