From f72bc30c0b716dbba67e012ce93fb94a42e45f29 Mon Sep 17 00:00:00 2001 From: BlueSkeye_cp <7a9a8e0be1356805ba39aaefd2f72a2ce9bd2015XwR8pJkz> Date: Tue, 2 Oct 2012 08:52:48 +0000 Subject: [PATCH] Added a -d command line argument to XSharp.Test application. Using this argument you can check X# source code files located in another directory than the one where the Cosmos User Kit is installed. Launching the program without argument preserves previous behavior. --- XSharp/source/XSharp.Test/MainForm.cs | 9 ++- XSharp/source/XSharp.Test/Program.cs | 105 +++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/XSharp/source/XSharp.Test/MainForm.cs b/XSharp/source/XSharp.Test/MainForm.cs index ee21b0b01..e63ade158 100644 --- a/XSharp/source/XSharp.Test/MainForm.cs +++ b/XSharp/source/XSharp.Test/MainForm.cs @@ -14,12 +14,13 @@ namespace XSharp.Test { public partial class MainForm : Form { // D:\source\Cosmos\source2\Tests\XSharpCompilerTester\bin\Debug // D:\source\Cosmos\source2\Users\Matthijs\MatthijsPlayground - protected string mPath; public MainForm() { InitializeComponent(); } + internal DirectoryInfo RootDirectory { get; set; } + protected void Test(string aFilename) { tabsMain.TabPages.Add(Path.GetFileNameWithoutExtension(aFilename)); var xTab = tabsMain.TabPages[tabsMain.TabPages.Count - 1]; @@ -52,11 +53,11 @@ namespace XSharp.Test { } private void MainForm_Load(object sender, EventArgs e) { - mPath = CosmosPaths.DebugStubSrc; + if (null == RootDirectory) { RootDirectory = new DirectoryInfo(CosmosPaths.DebugStubSrc); } // For testing - Test(Path.Combine(mPath, "Serial.xs")); + // Test(Path.Combine(RootDirectory.FullName, "Serial.xs")); - var xFiles = Directory.GetFiles(mPath, "*.xs"); + var xFiles = Directory.GetFiles(RootDirectory.FullName, "*.xs"); foreach (var xFile in xFiles) { Test(xFile); } diff --git a/XSharp/source/XSharp.Test/Program.cs b/XSharp/source/XSharp.Test/Program.cs index 8e3b2ff82..0e684f1c0 100644 --- a/XSharp/source/XSharp.Test/Program.cs +++ b/XSharp/source/XSharp.Test/Program.cs @@ -1,15 +1,102 @@ using System; using System.Collections.Generic; -using System.Linq; +using System.IO; +using System.Reflection; +using System.Text; using System.Windows.Forms; -namespace XSharp.Test { - static class Program { - [STAThread] - static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); +namespace XSharp.Test +{ + public static class Program + { + private static void DisplayUsage() + { + StringBuilder builder = new StringBuilder(); + builder.AppendFormat("{0} [-h] [-d ]\r\n", + Assembly.GetExecutingAssembly().GetName().Name); + builder.AppendFormat("-h : display this notice.\r\n"); + builder.AppendFormat("-d : names a directory that will be searched for .xs file.\r\n"); + MessageBox.Show(builder.ToString()); + return; + } + + [STAThread] + public static void Main(string[] args) + { + if (!ParseArgs(args) || _displayUsage) + { + DisplayUsage(); + return; + } + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + MainForm form = new MainForm(); + form.RootDirectory = _rootDirectory; + Application.Run(form); + } + + private static bool ParseArgs(string[] args) + { + bool result = true; + + for (int index = 0; index < args.Length; index++) + { + string scannedArgument = args[index]; + // Defensive programming. + if (string.IsNullOrEmpty(scannedArgument)) { scannedArgument = " "; } + switch (scannedArgument[0]) + { + case '-': + case '/': + if (1 == scannedArgument.Length) { goto default; } + scannedArgument = scannedArgument.Substring(1); + break; + default: + MessageBox.Show(string.Format("Unrecognized command line argument '{0}'.", scannedArgument)); + result = false; + break; + } + switch (scannedArgument.ToLower()) + { + case "h": + _displayUsage = true; + break; + case "d": + if (++index >= args.Length) + { + MessageBox.Show("The -d command line argument must be followed by a directory name."); + result = false; + } + else + { + string directoryPath = args[index] ?? ""; + try { _rootDirectory = new DirectoryInfo(args[index]); } + catch + { + MessageBox.Show(string.Format("'{0}' is not a valid directory path for -d command line argument.", + directoryPath)); + result = false; + break; + } + if (!_rootDirectory.Exists) + { + MessageBox.Show(string.Format("The target directory '{0} doesn't exist.", _rootDirectory.FullName)); + result = false; + } + } + break; + default: + // Must reinitialize scannedArgument to its original value. + scannedArgument = args[index]; + MessageBox.Show(string.Format("Unrecognized command line argument '{0}'.", scannedArgument)); + result = false; + break; + } + } + return result; + } + + private static bool _displayUsage = false; + private static DirectoryInfo _rootDirectory; } - } }