From 0a3621b8ff158b8e1071ccfac38455aba1661725 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 6 Nov 2015 20:05:52 -0800 Subject: [PATCH] Refactor host command-line parsing Supports a syntax more similar to powershell.exe on Windows, with --file and --command options, as well as the automatic execution of a script by name, or a command via string. Prepares for reading of stdin. --- src/host/main.cs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/host/main.cs b/src/host/main.cs index 929cb02839..98f0477aef 100644 --- a/src/host/main.cs +++ b/src/host/main.cs @@ -77,7 +77,6 @@ namespace Microsoft.Samples.PowerShell.Host /// /// Creates and initiates the listener instance. /// - /// This parameter is not used. private static void Main(string[] args) { // Custom argument parsing @@ -90,26 +89,44 @@ namespace Microsoft.Samples.PowerShell.Host bool hasNext = (i+1) < args.Length; string nextArg = hasNext ? args[i+1] : string.Empty; - if (hasNext && arg == "--file") + // lone argument is a script + if (!hasNext && arg.EndsWith(".ps1")) + { + initialScript = Path.GetFullPath(arg); + } + // lone argument is an inline script + else if (!hasNext) + { + initialScript = arg; + } + // --file was specified + else if (hasNext && (arg == "--file" || arg == "-f")) { initialScript = Path.GetFullPath(nextArg); ++i; } - else if (arg.EndsWith(".ps1")) + // --command was specified + else if (hasNext && (arg == "--command" || arg == "-c")) { - initialScript = Path.GetFullPath(arg); + if (nextArg == "-") + { + initialScript = "\"TODO: read stdin using Console.OpenStandardInput\""; + } + else + { + initialScript = nextArg; + } + ++i; } + // --working-dir (not on PowerShell for Windows, may be removed) else if (hasNext && arg == "--working-dir") { Directory.SetCurrentDirectory(nextArg); ++i; } } - if (string.IsNullOrEmpty(initialScript)) - { - initialScript = string.Join(" ", args); - } } + // TODO: check for input on stdin // Create the listener and run it PSListenerConsoleSample listener = new PSListenerConsoleSample(initialScript);