summaryrefslogtreecommitdiff
path: root/tests/t-argparse.c
blob: 4d24e5ff7fd34b33c818c1358100a7bfd1e6c459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* t-argparse.c - Check the argparse API
 * Copyright (C) 2018, 2020 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>

#define PGM "t-argparse"
#include "t-common.h"


static struct {
    int verbose;
    int debug;
    char *outfile;
    char *crf;
    int myopt;
    int echo;
    int a_long_one;
    char *street;
} opt;



static const char *
my_strusage (int level)
{
  const char *p;

  switch (level)
    {
    case 9: p = "LGPL-2.1-or-later"; break;

    case 11: p = "t-argparse"; break;
    case 13: p = "6.42.17-beta99"; break;

    default: p = NULL;
    }
  return p;
}



int
main (int argc, char **argv)
{
  gpgrt_opt_t opts[] = {
    ARGPARSE_verbatim("Now for the options:\n"),
    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_header("extra-options", "List of extra options"),
    ARGPARSE_s_s('s', "street","|Straße|set the name of the street to Straße"),
    ARGPARSE_o_i('m', "my-option", 0),
    ARGPARSE_o_i('M', "not-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_verbatim("This epilog consists\nof only 2 lines\n"),
    ARGPARSE_end()
  };
  gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL
                                            | ARGPARSE_FLAG_MIXED
                                            | ARGPARSE_FLAG_ONEDASH
                                            | ARGPARSE_FLAG_SYS
                                            | ARGPARSE_FLAG_USER
                                            /* | ARGPARSE_FLAG_VERBOSE */
                                            /* | ARGPARSE_FLAG_WITHATTR */
                                            ) };
  int i;
  const char *srcdir;
  int any_warn = 0;

  gpgrt_set_strusage (my_strusage);
  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 %3d type %0x04x\n", pargs.r_opt, pargs.r_type); */
      /* if (pargs.r_type & (ARGPARSE_ATTR_IGNORE|ARGPARSE_ATTR_FORCE)) */
      /*   printf ("attributes:%s%s\n", */
      /*           (pargs.r_type & ARGPARSE_ATTR_IGNORE)? " ignore":"", */
      /*           (pargs.r_type & ARGPARSE_ATTR_FORCE)? " force":""); */
      /* if (pargs.r_type & ARGPARSE_OPT_IGNORE) */
      /*   { */
      /*     printf ("ignored\n"); */
      /*     continue; */
      /*   } */
      switch (pargs.r_opt)
        {
        case ARGPARSE_CONFFILE:
          printf ("current conffile='%s'\n",
                  pargs.r_type? 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++; 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;
        case 'M': opt.myopt = 0; break;
        case 's': opt.street = pargs.r.ret_str; break;
        case 500: opt.a_long_one++;  break;
        default : pargs.err = ARGPARSE_PRINT_WARNING; any_warn = 1; break;
	}
    }
  for (i=0; i < argc; i++ )
    printf ("%3d -> (%s)\n", i, argv[i] );
  if (opt.verbose)
    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.street)
    printf ("  street='%s'\n", opt.street );
  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);

  (void)show;
  (void)fail;
  (void)die;

  return !!any_warn;
}