Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[repl] Add support for -e EXPRESSION to the csharp command

  • Loading branch information...
commit d8520bf769b091348a10fdcc6830a9bb37502d2e 1 parent cd6a584
@migueldeicaza migueldeicaza authored
View
10 man/csharp.1
@@ -6,7 +6,7 @@
.SH NAME
csharp, gsharp \- Interactive C# Shell
.SH SYNOPSIS
-.B csharp [--attach PID] [file1 [file2]]
+.B csharp [--attach PID] [-e EXPRESSION] [file1 [file2]]
[options]
.P
.B gsharp [file1 [file2]]
@@ -40,6 +40,11 @@ your C# source code look like this:
Console.WriteLine ("Hello, World");
.fi
.SH OPTIONS
+The commands accept all of the commands that are available to the
+.I mcs
+command, so you can reference assemblies, specify paths, language
+level and so on from the command line. In addition, the following
+command line options are supported:
.TP
.I "\-\-attach"
This is an advanced option and should only be used if you have a deep
@@ -52,6 +57,9 @@ special measures to avoid crashing the target application while using
it. For example, you might have to take the proper locks before
issuing any commands that might affect the target process state, or
sending commands through a method dispatcher.
+.TP
+.I "\-e" EXPRESSION
+This will evaluate the specified C# EXPRESSION and exit
.SH OPERATION
Once you launch the csharp command, you will be greeted with the
interactive prompt:
View
18 mcs/mcs/driver.cs
@@ -42,6 +42,11 @@ class Driver
static readonly char[] argument_value_separator = new char [] { ';', ',' };
+#if !STATIC
+ // The expression to evaluate if -e is passed to the REPL
+ public static string EvalExpression;
+#endif
+
private Driver (CompilerContext ctx)
{
this.ctx = ctx;
@@ -748,6 +753,17 @@ bool UnixParseOption (string arg, ref string [] args, ref int i)
RootContext.LoadDefaultReferences = false;
return true;
+#if !STATIC
+ case "-e":
+ if ((i + 1) >= args.Length){
+ Report.Error (
+ 1900,
+ "-e requires an expression");
+ Environment.Exit (1);
+ }
+ EvalExpression = args [++i];
+ return true;
+#endif
default:
if (arg.StartsWith ("--fatal")){
if (arg.StartsWith ("--fatal=")){
@@ -1646,7 +1662,7 @@ public static void Reset (bool full_flag)
{
CSharpParser.yacc_verbose_flag = 0;
Location.Reset ();
-
+
if (!full_flag)
return;
View
6 mcs/mcs/eval.cs
@@ -148,6 +148,12 @@ internal static ReportPrinter SetPrinter (ReportPrinter report_printer)
}
}
+ public static string StartupEvalExpression {
+ get {
+ return Driver.EvalExpression;
+ }
+ }
+
static void Init ()
{
Init (new string [0]);
View
16 mcs/tools/csharp/repl.cs
@@ -173,7 +173,7 @@ void InitializeUsing ()
Evaluate ("using System; using System.Linq; using System.Collections.Generic; using System.Collections;");
}
- void InitTerminal ()
+ void InitTerminal (bool show_banner)
{
#if ON_DOTNET
is_unix = false;
@@ -195,7 +195,7 @@ void InitTerminal ()
// Report.Stderr = Console.Out;
SetupConsole ();
- if (isatty)
+ if (isatty && show_banner)
Console.WriteLine ("Mono C# Shell, type \"help;\" for help\n\nEnter statements below.");
}
@@ -265,7 +265,7 @@ void ReadEvalPrintLoopWith (ReadLiner readline)
public int ReadEvalPrintLoop ()
{
if (startup_files != null && startup_files.Length == 0)
- InitTerminal ();
+ InitTerminal (startup_files.Length == 0 && Evaluator.StartupEvalExpression == null);
InitializeUsing ();
@@ -274,9 +274,17 @@ public int ReadEvalPrintLoop ()
//
// Interactive or startup files provided?
//
+ string startup_expression = Evaluator.StartupEvalExpression;
+
if (startup_files.Length != 0)
ExecuteSources (startup_files, false);
- else
+ else if (Evaluator.StartupEvalExpression != null){
+ ReadEvalPrintLoopWith (p => {
+ var ret = startup_expression;
+ startup_expression = null;
+ return ret;
+ });
+ } else
ReadEvalPrintLoopWith (GetLine);
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.