blob: 5cf4cbd7bb35e8ee3e3558aaf022d23cd0c93d8a [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
Antoine Pitrou86838b02012-02-21 19:03:47 +010044static wchar_t *opt_ptr = L"";
45
46void _PyOS_ResetGetOpt(void)
47{
Ezio Melottia0dd22e2012-11-23 18:48:32 +020048 _PyOS_opterr = 1;
Antoine Pitrou86838b02012-02-21 19:03:47 +010049 _PyOS_optind = 1;
50 _PyOS_optarg = NULL;
51 opt_ptr = L"";
52}
53
Martin v. Löwis790465f2008-04-05 20:41:37 +000054int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
Guido van Rossum2508ade1994-04-14 14:08:22 +000055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 wchar_t *ptr;
57 wchar_t option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (_PyOS_optind >= argc)
62 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000063#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
65 ++_PyOS_optind;
66 return 'h';
67 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000068#endif
69
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 else if (argv[_PyOS_optind][0] != L'-' ||
71 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
72 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
75 ++_PyOS_optind;
76 return -1;
77 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
80 ++_PyOS_optind;
81 return 'h';
82 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
85 ++_PyOS_optind;
86 return 'V';
87 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088
89
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 opt_ptr = &argv[_PyOS_optind++][1];
91 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000092
Ezio Melotti7c663192012-11-18 13:55:52 +020093 if ((option = *opt_ptr++) == L'\0')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return -1;
Christian Heimes33fe8092008-04-13 13:53:33 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 if (option == 'J') {
Ezio Melotti7c663192012-11-18 13:55:52 +020097 if (_PyOS_opterr)
98 fprintf(stderr, "-J is reserved for Jython\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 return '_';
100 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 if ((ptr = wcschr(optstring, option)) == NULL) {
103 if (_PyOS_opterr)
Ezio Melotti7c663192012-11-18 13:55:52 +0200104 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return '_';
106 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (*(ptr + 1) == L':') {
109 if (*opt_ptr != L'\0') {
110 _PyOS_optarg = opt_ptr;
111 opt_ptr = L"";
112 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 else {
115 if (_PyOS_optind >= argc) {
116 if (_PyOS_opterr)
117 fprintf(stderr,
118 "Argument expected for the -%c option\n", (char)option);
119 return '_';
120 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 _PyOS_optarg = argv[_PyOS_optind++];
123 }
124 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000127}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128
129#ifdef __cplusplus
130}
131#endif
132