blob: 7c1d60500607f98c9f04ff82cb66270ab9623b18 [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
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{
Martin v. Löwis790465f2008-04-05 20:41:37 +000046 static wchar_t *opt_ptr = L"";
47 wchar_t *ptr;
48 wchar_t option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000049
Guido van Rossum871b8051994-04-28 12:33:58 +000050 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000051
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052 if (_PyOS_optind >= argc)
53 return -1;
54#ifdef MS_WINDOWS
Martin v. Löwis790465f2008-04-05 20:41:37 +000055 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056 ++_PyOS_optind;
57 return 'h';
58 }
59#endif
60
Martin v. Löwis790465f2008-04-05 20:41:37 +000061 else if (argv[_PyOS_optind][0] != L'-' ||
62 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
Guido van Rossum871b8051994-04-28 12:33:58 +000063 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000064
Martin v. Löwis790465f2008-04-05 20:41:37 +000065 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000066 ++_PyOS_optind;
Guido van Rossum871b8051994-04-28 12:33:58 +000067 return -1;
68 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000069
Martin v. Löwis790465f2008-04-05 20:41:37 +000070 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071 ++_PyOS_optind;
72 return 'h';
73 }
74
Martin v. Löwis790465f2008-04-05 20:41:37 +000075 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +000076 ++_PyOS_optind;
77 return 'V';
78 }
79
80
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000081 opt_ptr = &argv[_PyOS_optind++][1];
Guido van Rossum871b8051994-04-28 12:33:58 +000082 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000083
Martin v. Löwis790465f2008-04-05 20:41:37 +000084 if ( (option = *opt_ptr++) == L'\0')
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000085 return -1;
Guido van Rossumb4102bf1997-09-30 22:00:13 +000086
Martin v. Löwis790465f2008-04-05 20:41:37 +000087 if ((ptr = wcschr(optstring, option)) == NULL) {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000088 if (_PyOS_opterr)
Martin v. Löwis790465f2008-04-05 20:41:37 +000089 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Guido van Rossum2508ade1994-04-14 14:08:22 +000090
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 return '_';
Guido van Rossum871b8051994-04-28 12:33:58 +000092 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000093
Martin v. Löwis790465f2008-04-05 20:41:37 +000094 if (*(ptr + 1) == L':') {
95 if (*opt_ptr != L'\0') {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +000096 _PyOS_optarg = opt_ptr;
Martin v. Löwis790465f2008-04-05 20:41:37 +000097 opt_ptr = L"";
Guido van Rossum871b8051994-04-28 12:33:58 +000098 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000099
Guido van Rossum871b8051994-04-28 12:33:58 +0000100 else {
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000101 if (_PyOS_optind >= argc) {
102 if (_PyOS_opterr)
Guido van Rossum871b8051994-04-28 12:33:58 +0000103 fprintf(stderr,
Martin v. Löwis790465f2008-04-05 20:41:37 +0000104 "Argument expected for the -%c option\n", (char)option);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 return '_';
Guido van Rossum871b8051994-04-28 12:33:58 +0000106 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000107
Thomas Wouters2cffc7d2000-11-03 08:18:37 +0000108 _PyOS_optarg = argv[_PyOS_optind++];
Guido van Rossum871b8051994-04-28 12:33:58 +0000109 }
110 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000111
Guido van Rossum871b8051994-04-28 12:33:58 +0000112 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000113}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114
115#ifdef __cplusplus
116}
117#endif
118