summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2020-02-18 16:47:05 +0100
committerWerner Koch <wk@gnupg.org>2020-02-18 16:50:46 +0100
commit933eb9346a84c87f83f77d990be2f66e2f7b62e7 (patch)
tree4560fa9440d0e98f8662eb737c56d40d4499dab9 /tests
parent5742b8eaf3fa9cda3dfb6b3ad0fea7485fff1a12 (diff)
downloadlibgpg-error-933eb9346a84c87f83f77d990be2f66e2f7b62e7.tar.gz
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 <wk@gnupg.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/etc/t-argparse.conf36
-rw-r--r--tests/t-argparse.c42
-rw-r--r--tests/t-argparse.conf8
4 files changed, 80 insertions, 8 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 39ca241..2d199da 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -25,6 +25,8 @@ else
extra_includes =
endif
+EXTRA_DIST = t-argparse.conf etc/t-argparse.conf
+
gpg_error_lib = ../src/libgpg-error.la
TESTS = t-version t-strerror t-syserror t-lock t-printf t-poll t-b64 \
diff --git a/tests/etc/t-argparse.conf b/tests/etc/t-argparse.conf
new file mode 100644
index 0000000..87bff72
--- /dev/null
+++ b/tests/etc/t-argparse.conf
@@ -0,0 +1,36 @@
+# Global test config file for t-argparse
+
+# Options applied to all user's config files
+#verbose
+
+#[user :staff]
+# These option are applied to all users the group staff up until the
+# next [user statement]
+
+#[+force]
+
+#[ignore]
+
+# The compliance is set immutable for these users
+verbose
+
+#no-verbose
+
+
+# (parsing does not stop for a group)
+#[user wk]
+# Options for user wk
+
+# Change the immutable flag back to mutable.
+#[] compliance gnupg
+
+# Default key for wk
+my-option 42
+
+# Parsing stops for user WK here.
+
+#[user *]
+# Options for all users which have no specific user sections above
+
+# The default algorithm for new keys is set to this.
+a-long-option
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;
}
diff --git a/tests/t-argparse.conf b/tests/t-argparse.conf
new file mode 100644
index 0000000..0bbdd3e
--- /dev/null
+++ b/tests/t-argparse.conf
@@ -0,0 +1,8 @@
+# User test config file for t-argparse
+
+# Options applied to all user's config files
+echo
+
+my-option 4711
+
+verbose