PageRenderTime 3ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Merlin/Main/Languages/Ruby/Ruby/Hosting/RubyOptionsParser.cs

https://github.com/PascalN2/ironruby
C# | 291 lines | 216 code | 45 blank | 30 comment | 31 complexity | 8064d0e97fdae0fb51f92504eac653ac MD5 | raw file
Possible License(s): LGPL-2.1, CC-BY-SA-3.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using Microsoft.Scripting;
  20. using Microsoft.Scripting.Hosting;
  21. using Microsoft.Scripting.Hosting.Shell;
  22. using Microsoft.Scripting.Runtime;
  23. using Microsoft.Scripting.Utils;
  24. using IronRuby.Builtins;
  25. using System.Text;
  26. using IronRuby.Runtime;
  27. using System.Security;
  28. namespace IronRuby.Hosting {
  29. public sealed class RubyOptionsParser : OptionsParser<ConsoleOptions> {
  30. private readonly List<string>/*!*/ _loadPaths = new List<string>();
  31. #if DEBUG && !SILVERLIGHT
  32. private ConsoleTraceListener _debugListener;
  33. private sealed class CustomTraceFilter : TraceFilter {
  34. public readonly Dictionary<string, bool>/*!*/ Categories = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
  35. public bool EnableAll { get; set; }
  36. public override bool ShouldTrace(TraceEventCache cache, string source, TraceEventType eventType, int id, string category, object[] args, object data1, object[] data) {
  37. string message = data1 as string;
  38. if (message == null) return true;
  39. bool enabled;
  40. if (Categories.TryGetValue(category, out enabled)) {
  41. return enabled;
  42. } else {
  43. return EnableAll;
  44. }
  45. }
  46. }
  47. private void SetTraceFilter(string/*!*/ arg, bool enable) {
  48. string[] categories = arg.Split(new[] { ';', ','}, StringSplitOptions.RemoveEmptyEntries);
  49. if (categories.Length == 0 && !enable) {
  50. Debug.Listeners.Clear();
  51. return;
  52. }
  53. if (_debugListener == null) {
  54. _debugListener = new ConsoleTraceListener { IndentSize = 4, Filter = new CustomTraceFilter { EnableAll = categories.Length == 0 } };
  55. Debug.Listeners.Add(_debugListener);
  56. }
  57. foreach (var category in categories) {
  58. ((CustomTraceFilter)_debugListener.Filter).Categories[category] = enable;
  59. }
  60. }
  61. #endif
  62. /// <exception cref="Exception">On error.</exception>
  63. protected override void ParseArgument(string arg) {
  64. ContractUtils.RequiresNotNull(arg, "arg");
  65. int colon = arg.IndexOf(':');
  66. string optionName, optionValue;
  67. if (colon >= 0) {
  68. optionName = arg.Substring(0, colon);
  69. optionValue = arg.Substring(colon + 1);
  70. } else {
  71. optionName = arg;
  72. optionValue = null;
  73. }
  74. switch (optionName) {
  75. #region Ruby options
  76. case "-v":
  77. CommonConsoleOptions.PrintVersion = true;
  78. CommonConsoleOptions.Exit = true;
  79. goto case "-W2";
  80. case "-W0":
  81. LanguageSetup.Options["Verbosity"] = 0; // $VERBOSE = nil
  82. break;
  83. case "-W1":
  84. LanguageSetup.Options["Verbosity"] = 1; // $VERBOSE = false
  85. break;
  86. case "-w":
  87. case "-W2":
  88. LanguageSetup.Options["Verbosity"] = 2; // $VERBOSE = true
  89. break;
  90. case "-d":
  91. RuntimeSetup.DebugMode = true; // $DEBUG = true
  92. break;
  93. case "-r":
  94. string libPath = PopNextArg();
  95. LanguageSetup.Options["RequiredLibraries"] = libPath;
  96. break;
  97. case "-e":
  98. LanguageSetup.Options["MainFile"] = "-e";
  99. if (CommonConsoleOptions.Command == null) {
  100. CommonConsoleOptions.Command = String.Empty;
  101. } else {
  102. CommonConsoleOptions.Command += "\n";
  103. }
  104. CommonConsoleOptions.Command += PopNextArg();
  105. break;
  106. #endregion
  107. #if DEBUG && !SILVERLIGHT
  108. case "-DT*":
  109. SetTraceFilter(String.Empty, false);
  110. break;
  111. case "-DT":
  112. SetTraceFilter(PopNextArg(), false);
  113. break;
  114. case "-ET*":
  115. SetTraceFilter(String.Empty, true);
  116. break;
  117. case "-ET":
  118. SetTraceFilter(PopNextArg(), true);
  119. break;
  120. case "-ER":
  121. RubyOptions.ShowRules = true;
  122. break;
  123. case "-save":
  124. LanguageSetup.Options["SavePath"] = optionValue ?? AppDomain.CurrentDomain.BaseDirectory;
  125. break;
  126. case "-load":
  127. LanguageSetup.Options["LoadFromDisk"] = ScriptingRuntimeHelpers.True;
  128. break;
  129. case "-useThreadAbortForSyncRaise":
  130. RubyOptions.UseThreadAbortForSyncRaise = true;
  131. break;
  132. case "-compileRegexps":
  133. RubyOptions.CompileRegexps = true;
  134. break;
  135. #endif
  136. case "-trace":
  137. LanguageSetup.Options["EnableTracing"] = ScriptingRuntimeHelpers.True;
  138. break;
  139. case "-profile":
  140. LanguageSetup.Options["Profile"] = ScriptingRuntimeHelpers.True;
  141. break;
  142. case "-18":
  143. LanguageSetup.Options["Compatibility"] = RubyCompatibility.Ruby18;
  144. break;
  145. case "-19":
  146. LanguageSetup.Options["Compatibility"] = RubyCompatibility.Ruby19;
  147. break;
  148. case "-20":
  149. LanguageSetup.Options["Compatibility"] = RubyCompatibility.Ruby20;
  150. break;
  151. default:
  152. if (arg.StartsWith("-I")) {
  153. if (arg == "-I") {
  154. _loadPaths.Add(PopNextArg());
  155. } else {
  156. _loadPaths.Add(arg.Substring(2));
  157. }
  158. break;
  159. }
  160. #if !SILVERLIGHT
  161. if (arg.StartsWith("-K")) {
  162. LanguageSetup.Options["KCode"] = optionName.Length >= 3 ? RubyEncoding.GetKCodingByNameInitial(optionName[2]) : null;
  163. break;
  164. }
  165. #endif
  166. if (arg == "-X:Interpret") {
  167. LanguageSetup.Options["InterpretedMode"] = ScriptingRuntimeHelpers.True;
  168. break;
  169. }
  170. base.ParseArgument(arg);
  171. if (ConsoleOptions.FileName != null) {
  172. LanguageSetup.Options["MainFile"] = RubyUtils.CanonicalizePath(ConsoleOptions.FileName);
  173. LanguageSetup.Options["Arguments"] = PopRemainingArgs();
  174. CommonConsoleOptions.Exit = false;
  175. }
  176. break;
  177. }
  178. }
  179. protected override void AfterParse() {
  180. var existingSearchPaths =
  181. LanguageOptions.GetSearchPathsOption(LanguageSetup.Options) ??
  182. LanguageOptions.GetSearchPathsOption(RuntimeSetup.Options);
  183. if (existingSearchPaths != null) {
  184. _loadPaths.InsertRange(0, existingSearchPaths);
  185. }
  186. #if !SILVERLIGHT
  187. try {
  188. string rubylib = Environment.GetEnvironmentVariable("RUBYLIB");
  189. if (rubylib != null) {
  190. _loadPaths.AddRange(rubylib.Split(Path.PathSeparator));
  191. }
  192. } catch (SecurityException) {
  193. // nop
  194. }
  195. #endif
  196. LanguageSetup.Options["SearchPaths"] = _loadPaths;
  197. }
  198. public override void GetHelp(out string commandLine, out string[,] options, out string[,] environmentVariables, out string comments) {
  199. string[,] standardOptions;
  200. base.GetHelp(out commandLine, out standardOptions, out environmentVariables, out comments);
  201. string [,] rubyOptions = new string[,] {
  202. // { "-0[octal]", "specify record separator (\0, if no argument)" },
  203. // { "-a", "autosplit mode with -n or -p (splits $_ into $F)" },
  204. // { "-c", "check syntax only" },
  205. // { "-Cdirectory", "cd to directory, before executing your script" },
  206. { "-d", "set debugging flags (set $DEBUG to true)" },
  207. { "-e 'command'", "one line of script. Several -e's allowed. Omit [programfile]" },
  208. // { "-Fpattern", "split() pattern for autosplit (-a)" },
  209. // { "-i[extension]", "edit ARGV files in place (make backup if extension supplied)" },
  210. { "-Idirectory", "specify $LOAD_PATH directory (may be used more than once)" },
  211. #if !SILVERLIGHT
  212. { "-Kkcode", "specifies KANJI (Japanese) code-set: { U, UTF8" },
  213. #endif
  214. // { "-l", "enable line ending processing" },
  215. // { "-n", "assume 'while gets(); ... end' loop around your script" },
  216. // { "-p", "assume loop like -n but print line also like sed" },
  217. { "-rlibrary", "require the library, before executing your script" },
  218. // { "-s", "enable some switch parsing for switches after script name" },
  219. // { "-S", "look for the script using PATH environment variable" },
  220. // { "-T[level]", "turn on tainting checks" },
  221. { "-v", "print version number, then turn on verbose mode" },
  222. { "-w", "turn warnings on for your script" },
  223. { "-W[level]", "set warning level; 0=silence, 1=medium, 2=verbose (default)" },
  224. // { "-x[directory]", "strip off text before #!ruby line and perhaps cd to directory" },
  225. #if DEBUG
  226. { "-opt", "dummy" },
  227. { "-DT", "" },
  228. { "-DT*", "" },
  229. { "-ET", "" },
  230. { "-ET*", "" },
  231. { "-save [path]", "Save generated code to given path" },
  232. { "-load", "Load pre-compiled code" },
  233. { "-useThreadAbortForSyncRaise", "For testing purposes" },
  234. { "-compileRegexps", "Faster throughput, slower startup" },
  235. #endif
  236. { "-trace", "Enable support for set_trace_func" },
  237. { "-profile", "Enable support Clr.profile" },
  238. { "-18", "Ruby 1.8 mode" },
  239. { "-19", "Ruby 1.9 mode" },
  240. { "-20", "Ruby 2.0 mode" },
  241. };
  242. // Append the Ruby-specific options and the standard options
  243. options = ArrayUtils.Concatenate(rubyOptions, standardOptions);
  244. }
  245. }
  246. }