Debian Codegraph @

tests/parse-test.c

raw symbols SCIP
1 /* parse-test.c - test program for simpleparse.
2 Copyright (C) 2008, 2009 g10 Code GmbH
3
4 This file is part of Poldi.
5
6 Poldi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 Poldi 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 /* For Poldi I wrote a minimalistic library for parsing configuration
21 files and command-line arguments named "simpleparse". This is a
22 test program for simpleparse. -mo */
23
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27
28 #include <gpg-error.h>
29
30 #include <simpleparse.h>
31 #include <simplelog.h>
32
33 enum opt_ids
34 {
35 FOO = 1,
36 BAR,
37 BAZ
38 };
39
40 static simpleparse_opt_spec_t opt_specs[] =
41 {
42 { FOO, "foo", 'f', SIMPLEPARSE_ARG_REQUIRED, 0, "the foo switch requires an argument" },
43 { BAR, "bar", 'b', SIMPLEPARSE_ARG_OPTIONAL, 0, "the bar switch takes an optional argument" },
44 { BAZ, "baz", 0, SIMPLEPARSE_ARG_NONE, 0, "the baz switch takes no argument" },
45 { 0 }
46 };
47
48 static gpg_error_t
49 parsecb (void *cookie, simpleparse_opt_spec_t spec, const char *arg)
50 {
51 const char *prefix = cookie;
52
53 printf ("[%s] opt: '%s', argument: '%s'\n", prefix, spec.long_opt, arg);
54
55 return 0;
56 }
57
58 int
59 main (int argc, const char **argv)
60 {
61 simpleparse_handle_t handle = NULL;
62 gpg_error_t err = 0;
63 const char **rest_args;
64 log_handle_t loghandle = NULL;
65
66 assert (argc > 0);
67
68 /* Init. */
69 err = log_create (&loghandle);
70 assert (!err);
71
72 err = log_set_backend_stream (loghandle, stderr);
73 assert (!err);
74
75 err = simpleparse_create (&handle);
76 assert (!err);
77
78 simpleparse_set_parse_cb (handle, parsecb, "parse-test parser");
79 simpleparse_set_loghandle (handle, loghandle);
80 simpleparse_set_specs (handle, opt_specs);
81
82 /* Parse command-line arguments. */
83 err = simpleparse_parse (handle, 0, argc - 1, argv + 1, &rest_args);
84 if (err)
85 {
86 fprintf (stderr, "simpleparse_parse returned error: %s\n",
87 gpg_strerror (err));
88 goto out;
89 }
90
91 printf ("Rest args: ");
92 if (rest_args)
93 {
94 while (*rest_args)
95 {
96 printf ("%s%s", *rest_args, *(rest_args + 1) ? ", " : "");
97 rest_args++;
98 }
99 }
100 printf ("\n");
101
102 /* Parse stdin as config file. */
103 err = simpleparse_parse_stream (handle, 0, stdin);
104 if (err)
105 {
106 fprintf (stderr, "simpleparse_parse_stream returned error: %s\n",
107 gpg_strerror (err));
108 goto out;
109 }
110
111 out:
112
113 simpleparse_destroy (handle);
114
115 return !!err;
116 }