blob: 249ad1e8736075eedcf3331a4eb06493db75fd00 [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>
Victor Stinner27e2d1f2018-11-01 00:52:28 +010034#include "pycore_getopt.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
Victor Stinner74f65682019-03-15 15:08:05 +010040int _PyOS_opterr = 1; /* generate error messages */
41Py_ssize_t _PyOS_optind = 1; /* index into argv array */
42const wchar_t *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000043
Victor Stinner870b0352019-05-17 03:15:12 +020044static const wchar_t *opt_ptr = L"";
Antoine Pitrou86838b02012-02-21 19:03:47 +010045
Victor Stinner6dcb5422019-03-05 02:44:12 +010046/* Python command line short and long options */
47
48#define SHORT_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
49
50static const _PyOS_LongOption longopts[] = {
51 {L"check-hash-based-pycs", 1, 0},
52 {NULL, 0, 0},
53};
54
55
Antoine Pitrou86838b02012-02-21 19:03:47 +010056void _PyOS_ResetGetOpt(void)
57{
Ezio Melottia0dd22e2012-11-23 18:48:32 +020058 _PyOS_opterr = 1;
Antoine Pitrou86838b02012-02-21 19:03:47 +010059 _PyOS_optind = 1;
60 _PyOS_optarg = NULL;
61 opt_ptr = L"";
62}
63
Victor Stinner870b0352019-05-17 03:15:12 +020064int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex)
Guido van Rossum2508ade1994-04-14 14:08:22 +000065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 wchar_t *ptr;
67 wchar_t option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (_PyOS_optind >= argc)
72 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
75 ++_PyOS_optind;
76 return 'h';
77 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000078#endif
79
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 else if (argv[_PyOS_optind][0] != L'-' ||
81 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
82 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
85 ++_PyOS_optind;
86 return -1;
87 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
90 ++_PyOS_optind;
91 return 'h';
92 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
95 ++_PyOS_optind;
96 return 'V';
97 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 opt_ptr = &argv[_PyOS_optind++][1];
100 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000101
Ezio Melotti7c663192012-11-18 13:55:52 +0200102 if ((option = *opt_ptr++) == L'\0')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 return -1;
Christian Heimes33fe8092008-04-13 13:53:33 +0000104
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800105 if (option == L'-') {
106 // Parse long option.
107 if (*opt_ptr == L'\0') {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700108 if (_PyOS_opterr) {
109 fprintf(stderr, "expected long option\n");
110 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800111 return -1;
112 }
113 *longindex = 0;
114 const _PyOS_LongOption *opt;
115 for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {
116 if (!wcscmp(opt->name, opt_ptr))
117 break;
118 }
119 if (!opt->name) {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700120 if (_PyOS_opterr) {
121 fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]);
122 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800123 return '_';
124 }
125 opt_ptr = L"";
126 if (!opt->has_arg) {
127 return opt->val;
128 }
129 if (_PyOS_optind >= argc) {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700130 if (_PyOS_opterr) {
131 fprintf(stderr, "Argument expected for the %ls options\n",
132 argv[_PyOS_optind - 1]);
133 }
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800134 return '_';
135 }
136 _PyOS_optarg = argv[_PyOS_optind++];
137 return opt->val;
138 }
139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (option == 'J') {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700141 if (_PyOS_opterr) {
Ezio Melotti7c663192012-11-18 13:55:52 +0200142 fprintf(stderr, "-J is reserved for Jython\n");
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 return '_';
145 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000146
Victor Stinner6dcb5422019-03-05 02:44:12 +0100147 if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700148 if (_PyOS_opterr) {
Ezio Melotti7c663192012-11-18 13:55:52 +0200149 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return '_';
152 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (*(ptr + 1) == L':') {
155 if (*opt_ptr != L'\0') {
156 _PyOS_optarg = opt_ptr;
157 opt_ptr = L"";
158 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 else {
161 if (_PyOS_optind >= argc) {
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700162 if (_PyOS_opterr) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 fprintf(stderr,
164 "Argument expected for the -%c option\n", (char)option);
Miss Islington (bot)bce4dda2020-05-06 06:43:09 -0700165 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 return '_';
167 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 _PyOS_optarg = argv[_PyOS_optind++];
170 }
171 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000174}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
176#ifdef __cplusplus
177}
178#endif
179