Blame tests/t-argparse.c

Packit fc043f
/* t-argparse.c - Check the argparse API
Packit fc043f
 * Copyright (C) 2018 g10 Code GmbH
Packit fc043f
 *
Packit fc043f
 * This file is part of Libgpg-error.
Packit fc043f
 *
Packit fc043f
 * Libgpg-error is free software; you can redistribute it and/or
Packit fc043f
 * modify it under the terms of the GNU Lesser General Public License
Packit fc043f
 * as published by the Free Software Foundation; either version 2.1 of
Packit fc043f
 * the License, or (at your option) any later version.
Packit fc043f
 *
Packit fc043f
 * Libgpg-error is distributed in the hope that it will be useful, but
Packit fc043f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit fc043f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit fc043f
 * Lesser General Public License for more details.
Packit fc043f
 *
Packit fc043f
 * You should have received a copy of the GNU Lesser General Public License
Packit fc043f
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit fc043f
 * SPDX-License-Identifier: LGPL-2.1-or-later
Packit fc043f
 */
Packit fc043f
Packit fc043f
#ifdef HAVE_CONFIG_H
Packit fc043f
#include <config.h>
Packit fc043f
#endif
Packit fc043f
Packit fc043f
#include <stdio.h>
Packit fc043f
#include <stdlib.h>
Packit fc043f
#include <string.h>
Packit fc043f
#include <assert.h>
Packit fc043f
Packit fc043f
#include "../src/gpg-error.h"
Packit fc043f
Packit fc043f
Packit fc043f
static struct {
Packit fc043f
    int verbose;
Packit fc043f
    int debug;
Packit fc043f
    char *outfile;
Packit fc043f
    char *crf;
Packit fc043f
    int myopt;
Packit fc043f
    int echo;
Packit fc043f
    int a_long_one;
Packit fc043f
} opt;
Packit fc043f
Packit fc043f
Packit fc043f
Packit fc043f
static const char *
Packit fc043f
my_strusage (int level)
Packit fc043f
{
Packit fc043f
  const char *p;
Packit fc043f
Packit fc043f
  switch (level)
Packit fc043f
    {
Packit fc043f
    case 9: p = "GPL-2.0-or-later"; break;
Packit fc043f
Packit fc043f
    case 11: p = "t-argparse"; break;
Packit fc043f
Packit fc043f
    default: p = NULL;
Packit fc043f
    }
Packit fc043f
  return p;
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
Packit fc043f
int
Packit fc043f
main (int argc, char **argv)
Packit fc043f
{
Packit fc043f
  gpgrt_opt_t opts[] = {
Packit fc043f
    ARGPARSE_x  ('v', "verbose", NONE, 0, "Laut sein"),
Packit fc043f
    ARGPARSE_s_n('e', "echo"   , ("Zeile ausgeben, damit wir sehen, "
Packit fc043f
                                  "was wir eingegeben haben")),
Packit fc043f
    ARGPARSE_s_n('d', "debug", "Debug\nfalls mal etwas\nschief geht"),
Packit fc043f
    ARGPARSE_s_s('o', "output", 0 ),
Packit fc043f
    ARGPARSE_o_s('c', "cross-ref", "cross-reference erzeugen\n" ),
Packit fc043f
    /* Note that on a non-utf8 terminal the ß might garble the output. */
Packit fc043f
    ARGPARSE_s_n('s', "street","|Straße|set the name of the street to Straße"),
Packit fc043f
    ARGPARSE_o_i('m', "my-option", 0),
Packit fc043f
    ARGPARSE_s_n(500, "a-long-option", 0 ),
Packit fc043f
    ARGPARSE_end()
Packit fc043f
  };
Packit fc043f
  gpgrt_argparse_t pargs = { &argc, &argv, (ARGPARSE_FLAG_ALL
Packit fc043f
                                            | ARGPARSE_FLAG_MIXED
Packit fc043f
                                            | ARGPARSE_FLAG_ONEDASH) };
Packit fc043f
  int i;
Packit fc043f
Packit fc043f
  gpgrt_set_strusage (my_strusage);
Packit fc043f
Packit fc043f
Packit fc043f
  while (gpgrt_argparse  (NULL, &pargs, opts))
Packit fc043f
    {
Packit fc043f
      switch (pargs.r_opt)
Packit fc043f
        {
Packit fc043f
        case ARGPARSE_IS_ARG :
Packit fc043f
          printf ("arg='%s'\n", pargs.r.ret_str);
Packit fc043f
          break;
Packit fc043f
        case 'v': opt.verbose++; break;
Packit fc043f
        case 'e': opt.echo++; break;
Packit fc043f
        case 'd': opt.debug++; break;
Packit fc043f
        case 'o': opt.outfile = pargs.r.ret_str; break;
Packit fc043f
        case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break;
Packit fc043f
        case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break;
Packit fc043f
        case 500: opt.a_long_one++;  break;
Packit fc043f
        default : pargs.err = ARGPARSE_PRINT_ERROR; break;
Packit fc043f
	}
Packit fc043f
    }
Packit fc043f
  for (i=0; i < argc; i++ )
Packit fc043f
    printf ("%3d -> (%s)\n", i, argv[i] );
Packit fc043f
  if (opt.verbose)
Packit fc043f
    puts ("Options:");
Packit fc043f
  if (opt.verbose)
Packit fc043f
    printf ("  verbose=%d\n", opt.verbose );
Packit fc043f
  if (opt.debug)
Packit fc043f
    printf ("  debug=%d\n", opt.debug );
Packit fc043f
  if (opt.outfile)
Packit fc043f
    printf ("  outfile='%s'\n", opt.outfile );
Packit fc043f
  if (opt.crf)
Packit fc043f
    printf ("  crffile='%s'\n", opt.crf );
Packit fc043f
  if (opt.myopt)
Packit fc043f
    printf ("  myopt=%d\n", opt.myopt );
Packit fc043f
  if (opt.a_long_one)
Packit fc043f
    printf ("  a-long-one=%d\n", opt.a_long_one );
Packit fc043f
  if (opt.echo)
Packit fc043f
    printf ("  echo=%d\n", opt.echo );
Packit fc043f
Packit fc043f
  gpgrt_argparse (NULL, &pargs, NULL);
Packit fc043f
Packit fc043f
  return 0;
Packit fc043f
}