blob: 659efcfff812bc1db1734eac2767bf69a58f8086 [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 *
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.
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{
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000043 static char *opt_ptr = "";
44 char *ptr;
45 int option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000046
Guido van Rossum871b8051994-04-28 12:33:58 +000047 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000048
Georg Brandl9dceedb2006-07-12 15:31:17 +000049 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';
55 }
56#endif
57
58 else if (argv[_PyOS_optind][0] != '-' ||
59 argv[_PyOS_optind][1] == '\0' /* lone dash */ )
Guido van Rossum871b8051994-04-28 12:33:58 +000060 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000061
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000062 else if (strcmp(argv[_PyOS_optind], "--") == 0) {
63 ++_PyOS_optind;
Guido van Rossum871b8051994-04-28 12:33:58 +000064 return -1;
65 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000066
Georg Brandl9dceedb2006-07-12 15:31:17 +000067 else if (strcmp(argv[_PyOS_optind], "--help") == 0) {
68 ++_PyOS_optind;
69 return 'h';
70 }
71
72 else if (strcmp(argv[_PyOS_optind], "--version") == 0) {
73 ++_PyOS_optind;
74 return 'V';
75 }
76
77
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000078 opt_ptr = &argv[_PyOS_optind++][1];
Guido van Rossum871b8051994-04-28 12:33:58 +000079 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000080
Guido van Rossumb4102bf1997-09-30 22:00:13 +000081 if ( (option = *opt_ptr++) == '\0')
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000082 return -1;
Guido van Rossumb4102bf1997-09-30 22:00:13 +000083
84 if ((ptr = strchr(optstring, option)) == NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000085 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +000086 fprintf(stderr, "Unknown option: -%c\n", option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000087
Georg Brandl9dceedb2006-07-12 15:31:17 +000088 return '_';
Guido van Rossum871b8051994-04-28 12:33:58 +000089 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000090
Guido van Rossum871b8051994-04-28 12:33:58 +000091 if (*(ptr + 1) == ':') {
92 if (*opt_ptr != '\0') {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000093 _PyOS_optarg = opt_ptr;
Guido van Rossum871b8051994-04-28 12:33:58 +000094 opt_ptr = "";
95 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000096
Guido van Rossum871b8051994-04-28 12:33:58 +000097 else {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000098 if (_PyOS_optind >= argc) {
99 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +0000100 fprintf(stderr,
101 "Argument expected for the -%c option\n", option);
Georg Brandl9dceedb2006-07-12 15:31:17 +0000102 return '_';
Guido van Rossum871b8051994-04-28 12:33:58 +0000103 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000104
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000105 _PyOS_optarg = argv[_PyOS_optind++];
Guido van Rossum871b8051994-04-28 12:33:58 +0000106 }
107 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000108
Guido van Rossum871b8051994-04-28 12:33:58 +0000109 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000110}
Anthony Baxterac6bd462006-04-13 02:06:09 +0000111
112#ifdef __cplusplus
113}
114#endif
115