Since r76034 was successful, add a NEWS entry for it.
[python.git] / Python / getopt.c
blob247600bf2ab016d1782eec345716471c48d85141
1 /*---------------------------------------------------------------------------*
2 * <RCS keywords>
4 * C++ Library
6 * Copyright 1992-1994, David Gottner
8 * All Rights Reserved
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice, this permission notice and
13 * the following disclaimer notice appear unmodified in all copies.
15 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Nevertheless, I would like to know about bugs in this library or
23 * suggestions for improvment. Send bug reports and feedback to
24 * davegottner@delphi.com.
25 *---------------------------------------------------------------------------*/
27 /* Modified to support --help and --version, as well as /? on Windows
28 * by Georg Brandl. */
30 #include <stdio.h>
31 #include <string.h>
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
37 int _PyOS_opterr = 1; /* generate error messages */
38 int _PyOS_optind = 1; /* index into argv array */
39 char *_PyOS_optarg = NULL; /* optional argument */
41 int _PyOS_GetOpt(int argc, char **argv, char *optstring)
43 static char *opt_ptr = "";
44 char *ptr;
45 int option;
47 if (*opt_ptr == '\0') {
49 if (_PyOS_optind >= argc)
50 return -1;
51 #ifdef MS_WINDOWS
52 else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
53 ++_PyOS_optind;
54 return 'h';
56 #endif
58 else if (argv[_PyOS_optind][0] != '-' ||
59 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
60 return -1;
62 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
63 ++_PyOS_optind;
64 return -1;
67 else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
68 ++_PyOS_optind;
69 return 'h';
72 else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
73 ++_PyOS_optind;
74 return 'V';
78 opt_ptr = &argv[_PyOS_optind++][1];
81 if ( (option = *opt_ptr++) == '\0')
82 return -1;
84 if (option == 'J') {
85 fprintf(stderr, "-J is reserved for Jython\n");
86 return '_';
89 if (option == 'X') {
90 fprintf(stderr,
91 "-X is reserved for implementation-specific arguments\n");
92 return '_';
95 if ((ptr = strchr(optstring, option)) == NULL) {
96 if (_PyOS_opterr)
97 fprintf(stderr, "Unknown option: -%c\n", option);
99 return '_';
102 if (*(ptr + 1) == ':') {
103 if (*opt_ptr != '\0') {
104 _PyOS_optarg = opt_ptr;
105 opt_ptr = "";
108 else {
109 if (_PyOS_optind >= argc) {
110 if (_PyOS_opterr)
111 fprintf(stderr,
112 "Argument expected for the -%c option\n", option);
113 return '_';
116 _PyOS_optarg = argv[_PyOS_optind++];
120 return option;
123 #ifdef __cplusplus
125 #endif