1 /* getopt_long and getopt_long_only entry points for GNU getopt.
3 Copyright 2008, 2010 Karl Berry.
4 Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
6 The original version of this file was part of the GNU C Library.
7 It has been modified for use with libkpathsea.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with this library; if not, see <http://www.gnu.org/licenses/>. */
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself. This code is part of the GNU C
33 Library, but also included in many other GNU distributions. Compiling
34 and linking in this code is a waste when using the GNU C library
35 (especially if it is a shared library). Rather than having every GNU
36 program understand `configure --with-gnu-libc' and omit the object files,
37 it is simpler to just do this in the source for each such file. */
39 #define GETOPT_INTERFACE_VERSION 2
40 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
41 #include <gnu-versions.h>
42 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
50 /* This needs to come after some library #include
51 to get __GNU_LIBRARY__ defined. */
52 #ifdef __GNU_LIBRARY__
64 const struct option
*long_options
,
67 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 0);
70 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
71 If an option that starts with '-' (not '--') doesn't match a long option,
72 but does match a short option, it is parsed as a short option
79 const struct option
*long_options
,
82 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 1);
86 #endif /* Not ELIDE_CODE. */
92 int main(int argc
, char **argv
)
99 int this_option_optind
= optind
? optind
: 1;
100 int option_index
= 0;
101 static struct option long_options
[] =
106 {"verbose", 0, 0, 0},
112 c
= getopt_long (argc
, argv
, "abc:d:0123456789",
113 long_options
, &option_index
);
120 printf ("option %s", long_options
[option_index
].name
);
122 printf (" with arg %s", optarg
);
136 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
137 printf ("digits occur in two different argv-elements.\n");
138 digit_optind
= this_option_optind
;
139 printf ("option %c\n", c
);
143 printf ("option a\n");
147 printf ("option b\n");
151 printf ("option c with value `%s'\n", optarg
);
155 printf ("option d with value `%s'\n", optarg
);
162 printf ("?? getopt returned character code 0%o ??\n", c
);
168 printf ("non-option ARGV-elements: ");
169 while (optind
< argc
)
170 printf ("%s ", argv
[optind
++]);