forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonOptionsParser.cs
More file actions
299 lines (250 loc) · 13 KB
/
PythonOptionsParser.cs
File metadata and controls
299 lines (250 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if FEATURE_FULL_CONSOLE
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using Microsoft.Scripting.Hosting.Shell;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
namespace IronPython.Hosting {
public sealed class PythonOptionsParser : OptionsParser<PythonConsoleOptions> {
private List<string> _warningFilters;
public PythonOptionsParser() {
}
/// <exception cref="Exception">On error.</exception>
protected override void ParseArgument(string/*!*/ arg) {
ContractUtils.RequiresNotNull(arg, "arg");
switch (arg) {
case "-B": break; // dont_write_bytecode always true in IronPython
case "-U": break; // unicode always true in IronPython
case "-d": break; // debug output from parser, always False in IronPython
case "-b": // Not shown in help on CPython
LanguageSetup.Options["BytesWarning"] = ScriptingRuntimeHelpers.True;
break;
case "-c":
ConsoleOptions.Command = PeekNextArg();
string[] arguments = PopRemainingArgs();
arguments[0] = arg;
LanguageSetup.Options["Arguments"] = arguments;
break;
case "-?":
ConsoleOptions.PrintUsage = true;
ConsoleOptions.Exit = true;
break;
case "-i":
ConsoleOptions.Introspection = true;
LanguageSetup.Options["Inspect"] = ScriptingRuntimeHelpers.True;
break;
case "-m":
ConsoleOptions.ModuleToRun = PeekNextArg();
LanguageSetup.Options["Arguments"] = PopRemainingArgs();
break;
case "-x":
ConsoleOptions.SkipFirstSourceLine = true;
break;
// TODO: unbuffered stdout?
case "-u": break;
// TODO: create a trace listener?
case "-v":
LanguageSetup.Options["Verbose"] = ScriptingRuntimeHelpers.True;
break;
case "-S":
ConsoleOptions.SkipImportSite = true;
LanguageSetup.Options["NoSite"] = ScriptingRuntimeHelpers.True;
break;
case "-s":
LanguageSetup.Options["NoUserSite"] = ScriptingRuntimeHelpers.True;
break;
case "-E":
ConsoleOptions.IgnoreEnvironmentVariables = true;
LanguageSetup.Options["IgnoreEnvironment"] = ScriptingRuntimeHelpers.True;
break;
case "-t": LanguageSetup.Options["IndentationInconsistencySeverity"] = Severity.Warning; break;
case "-tt": LanguageSetup.Options["IndentationInconsistencySeverity"] = Severity.Error; break;
case "-O":
LanguageSetup.Options["Optimize"] = ScriptingRuntimeHelpers.True;
break;
case "-OO":
LanguageSetup.Options["Optimize"] = ScriptingRuntimeHelpers.True;
LanguageSetup.Options["StripDocStrings"] = ScriptingRuntimeHelpers.True;
break;
case "-Q":
LanguageSetup.Options["DivisionOptions"] = ToDivisionOptions(PopNextArg());
break;
case "-Qold":
case "-Qnew":
case "-Qwarn":
case "-Qwarnall":
LanguageSetup.Options["DivisionOptions"] = ToDivisionOptions(arg.Substring(2));
break;
case "-V":
ConsoleOptions.PrintVersion = true;
ConsoleOptions.Exit = true;
IgnoreRemainingArgs();
break;
case "-W":
if (_warningFilters == null) {
_warningFilters = new List<string>();
}
_warningFilters.Add(PopNextArg());
break;
case "-3":
LanguageSetup.Options["WarnPy3k"] = ScriptingRuntimeHelpers.True;
break;
case "-":
PushArgBack();
LanguageSetup.Options["Arguments"] = PopRemainingArgs();
break;
case "-X:Frames":
LanguageSetup.Options["Frames"] = ScriptingRuntimeHelpers.True;
break;
case "-X:FullFrames":
LanguageSetup.Options["Frames"] = LanguageSetup.Options["FullFrames"] = ScriptingRuntimeHelpers.True;
break;
case "-X:Tracing":
LanguageSetup.Options["Tracing"] = ScriptingRuntimeHelpers.True;
break;
case "-X:GCStress":
int gcStress;
if (!StringUtils.TryParseInt32(PopNextArg(), out gcStress) || (gcStress < 0 || gcStress > GC.MaxGeneration)) {
throw new InvalidOptionException(String.Format("The argument for the {0} option must be between 0 and {1}.", arg, GC.MaxGeneration));
}
LanguageSetup.Options["GCStress"] = gcStress;
break;
case "-X:MaxRecursion":
// we need about 6 frames for starting up, so 10 is a nice round number.
int limit;
if (!StringUtils.TryParseInt32(PopNextArg(), out limit) || limit < 10) {
throw new InvalidOptionException(String.Format("The argument for the {0} option must be an integer >= 10.", arg));
}
LanguageSetup.Options["RecursionLimit"] = limit;
break;
case "-X:EnableProfiler":
LanguageSetup.Options["EnableProfiler"] = ScriptingRuntimeHelpers.True;
break;
case "-X:LightweightScopes":
LanguageSetup.Options["LightweightScopes"] = ScriptingRuntimeHelpers.True;
break;
case "-X:MTA":
ConsoleOptions.IsMta = true;
break;
case "-X:Python30":
LanguageSetup.Options["PythonVersion"] = new Version(3, 0);
break;
case "-X:Debug":
RuntimeSetup.DebugMode = true;
LanguageSetup.Options["Debug"] = ScriptingRuntimeHelpers.True;
break;
case "-X:NoDebug":
string regex = PopNextArg();
try {
LanguageSetup.Options["NoDebug"] = new Regex(regex);
} catch {
throw InvalidOptionValue("-X:NoDebug", regex);
}
break;
case "-X:BasicConsole":
ConsoleOptions.BasicConsole = true;
break;
default:
base.ParseArgument(arg);
if (ConsoleOptions.FileName != null) {
PushArgBack();
LanguageSetup.Options["Arguments"] = PopRemainingArgs();
}
break;
}
}
protected override void AfterParse() {
if (_warningFilters != null) {
LanguageSetup.Options["WarningFilters"] = _warningFilters.ToArray();
}
}
private static PythonDivisionOptions ToDivisionOptions(string/*!*/ value) {
switch (value) {
case "old": return PythonDivisionOptions.Old;
case "new": return PythonDivisionOptions.New;
case "warn": return PythonDivisionOptions.Warn;
case "warnall": return PythonDivisionOptions.WarnAll;
default:
throw InvalidOptionValue("-Q", value);
}
}
public override void GetHelp(out string commandLine, out string[,] options, out string[,] environmentVariables, out string comments) {
string[,] standardOptions;
base.GetHelp(out commandLine, out standardOptions, out environmentVariables, out comments);
#if !IRONPYTHON_WINDOW
commandLine = "Usage: ipy [options] [file.py|- [arguments]]";
#else
commandLine = "Usage: ipyw [options] [file.py|- [arguments]]";
#endif
string[,] pythonOptions = new string[,] {
#if !IRONPYTHON_WINDOW
{ "-v", "Verbose (trace import statements) (also PYTHONVERBOSE=x)" },
#endif
{ "-m module", "run library module as a script"},
{ "-x", "Skip first line of the source" },
{ "-u", "Unbuffered stdout & stderr" },
{ "-O", "generate optimized code" },
{ "-OO", "remove doc strings and apply -O optimizations" },
{ "-E", "Ignore environment variables" },
{ "-Q arg", "Division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew" },
{ "-S", "Don't imply 'import site' on initialization" },
{ "-s", "Don't add user site directory to sys.path" },
{ "-t", "Issue warnings about inconsistent tab usage" },
{ "-tt", "Issue errors for inconsistent tab usage" },
{ "-W arg", "Warning control (arg is action:message:category:module:lineno)" },
{ "-3", "Warn about Python 3.x incompatibilities" },
{ "-X:Frames", "Enable basic sys._getframe support" },
{ "-X:FullFrames", "Enable sys._getframe with access to locals" },
{ "-X:Tracing", "Enable support for tracing all methods even before sys.settrace is called" },
{ "-X:GCStress", "Specifies the GC stress level (the generation to collect each statement)" },
{ "-X:MaxRecursion", "Set the maximum recursion level" },
{ "-X:Debug", "Enable application debugging (preferred over -D)" },
{ "-X:NoDebug <regex>", "Provides a regular expression of files which should not be emitted in debug mode"},
{ "-X:MTA", "Run in multithreaded apartment" },
{ "-X:Python30", "Enable available Python 3.0 features" },
{ "-X:EnableProfiler", "Enables profiling support in the compiler" },
{ "-X:LightweightScopes", "Generate optimized scopes that can be garbage collected" },
{ "-X:BasicConsole", "Use only the basic console features" },
};
// Ensure the combined options come out sorted
string[,] allOptions = ArrayUtils.Concatenate(pythonOptions, standardOptions);
List<string> optName = new List<string>();
List<int> indiciesList = new List<int>();
for (int i = 0; i < allOptions.Length / 2; i++) {
optName.Add(allOptions[i, 0]);
indiciesList.Add(i);
}
int[] indicies = indiciesList.ToArray();
Array.Sort(optName.ToArray(), indicies, StringComparer.InvariantCulture);
options = new string[allOptions.Length / 2, 2];
for (int i = 0; i < indicies.Length; i++) {
options[i, 0] = allOptions[indicies[i], 0];
options[i, 1] = allOptions[indicies[i], 1];
}
Debug.Assert(environmentVariables.GetLength(0) == 0); // No need to append if the default is empty
environmentVariables = new string[,] {
{ "IRONPYTHONPATH", "Path to search for module" },
{ "IRONPYTHONSTARTUP", "Startup module" }
};
}
}
}
#endif