public override void SetValue(RubyContext /*!*/ context, RubyScope scope, string /*!*/ name, object value) { switch (_id) { // regex: case GlobalVariableId.MatchData: if (scope == null) { throw ReadOnlyError(name); } scope.GetInnerMostClosureScope().CurrentMatch = (value != null) ? RequireType <MatchData>(value, name, "MatchData") : null; return; case GlobalVariableId.MatchLastGroup: case GlobalVariableId.PreMatch: case GlobalVariableId.PostMatch: case GlobalVariableId.EntireMatch: throw ReadOnlyError(name); // exceptions: case GlobalVariableId.CurrentException: context.SetCurrentException(value); return; case GlobalVariableId.CurrentExceptionBacktrace: context.SetCurrentExceptionBacktrace(value); return; // input: case GlobalVariableId.LastInputLine: if (scope == null) { throw ReadOnlyError(name); } scope.GetInnerMostClosureScope().LastInputLine = value; return; case GlobalVariableId.LastInputLineNumber: context.InputProvider.LastInputLineNumber = RequireType <int>(value, name, "Fixnum"); return; case GlobalVariableId.CommandLineArguments: case GlobalVariableId.InputFileName: throw ReadOnlyError(name); // output: case GlobalVariableId.OutputStream: context.StandardOutput = RequireWriteProtocol(context, value, name); return; case GlobalVariableId.ErrorOutputStream: context.StandardErrorOutput = RequireWriteProtocol(context, value, name); break; case GlobalVariableId.InputStream: context.StandardInput = value; return; // separators: case GlobalVariableId.InputContent: throw ReadOnlyError(name); case GlobalVariableId.InputSeparator: context.InputSeparator = (value != null) ? RequireType <MutableString>(value, name, "String") : null; return; case GlobalVariableId.OutputSeparator: context.OutputSeparator = (value != null) ? RequireType <MutableString>(value, name, "String") : null; return; case GlobalVariableId.StringSeparator: // type not enforced: context.StringSeparator = value; return; case GlobalVariableId.ItemSeparator: context.ItemSeparator = (value != null) ? RequireType <MutableString>(value, name, "String") : null; return; // loader: case GlobalVariableId.LoadedFiles: case GlobalVariableId.LoadPath: throw ReadOnlyError(name); // misc: case GlobalVariableId.SafeLevel: context.SetSafeLevel(RequireType <int>(value, name, "Fixnum")); return; case GlobalVariableId.Verbose: context.Verbose = value; return; case GlobalVariableId.CommandLineProgramPath: context.CommandLineProgramPath = (value != null) ? RequireType <MutableString>(value, name, "String") : null; return; case GlobalVariableId.KCode: if (context.RubyOptions.Compatibility < RubyCompatibility.Ruby19) { // MRI calls to_str; we don't do that, it's inconsistent with other globals. // If some app depends on this behavior, it will fail gracefully: context.KCode = RubyEncoding.GetKCodingByNameInitial(RequireType <MutableString>(value, name, "String").GetFirstChar()); Utils.Log(String.Format("Set to {0}", context.KCode), "KCODE"); return; } context.ReportWarning("variable $KCODE is no longer effective"); return; case GlobalVariableId.ChildProcessExitStatus: throw ReadOnlyError(name); default: throw Assert.Unreachable; } }
/// <exception cref="Exception">On error.</exception> protected override void ParseArgument(string arg) { ContractUtils.RequiresNotNull(arg, "arg"); string mainFileFromPath = null; if (arg.StartsWith("-e", StringComparison.Ordinal)) { string command; if (arg == "-e") { command = PopNextArg(); } else { command = arg.Substring(2); } LanguageSetup.Options["MainFile"] = "-e"; if (CommonConsoleOptions.Command == null) { CommonConsoleOptions.Command = String.Empty; } else { CommonConsoleOptions.Command += "\n"; } CommonConsoleOptions.Command += command; return; } if (arg.StartsWith("-S", StringComparison.Ordinal)) { mainFileFromPath = arg == "-S" ? PopNextArg() : arg.Substring(2); } if (arg.StartsWith("-I", StringComparison.Ordinal)) { string includePaths; if (arg == "-I") { includePaths = PopNextArg(); } else { includePaths = arg.Substring(2); } _loadPaths.AddRange(GetPaths(includePaths)); return; } if (arg.StartsWith("-K", StringComparison.Ordinal)) { LanguageSetup.Options["KCode"] = arg.Length >= 3 ? RubyEncoding.GetKCodingByNameInitial(arg[2]) : null; return; } if (arg.StartsWith("-r", StringComparison.Ordinal)) { _requiredPaths.Add((arg == "-r") ? PopNextArg() : arg.Substring(2)); return; } if (arg.StartsWith("-C", StringComparison.Ordinal)) { ConsoleOptions.ChangeDirectory = arg.Substring(2); return; } if (arg.StartsWith("-0", StringComparison.Ordinal) || arg.StartsWith("-C", StringComparison.Ordinal) || arg.StartsWith("-F", StringComparison.Ordinal) || arg.StartsWith("-i", StringComparison.Ordinal) || arg.StartsWith("-T", StringComparison.Ordinal) || arg.StartsWith("-x", StringComparison.Ordinal)) { throw new InvalidOptionException(String.Format("Option `{0}' not supported", arg)); } int colon = arg.IndexOf(':'); string optionName, optionValue; if (colon >= 0) { optionName = arg.Substring(0, colon); optionValue = arg.Substring(colon + 1); } else { optionName = arg; optionValue = null; } switch (optionName) { #region Ruby options case "-a": case "-c": case "--copyright": case "-l": case "-n": case "-p": case "-s": throw new InvalidOptionException(String.Format("Option `{0}' not supported", optionName)); case "-d": LanguageSetup.Options["DebugVariable"] = true; // $DEBUG = true break; case "--version": ConsoleOptions.PrintVersion = true; ConsoleOptions.Exit = true; break; case "-v": ConsoleOptions.DisplayVersion = true; goto case "-W2"; case "-W0": LanguageSetup.Options["Verbosity"] = 0; // $VERBOSE = nil break; case "-W1": LanguageSetup.Options["Verbosity"] = 1; // $VERBOSE = false break; case "-w": case "-W2": LanguageSetup.Options["Verbosity"] = 2; // $VERBOSE = true break; #endregion #if DEBUG && !SILVERLIGHT case "-DT*": SetTraceFilter(String.Empty, false); break; case "-DT": SetTraceFilter(PopNextArg(), false); break; case "-ET*": SetTraceFilter(String.Empty, true); break; case "-ET": SetTraceFilter(PopNextArg(), true); break; case "-ER": RubyOptions.ShowRules = true; break; case "-save": LanguageSetup.Options["SavePath"] = optionValue ?? AppDomain.CurrentDomain.BaseDirectory; break; case "-load": LanguageSetup.Options["LoadFromDisk"] = ScriptingRuntimeHelpers.True; break; case "-useThreadAbortForSyncRaise": RubyOptions.UseThreadAbortForSyncRaise = true; break; case "-compileRegexps": RubyOptions.CompileRegexps = true; break; #endif case "-trace": LanguageSetup.Options["EnableTracing"] = ScriptingRuntimeHelpers.True; break; case "-profile": LanguageSetup.Options["Profile"] = ScriptingRuntimeHelpers.True; break; case "-1.8.6": case "-1.8.7": case "-1.9": case "-2.0": throw new InvalidOptionException(String.Format("Option `{0}' is no longer supported. The compatible Ruby version is 1.9.", optionName)); case "-X": switch (optionValue) { case "AutoIndent": case "TabCompletion": case "ColorfulConsole": throw new InvalidOptionException(String.Format("Option `{0}' not supported", optionName)); } goto default; default: base.ParseArgument(arg); if (ConsoleOptions.FileName != null) { if (mainFileFromPath != null) { ConsoleOptions.FileName = FindMainFileFromPath(mainFileFromPath); } if (ConsoleOptions.Command == null) { SetupOptionsForMainFile(); } else { SetupOptionsForCommand(); } LanguageSetup.Options["ArgumentEncoding"] = #if SILVERLIGHT RubyEncoding.UTF8; #else RubyEncoding.GetRubyEncoding(Console.InputEncoding); #endif } break; } }