Implement new command-line argument parser.
[pspp-builds.git] / src / libpspp / argv-parser.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef LIBPSPP_ARGV_PARSER_H
18 #define LIBPSPP_ARGV_PARSER_H 1
19
20 /* Simple, modular command-line argument parser.
21
22    glibc has two option parsers, but neither one of them feels
23    quite right:
24
25      - getopt_long is simple, but not modular, in that there is
26        no easy way to make it accept multiple collections of
27        options supported by different modules.
28
29      - argp is more sophisticated and more complete, and hence
30        more complex.  It still lacks one important feature for
31        modularity: there is no straightforward way for option
32        groups that are implemented independently to have separate
33        auxiliary data.
34
35    The parser implemented in this file is meant to be simple and
36    modular.  It is based internally on getopt_long. */
37
38 #include <getopt.h>
39 #include <stdbool.h>
40
41 struct argv_option
42   {
43     const char *long_name;  /* Long option name, NULL if none. */
44     int short_name;         /* Short option character, 0 if none. */
45     int has_arg;            /* no_argument, required_argument, or
46                                optional_argument. */
47     int id;                 /* Value passed to callback. */
48   };
49
50 struct argv_parser *argv_parser_create (void);
51 void argv_parser_destroy (struct argv_parser *);
52
53 void argv_parser_add_options (struct argv_parser *,
54                               const struct argv_option *options, size_t n,
55                               void (*cb) (int id, void *aux), void *aux);
56 bool argv_parser_run (struct argv_parser *, int argc, char **argv);
57
58 #endif /* libpspp/argv-parser.h */