blob: 093c3dada294fdf6c5caa0ba3dcd89d50bfbbabe [file] [log] [blame]
Guido van Rossum871b8051994-04-28 12:33:58 +00001/*---------------------------------------------------------------------------*
2 * <RCS keywords>
3 *
4 * C++ Library
5 *
6 * Copyright 1992-1994, David Gottner
7 *
8 * All Rights Reserved
9 *
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000010 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
Guido van Rossum871b8051994-04-28 12:33:58 +000012 * provided that the above copyright notice, this permission notice and
13 * the following disclaimer notice appear unmodified in all copies.
14 *
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.
21 *
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 *---------------------------------------------------------------------------*/
Guido van Rossum2508ade1994-04-14 14:08:22 +000026
Georg Brandl9dceedb2006-07-12 15:31:17 +000027/* Modified to support --help and --version, as well as /? on Windows
28 * by Georg Brandl. */
29
Guido van Rossum2508ade1994-04-14 14:08:22 +000030#include <stdio.h>
31#include <string.h>
32
Anthony Baxterac6bd462006-04-13 02:06:09 +000033#ifdef __cplusplus
34extern "C" {
35#endif
36
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000037int _PyOS_opterr = 1; /* generate error messages */
38int _PyOS_optind = 1; /* index into argv array */
39char *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000040
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000041int _PyOS_GetOpt(int argc, char **argv, char *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000042{
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000043 static char *opt_ptr = "";
44 char *ptr;
45 int option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000046
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000047 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000048
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000049 if (_PyOS_optind >= argc)
50 return -1;
Georg Brandl9dceedb2006-07-12 15:31:17 +000051#ifdef MS_WINDOWS
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000052 else if (strcmp(argv[_PyOS_optind], "/?") == 0) {
53 ++_PyOS_optind;
54 return 'h';
55 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000056#endif
57
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000058 else if (argv[_PyOS_optind][0] != '-' ||
59 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
60 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000061
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000062 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
63 ++_PyOS_optind;
64 return -1;
65 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000066
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000067 else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
68 ++_PyOS_optind;
69 return 'h';
70 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000071
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000072 else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
73 ++_PyOS_optind;
74 return 'V';
75 }
Georg Brandl9dceedb2006-07-12 15:31:17 +000076
77
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000078 opt_ptr = &argv[_PyOS_optind++][1];
79 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000080
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000081 if ( (option = *opt_ptr++) == '\0')
82 return -1;
Christian Heimes7a98d272008-04-12 13:03:03 +000083
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000084 if (option == 'J') {
85 fprintf(stderr, "-J is reserved for Jython\n");
86 return '_';
87 }
Christian Heimes7a98d272008-04-12 13:03:03 +000088
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000089 if (option == 'X') {
90 fprintf(stderr,
91 "-X is reserved for implementation-specific arguments\n");
92 return '_';
93 }
Christian Heimes7a98d272008-04-12 13:03:03 +000094
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000095 if ((ptr = strchr(optstring, option)) == NULL) {
96 if (_PyOS_opterr)
97 fprintf(stderr, "Unknown option: -%c\n", option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000098
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000099 return '_';
100 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000101
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000102 if (*(ptr + 1) == ':') {
103 if (*opt_ptr != '\0') {
104 _PyOS_optarg = opt_ptr;
105 opt_ptr = "";
106 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000107
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000108 else {
109 if (_PyOS_optind >= argc) {
110 if (_PyOS_opterr)
111 fprintf(stderr,
112 "Argument expected for the -%c option\n", option);
113 return '_';
114 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000115
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000116 _PyOS_optarg = argv[_PyOS_optind++];
117 }
118 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000119
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000120 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000121}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000122
123#ifdef __cplusplus
124}
125#endif
126