summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2018-03-21 16:23:29 +0100
committerWerner Koch <wk@gnupg.org>2018-03-21 16:23:29 +0100
commit13d6fbfd4360cb74588556a3a0058c4057d0bcf0 (patch)
treef8cecb6515b9056826f95a272560af1fcf68bb9b /tests
parent1beb22fc25f6622002ff592ac4d67400b1a3d3cb (diff)
downloadlibgpg-error-13d6fbfd4360cb74588556a3a0058c4057d0bcf0.tar.gz
core: Add the group of argparse functions.
* src/visibility.c (gpgrt_argparse): New. (gpgrt_strusage): New. (gpgrt_set_strusage): New. (gpgrt_set_usage_outfnc): New. (gpgrt_set_fixed_string_mapper): New. * src/gpg-error.def.in, src/gpg-error.vers: Add new functions. * src/gpg-error.h.in: Add ARGPARSE macros from the former argparse.h. (gpgrt_argparse_t): New. (gpgrt_opt_t): New. * configure.ac (AH_BOTTOM): Request argparse macros. * src/Makefile.am (libgpg_error_la_SOURCES): Add argparse.c. * src/argparse.h: Remove. * src/argparse.c: Revamp to fit into libgpg-error. (_gpgrt_argparse): New. (_gpgrt_usage): Rename from usage. (_gpgrt_strusage): Rename from strusage. Define two new levels and templates for three common licenses. (_gpgrt_set_strusage): Rename from set_strusage. (_gpgrt_set_usage_outfnc): New. (_gpgrt_set_fixed_string_mapper): New. * tests/t-argparse.c: New. -- These functions are in use by GnuPG and other software for 20 years and it makes sense to have them always available instead of copying and maintaing the source in several projects. Note that there are minor changes in the API compared to GnuPG. Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am3
-rw-r--r--tests/t-argparse.c125
2 files changed, 127 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a3c6cbd..cc3a2ad 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,7 +27,8 @@ endif
gpg_error_lib = ../src/libgpg-error.la
-TESTS = t-version t-strerror t-syserror t-lock t-printf t-poll t-b64dec
+TESTS = t-version t-strerror t-syserror t-lock t-printf t-poll t-b64dec \
+ t-argparse
AM_CPPFLAGS = -I$(top_builddir)/src $(extra_includes)
diff --git a/tests/t-argparse.c b/tests/t-argparse.c
new file mode 100644
index 0000000..177992a
--- /dev/null
+++ b/tests/t-argparse.c
@@ -0,0 +1,125 @@
+/* t-argparse.c - Check the argparse API
+ * Copyright (C) 2018 g10 Code GmbH
+ *
+ * This file is part of Libgpg-error.
+ *
+ * Libgpg-error is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgpg-error is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "../src/gpg-error.h"
+
+
+static struct {
+ int verbose;
+ int debug;
+ char *outfile;
+ char *crf;
+ int myopt;
+ int echo;
+ int a_long_one;
+} opt;
+
+
+
+static const char *
+my_strusage (int level)
+{
+ const char *p;
+
+ switch (level)
+ {
+ case 9: p = "GPL-2.0-or-later"; break;
+
+ case 11: p = "t-argparse"; break;
+
+ default: p = NULL;
+ }
+ return p;
+}
+
+
+
+int
+main (int argc, char **argv)
+{
+ gpgrt_opt_t opts[] = {
+ ARGPARSE_x ('v', "verbose", NONE, 0, "Laut sein"),
+ ARGPARSE_s_n('e', "echo" , ("Zeile ausgeben, damit wir sehen, "
+ "was wir eingegeben haben")),
+ ARGPARSE_s_n('d', "debug", "Debug\nfalls mal etwas\nschief geht"),
+ ARGPARSE_s_s('o', "output", 0 ),
+ ARGPARSE_o_s('c', "cross-ref", "cross-reference erzeugen\n" ),
+ /* Note that on a non-utf8 terminal the ß might garble the output. */
+ 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_end()
+ };
+ gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL
+ | ARGPARSE_FLAG_MIXED
+ | ARGPARSE_FLAG_ONEDASH) };
+ int i;
+
+ gpgrt_set_strusage (my_strusage);
+
+
+ while (gpgrt_argparse (NULL, &pargs, opts))
+ {
+ switch (pargs.r_opt)
+ {
+ 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 '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;
+ case 500: opt.a_long_one++; break;
+ default : pargs.err = ARGPARSE_PRINT_ERROR; break;
+ }
+ }
+ for (i=0; i < argc; i++ )
+ printf ("%3d -> (%s)\n", i, argv[i] );
+ puts ("Options:");
+ if (opt.verbose)
+ printf (" verbose=%d\n", opt.verbose );
+ if (opt.debug)
+ printf (" debug=%d\n", opt.debug );
+ if (opt.outfile)
+ printf (" outfile='%s'\n", opt.outfile );
+ if (opt.crf)
+ printf (" crffile='%s'\n", opt.crf );
+ if (opt.myopt)
+ printf (" myopt=%d\n", opt.myopt );
+ if (opt.a_long_one)
+ printf (" a-long-one=%d\n", opt.a_long_one );
+ if (opt.echo)
+ printf (" echo=%d\n", opt.echo );
+
+ gpgrt_argparse (NULL, &pargs, NULL);
+
+ return 0;
+}