blob: 708d9ce496287c508b163d46695d557d50b0b056 [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.
Guido van Rossum871b8051994-04-28 12:33:58 +000021 *---------------------------------------------------------------------------*/
Guido van Rossum2508ade1994-04-14 14:08:22 +000022
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023/* Modified to support --help and --version, as well as /? on Windows
24 * by Georg Brandl. */
25
Martin v. Löwis790465f2008-04-05 20:41:37 +000026#include <Python.h>
Guido van Rossum2508ade1994-04-14 14:08:22 +000027#include <stdio.h>
28#include <string.h>
Martin v. Löwis790465f2008-04-05 20:41:37 +000029#include <wchar.h>
Victor Stinner27e2d1f2018-11-01 00:52:28 +010030#include "pycore_getopt.h"
Guido van Rossum2508ade1994-04-14 14:08:22 +000031
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032#ifdef __cplusplus
33extern "C" {
34#endif
35
Victor Stinner74f65682019-03-15 15:08:05 +010036int _PyOS_opterr = 1; /* generate error messages */
37Py_ssize_t _PyOS_optind = 1; /* index into argv array */
38const wchar_t *_PyOS_optarg = NULL; /* optional argument */
Guido van Rossum2508ade1994-04-14 14:08:22 +000039
Victor Stinner870b0352019-05-17 03:15:12 +020040static const wchar_t *opt_ptr = L"";
Antoine Pitrou86838b02012-02-21 19:03:47 +010041
Victor Stinner6dcb5422019-03-05 02:44:12 +010042/* Python command line short and long options */
43
44#define SHORT_OPTS L"bBc:dEhiIJm:OqRsStuvVW:xX:?"
45
46static const _PyOS_LongOption longopts[] = {
47 {L"check-hash-based-pycs", 1, 0},
48 {NULL, 0, 0},
49};
50
51
Antoine Pitrou86838b02012-02-21 19:03:47 +010052void _PyOS_ResetGetOpt(void)
53{
Ezio Melottia0dd22e2012-11-23 18:48:32 +020054 _PyOS_opterr = 1;
Antoine Pitrou86838b02012-02-21 19:03:47 +010055 _PyOS_optind = 1;
56 _PyOS_optarg = NULL;
57 opt_ptr = L"";
58}
59
Victor Stinner870b0352019-05-17 03:15:12 +020060int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex)
Guido van Rossum2508ade1994-04-14 14:08:22 +000061{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 wchar_t *ptr;
63 wchar_t option;
Guido van Rossum2508ade1994-04-14 14:08:22 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (*opt_ptr == '\0') {
Guido van Rossum2508ade1994-04-14 14:08:22 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (_PyOS_optind >= argc)
68 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000069#ifdef MS_WINDOWS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 else if (wcscmp(argv[_PyOS_optind], L"/?") == 0) {
71 ++_PyOS_optind;
72 return 'h';
73 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000074#endif
75
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 else if (argv[_PyOS_optind][0] != L'-' ||
77 argv[_PyOS_optind][1] == L'\0' /* lone dash */ )
78 return -1;
Guido van Rossum2508ade1994-04-14 14:08:22 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 else if (wcscmp(argv[_PyOS_optind], L"--") == 0) {
81 ++_PyOS_optind;
82 return -1;
83 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 else if (wcscmp(argv[_PyOS_optind], L"--help") == 0) {
86 ++_PyOS_optind;
87 return 'h';
88 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 else if (wcscmp(argv[_PyOS_optind], L"--version") == 0) {
91 ++_PyOS_optind;
92 return 'V';
93 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 opt_ptr = &argv[_PyOS_optind++][1];
96 }
Guido van Rossum2508ade1994-04-14 14:08:22 +000097
Ezio Melotti7c663192012-11-18 13:55:52 +020098 if ((option = *opt_ptr++) == L'\0')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 return -1;
Christian Heimes33fe8092008-04-13 13:53:33 +0000100
Benjamin Peterson42aa93b2017-12-09 10:26:52 -0800101 if (option == L'-') {
102 // Parse long option.
103 if (*opt_ptr == L'\0') {
104 fprintf(stderr, "expected long option\n");
105 return -1;
106 }
107 *longindex = 0;
108 const _PyOS_LongOption *opt;
109 for (opt = &longopts[*longindex]; opt->name; opt = &longopts[++(*longindex)]) {
110 if (!wcscmp(opt->name, opt_ptr))
111 break;
112 }
113 if (!opt->name) {
114 fprintf(stderr, "unknown option %ls\n", argv[_PyOS_optind - 1]);
115 return '_';
116 }
117 opt_ptr = L"";
118 if (!opt->has_arg) {
119 return opt->val;
120 }
121 if (_PyOS_optind >= argc) {
122 fprintf(stderr, "Argument expected for the %ls options\n",
123 argv[_PyOS_optind - 1]);
124 return '_';
125 }
126 _PyOS_optarg = argv[_PyOS_optind++];
127 return opt->val;
128 }
129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 if (option == 'J') {
Ezio Melotti7c663192012-11-18 13:55:52 +0200131 if (_PyOS_opterr)
132 fprintf(stderr, "-J is reserved for Jython\n");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 return '_';
134 }
Christian Heimes33fe8092008-04-13 13:53:33 +0000135
Victor Stinner6dcb5422019-03-05 02:44:12 +0100136 if ((ptr = wcschr(SHORT_OPTS, option)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 if (_PyOS_opterr)
Ezio Melotti7c663192012-11-18 13:55:52 +0200138 fprintf(stderr, "Unknown option: -%c\n", (char)option);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return '_';
140 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 if (*(ptr + 1) == L':') {
143 if (*opt_ptr != L'\0') {
144 _PyOS_optarg = opt_ptr;
145 opt_ptr = L"";
146 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 else {
149 if (_PyOS_optind >= argc) {
150 if (_PyOS_opterr)
151 fprintf(stderr,
152 "Argument expected for the -%c option\n", (char)option);
153 return '_';
154 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 _PyOS_optarg = argv[_PyOS_optind++];
157 }
158 }
Guido van Rossum2508ade1994-04-14 14:08:22 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 return option;
Guido van Rossum2508ade1994-04-14 14:08:22 +0000161}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000162
163#ifdef __cplusplus
164}
165#endif
166