2 * @brief Wrappers to allow GNU getopt to be used cleanly from C++ code.
4 /* Copyright (C) 2004,2009,2010 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #ifndef XAPIAN_INCLUDED_GNU_GETOPT_H
23 #define XAPIAN_INCLUDED_GNU_GETOPT_H
25 // We need to include a header to get __GLIBC__ defined. Hopefully <cctype>
29 #define GNU_GETOPT_INTERFACE_VERSION 2
30 #if defined __GLIBC__ && __GLIBC__ >= 2
31 # include <gnu-versions.h>
32 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
33 # define USE_GLIBC_GNUGETOPT
37 #ifdef USE_GLIBC_GNUGETOPT
42 gnu_getopt(int argc_
, char *const *argv_
, const char *shortopts_
) {
43 return getopt(argc_
, argv_
, shortopts_
);
47 gnu_getopt_long(int argc_
, char *const *argv_
, const char *shortopts_
,
48 const struct option
*longopts_
, int *optind_
) {
49 return getopt_long(argc_
, argv_
, shortopts_
, longopts_
, optind_
);
53 gnu_getopt_long_only(int argc_
, char *const *argv_
, const char *shortopts_
,
54 const struct option
*longopts_
, int *optind_
) {
55 return getopt_long_only(argc_
, argv_
, shortopts_
, longopts_
, optind_
);
61 // Cygwin has __declspec(dllimport) magic on optarg, etc, so just pull in the
62 // header there rather than trying to duplicate that.
79 # define no_argument 0
80 # define required_argument 1
81 # define optional_argument 2
84 // For internal use only.
86 gnu_getopt_internal_(int, char *const *, const char *, const struct option
*,
90 gnu_getopt(int argc_
, char *const *argv_
, const char *shortopts_
) {
91 return gnu_getopt_internal_(argc_
, argv_
, shortopts_
,
92 reinterpret_cast<const struct option
*>(0),
93 reinterpret_cast<int *>(0), 0);
97 gnu_getopt_long(int argc_
, char *const *argv_
, const char *shortopts_
,
98 const struct option
*longopts_
, int *optind_
) {
99 return gnu_getopt_internal_(argc_
, argv_
, shortopts_
, longopts_
, optind_
, 0);
103 gnu_getopt_long_only(int argc_
, char *const *argv_
, const char *shortopts_
,
104 const struct option
*longopts_
, int *optind_
) {
105 return gnu_getopt_internal_(argc_
, argv_
, shortopts_
, longopts_
, optind_
, 1);