| 1 |
/* t-argparse.c - Check the argparse API |
| 2 |
* Copyright (C) 2018, 2020 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of Libgpg-error. |
| 5 |
* |
| 6 |
* Libgpg-error is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgpg-error is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <assert.h> |
| 29 |
|
| 30 |
#define PGM "t-argparse" |
| 31 |
#include "t-common.h" |
| 32 |
|
| 33 |
|
| 34 |
static struct { |
| 35 |
int verbose; |
| 36 |
int debug; |
| 37 |
char *outfile; |
| 38 |
char *crf; |
| 39 |
int myopt; |
| 40 |
int echo; |
| 41 |
int a_long_one; |
| 42 |
char *street; |
| 43 |
char *disable_akr; |
| 44 |
} opt; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static const char * |
| 49 |
my_strusage (int level) |
| 50 |
{ |
| 51 |
const char *p; |
| 52 |
|
| 53 |
switch (level) |
| 54 |
{ |
| 55 |
case 9: p = "LGPL-2.1-or-later"; break; |
| 56 |
|
| 57 |
case 11: p = "t-argparse"; break; |
| 58 |
case 13: p = "6.42.17-beta99"; break; |
| 59 |
|
| 60 |
default: p = NULL; |
| 61 |
} |
| 62 |
return p; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
int |
| 68 |
main (int argc, char **argv) |
| 69 |
{ |
| 70 |
gpgrt_opt_t opts[] = { |
| 71 |
ARGPARSE_verbatim("Now for the options:\n"), |
| 72 |
ARGPARSE_c (601, "foo", "Command FOO"), |
| 73 |
ARGPARSE_c (602, "bar", "Command BAR"), |
| 74 |
ARGPARSE_c (603, "foobar", "Command FOOBAR"), |
| 75 |
ARGPARSE_x ('v', "verbose", NONE, 0, "Laut sein"), |
| 76 |
ARGPARSE_s_n('e', "echo" , ("Zeile ausgeben, damit wir sehen, " |
| 77 |
"was wir eingegeben haben")), |
| 78 |
ARGPARSE_s_n('d', "debug", "Debug\nfalls mal etwas\nschief geht"), |
| 79 |
ARGPARSE_s_s('o', "output", 0 ), |
| 80 |
ARGPARSE_o_s('c', "cross-ref", "cross-reference erzeugen\n" ), |
| 81 |
/* Note that on a non-utf8 terminal the ß might garble the output. */ |
| 82 |
ARGPARSE_header("extra-options", "List of extra options"), |
| 83 |
ARGPARSE_s_s('s', "street","|Straße|set the name of the street to Straße"), |
| 84 |
ARGPARSE_o_i('m', "my-option", 0), |
| 85 |
ARGPARSE_o_i('M', "not-my-option", 0), |
| 86 |
ARGPARSE_s_n(500, "a-long-option", 0 ), |
| 87 |
ARGPARSE_conffile(501, "options", "|FILE|read options from FILE"), |
| 88 |
ARGPARSE_noconffile(502, "no-options", "Ignore conf files"), |
| 89 |
ARGPARSE_s_s(503, "disable-akr", "@" ), |
| 90 |
ARGPARSE_verbatim("This epilog consists\nof only 2 lines\n"), |
| 91 |
ARGPARSE_end() |
| 92 |
}; |
| 93 |
gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL |
| 94 |
| ARGPARSE_FLAG_MIXED |
| 95 |
| ARGPARSE_FLAG_ONEDASH |
| 96 |
| ARGPARSE_FLAG_SYS |
| 97 |
| ARGPARSE_FLAG_USER |
| 98 |
| ARGPARSE_FLAG_VERBOSE |
| 99 |
| ARGPARSE_FLAG_WITHATTR |
| 100 |
) }; |
| 101 |
int i; |
| 102 |
const char *srcdir; |
| 103 |
int any_warn = 0; |
| 104 |
int command = -1; |
| 105 |
|
| 106 |
if (getenv ("argparse_flag_command")) |
| 107 |
pargs.flags |= ARGPARSE_FLAG_COMMAND; |
| 108 |
|
| 109 |
gpgrt_set_strusage (my_strusage); |
| 110 |
srcdir = getenv ("srcdir"); |
| 111 |
if (!srcdir) |
| 112 |
srcdir = "."; |
| 113 |
gpgrt_set_confdir (GPGRT_CONFDIR_USER, srcdir); |
| 114 |
{ |
| 115 |
char *p = gpgrt_fnameconcat (srcdir, "etc", NULL); |
| 116 |
gpgrt_set_confdir (GPGRT_CONFDIR_SYS, p); |
| 117 |
xfree (p); |
| 118 |
} |
| 119 |
|
| 120 |
while (gpgrt_argparser (&pargs, opts, PGM".conf")) |
| 121 |
{ |
| 122 |
/* printf ("got option %3d type %0x04x\n", pargs.r_opt, pargs.r_type); */ |
| 123 |
/* if (pargs.r_type & (ARGPARSE_ATTR_IGNORE|ARGPARSE_ATTR_FORCE)) */ |
| 124 |
/* printf ("attributes:%s%s\n", */ |
| 125 |
/* (pargs.r_type & ARGPARSE_ATTR_IGNORE)? " ignore":"", */ |
| 126 |
/* (pargs.r_type & ARGPARSE_ATTR_FORCE)? " force":""); */ |
| 127 |
/* if (pargs.r_type & ARGPARSE_OPT_IGNORE) */ |
| 128 |
/* { */ |
| 129 |
/* printf ("ignored\n"); */ |
| 130 |
/* continue; */ |
| 131 |
/* } */ |
| 132 |
switch (pargs.r_opt) |
| 133 |
{ |
| 134 |
case ARGPARSE_CONFFILE: |
| 135 |
printf ("current conffile='%s'\n", |
| 136 |
pargs.r_type? pargs.r.ret_str: "[cmdline]"); |
| 137 |
break; |
| 138 |
case ARGPARSE_IS_ARG : |
| 139 |
printf ("arg='%s'\n", pargs.r.ret_str); |
| 140 |
break; |
| 141 |
|
| 142 |
case 'v': opt.verbose++; break; |
| 143 |
case 'e': opt.echo++; break; |
| 144 |
case 'd': opt.debug++; debug=1;break; |
| 145 |
case 'o': opt.outfile = pargs.r.ret_str; break; |
| 146 |
case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; |
| 147 |
case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; |
| 148 |
case 'M': opt.myopt = 0; break; |
| 149 |
case 's': opt.street = pargs.r.ret_str; break; |
| 150 |
case 500: opt.a_long_one++; break; |
| 151 |
case 503: opt.disable_akr = pargs.r.ret_str; break; |
| 152 |
|
| 153 |
case 601: |
| 154 |
case 602: |
| 155 |
case 603: |
| 156 |
command = pargs.r_opt; |
| 157 |
break; |
| 158 |
|
| 159 |
default : pargs.err = ARGPARSE_PRINT_WARNING; any_warn = 1; break; |
| 160 |
} |
| 161 |
} |
| 162 |
for (i=0; i < argc; i++ ) |
| 163 |
printf ("%3d -> (%s)\n", i, argv[i] ); |
| 164 |
if (command >= 0) |
| 165 |
printf ("Command: %d\n", command); |
| 166 |
if (opt.verbose) |
| 167 |
puts ("Options:"); |
| 168 |
if (opt.verbose) |
| 169 |
printf (" verbose=%d\n", opt.verbose ); |
| 170 |
if (opt.debug) |
| 171 |
printf (" debug=%d\n", opt.debug ); |
| 172 |
if (opt.outfile) |
| 173 |
printf (" outfile='%s'\n", opt.outfile ); |
| 174 |
if (opt.crf) |
| 175 |
printf (" crffile='%s'\n", opt.crf ); |
| 176 |
if (opt.street) |
| 177 |
printf (" street='%s'\n", opt.street ); |
| 178 |
if (opt.myopt) |
| 179 |
printf (" myopt=%d\n", opt.myopt ); |
| 180 |
if (opt.a_long_one) |
| 181 |
printf (" a-long-one=%d\n", opt.a_long_one ); |
| 182 |
if (opt.disable_akr) |
| 183 |
printf (" disable-akr=%s\n", opt.disable_akr); |
| 184 |
if (opt.echo) |
| 185 |
printf (" echo=%d\n", opt.echo ); |
| 186 |
|
| 187 |
gpgrt_argparse (NULL, &pargs, NULL); |
| 188 |
|
| 189 |
(void)show; |
| 190 |
(void)fail; |
| 191 |
(void)die; |
| 192 |
|
| 193 |
return !!any_warn; |
| 194 |
} |