File: parse-test.c

package info (click to toggle)
poldi 0.4.1-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,360 kB
  • ctags: 1,024
  • sloc: ansic: 9,554; sh: 4,671; makefile: 286; sed: 16
file content (113 lines) | stat: -rw-r--r-- 2,748 bytes parent folder | download | duplicates (3)
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
/* parse-test.c - test program for simpleparse.
   Copyright (C) 2008 g10 Code GmbH
 
   This file is part of Poldi.
 
   Poldi is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
 
   Poldi 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
   General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, see
   <http://www.gnu.org/licenses/>.  */
 

#include <assert.h>
#include <stdio.h>
#include <stdarg.h>

#include <gpg-error.h>

#include <simpleparse.h>
#include <simplelog.h>

enum opt_ids
  {
    FOO = 1,
    BAR,
    BAZ
  };

static simpleparse_opt_spec_t opt_specs[] =
  {
    { FOO, "foo", 'f', SIMPLEPARSE_ARG_REQUIRED, 0, "the foo switch requires an argument" },
    { BAR, "bar", 'b', SIMPLEPARSE_ARG_OPTIONAL, 0, "the bar switch takes an optional argument" },
    { BAZ, "baz", 0, SIMPLEPARSE_ARG_NONE, 0, "the baz switch takes no argument" },
    { 0 }
  };

static gpg_error_t
parsecb (void *cookie, simpleparse_opt_spec_t spec, const char *arg)
{
  const char *prefix = cookie;

  printf ("[%s] opt: '%s', argument: '%s'\n", prefix, spec.long_opt, arg);

  return 0;
}

int
main (int argc, const char **argv)
{
  simpleparse_handle_t handle = NULL;
  gpg_error_t err = 0;
  const char **rest_args;
  log_handle_t loghandle = NULL;

  assert (argc > 0);

  /* Init.  */
  err = log_create (&loghandle);
  assert (!err);

  err = log_set_backend_stream (loghandle, stderr);
  assert (!err);

  err = simpleparse_create (&handle);
  assert (!err);

  simpleparse_set_parse_cb (handle, parsecb, "parse-test parser");
  simpleparse_set_loghandle (handle, loghandle);
  simpleparse_set_specs (handle, opt_specs);

  /* Parse command-line arguments. */
  err = simpleparse_parse (handle, 0, argc - 1, argv + 1, &rest_args);
  if (err)
    {
      fprintf (stderr, "simpleparse_parse returned error: %s\n",
	       gpg_strerror (err));
      goto out;
    }

  printf ("Rest args: ");
  if (rest_args)
    {
      while (*rest_args)
	{
	  printf ("%s%s", *rest_args, *(rest_args + 1) ? ", " : "");
	  rest_args++;
	}
    }
  printf ("\n");

  /* Parse stdin as config file. */
  err = simpleparse_parse_stream (handle, 0, stdin);
  if (err)
    {
      fprintf (stderr, "simpleparse_parse_stream returned error: %s\n",
	       gpg_strerror (err));
      goto out;
    }

 out:

  simpleparse_destroy (handle);

  return !!err;
}