From d1edcb38bdd12a17a9b59d0224dad6e6447be37f Mon Sep 17 00:00:00 2001 From: George Fleming Date: Thu, 14 Apr 2016 09:09:52 -0700 Subject: [PATCH 1/5] Redo command-line parsing, add -noninteractive and -encodedcommand options --- .../main.cs | 153 +++++++++++++----- 1 file changed, 115 insertions(+), 38 deletions(-) diff --git a/src/Microsoft.PowerShell.CoreConsoleHost/main.cs b/src/Microsoft.PowerShell.CoreConsoleHost/main.cs index ba3f0be180..697774b59f 100644 --- a/src/Microsoft.PowerShell.CoreConsoleHost/main.cs +++ b/src/Microsoft.PowerShell.CoreConsoleHost/main.cs @@ -14,6 +14,30 @@ namespace Microsoft.PowerShell.CoreConsoleHost public static class Program { + private enum Options {Help, File, Command, NoProfile, NonInteractive, EncodedCommand, Invalid}; + private class CommandOptions + { + public Options opt; + public string optString; + + public CommandOptions(Options o, string s) + { + opt = o; + optString = s; + } + } + + private static CommandOptions[] commandOptions = new CommandOptions[] + { + new CommandOptions(Options.Help, "help"), + new CommandOptions(Options.Help, "?"), + new CommandOptions(Options.File, "file"), + new CommandOptions(Options.Command, "command"), + new CommandOptions(Options.NoProfile, "noprofile"), + new CommandOptions(Options.NonInteractive, "noninteractive"), + new CommandOptions(Options.EncodedCommand, "encodedcommand") + }; + /// /// Creates and initiates the listener instance. /// @@ -25,6 +49,7 @@ namespace Microsoft.PowerShell.CoreConsoleHost // here eliminates the need for a custom native host. PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); + // Argument parsing string initialScript = null; bool loadProfiles = true; @@ -37,15 +62,33 @@ namespace Microsoft.PowerShell.CoreConsoleHost bool hasNext = (i+1) < args.Length; string nextArg = hasNext ? args[i+1] : string.Empty; - // --help was specified - if (arg == "--help" || arg == "-h") + // options start with "-" or "--" + + string option; + if (arg.StartsWith("-")) { - Console.WriteLine(@" -usage: powershell[.exe] [ (--help | -h) ] + if (arg.StartsWith("--")) + { + option = arg.Remove(0, 2); + } + else + { + option = arg.Remove(0, 1); + } + + Options bestOption = FindOption(option); + + switch (bestOption) + { + case Options.Help: + Console.WriteLine(@" +usage: powershell[.exe] [ (--help | -h | -?) ] [ (--file | -f) ] [