blob: 064a1874ea5860719e0bc7d88e7c68cfca6a23a4 [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 Pitrouf95a1b32010-05-09 15:52:27 +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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027/* Modified to support --help and --version, as well as /? on Windows
28 * by Georg Brandl. */
29
Martin v. Löwis790465f2008-04-05 20:41:37 +000030#include <Python.h>
Guido van Rossum2508ade1994-04-14 14:08:22 +000031#include <stdio.h>
32#include <string.h>
Martin v. Löwis790465f2008-04-05 20:41:37 +000033#include <wchar.h>
34#include <pygetopt.h>
Guido van Rossum2508ade1994-04-14 14:08:22 +000035
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036#ifdef __cplusplus
37extern "C" {
38#endif
39
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000040int _PyOS_opterr = 1; /* generate error messages */
41int _PyOS_optind = 1; /* index into argv array */
Martin v. Löwis790465f2008-04-05 20:41:37 +000042wchar_t *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000043
Martin v. Löwis790465f2008-04-05 20:41:37 +000044int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 static wchar_t *opt_ptr = L"";
47 wchar_t *ptr;
48 wchar_t option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 if (_PyOS_optind >= argc)
53 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000054#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
56 ++_PyOS_optind;
57 return 'h';
58 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000059#endif
60
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 else if (argv[_PyOS_optind][0] != L'-' ||
62 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
63 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
66 ++_PyOS_optind;
67 return -1;
68 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
71 ++_PyOS_optind;
72 return 'h';
73 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
76 ++_PyOS_optind;
77 return 'V';
78 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079
80
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 opt_ptr = &argv[_PyOS_optind++][1];
82 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if ( (option = *opt_ptr++) == L'\0')
85 return -1;
Christian Heimes33fe8092008-04-13 13:53:33 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (option == 'J') {
88 fprintf(stderr, "-J is reserved for Jython\n");
89 return '_';
90 }
Christian Heimes33fe8092008-04-13 13:53:33 +000091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 if ((ptr = wcschr(optstring, option)) == NULL) {
93 if (_PyOS_opterr)
94 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 return '_';
97 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if (*(ptr + 1) == L':') {
100 if (*opt_ptr != L'\0') {
101 _PyOS_optarg = opt_ptr;
102 opt_ptr = L"";
103 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 else {
106 if (_PyOS_optind >= argc) {
107 if (_PyOS_opterr)
108 fprintf(stderr,
109 "Argument expected for the -%c option\n", (char)option);
110 return '_';
111 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 _PyOS_optarg = argv[_PyOS_optind++];
114 }
115 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000118}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119
120#ifdef __cplusplus
121}
122#endif
123