From d9c63b72185646ad12e13fa64956c0292d43e79e Mon Sep 17 00:00:00 2001 From: "Steve Lee [MSFT]" Date: Wed, 16 Aug 2017 15:02:43 -0700 Subject: [PATCH] Make invalid arg to -File consistent with -Command Improve error message if ambiguous arg is passed to -File --- .../host/msh/CommandLineParameterParser.cs | 53 +++++++++++++++++-- .../CommandLineParameterParserStrings.resx | 5 +- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index fd6823d6b..b0091c49c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -169,6 +169,30 @@ namespace Microsoft.PowerShell internal class CommandLineParameterParser { + internal static string[] validParameters = { + "psconsoleFile", + "version", + "nologo", + "noexit", +#if !CORECLR + "sta", + "mta", +#endif + "noprofile", + "noninteractive", + "inputformat", + "outputformat", +#if !UNIX + "windowstyle", +#endif + "encodedcommand", + "configurationname", + "file", + "executionpolicy", + "command", + "help" + }; + internal CommandLineParameterParser(PSHostUserInterface hostUI, string bannerText, string helpText) { if (hostUI == null) { throw new PSArgumentNullException("hostUI"); } @@ -657,7 +681,7 @@ namespace Microsoft.PowerShell WriteCommandLineError( "The -module option can only be specified with the -iss option.", showHelp: true, - showBanner: true); + showBanner: false); break; } @@ -880,7 +904,7 @@ namespace Microsoft.PowerShell WriteCommandLineError( CommandLineParameterParserStrings.MissingFileArgument, showHelp: true, - showBanner: true); + showBanner: false); return false; } @@ -924,15 +948,36 @@ namespace Microsoft.PowerShell { WriteCommandLineError( string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidFileArgument, args[i], exceptionMessage), - showBanner: true); + showBanner: false); return false; } if (!System.IO.File.Exists(_file)) { + if (args[i].StartsWith("-") && args[i].Length > 1) + { + string param = args[i].Substring(1, args[i].Length - 1).ToLower(); + StringBuilder possibleParameters = new StringBuilder(); + foreach (string validParameter in validParameters) + { + if (validParameter.Contains(param)) + { + possibleParameters.Append("\n -"); + possibleParameters.Append(validParameter); + } + } + if (possibleParameters.Length > 0) + { + WriteCommandLineError( + string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidArgument, args[i]), + showBanner: false); + WriteCommandLineError(possibleParameters.ToString(), showBanner: false); + return false; + } + } WriteCommandLineError( string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.ArgumentFileDoesNotExist, args[i]), - showBanner: true); + showBanner: false); return false; } diff --git a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx index 9209d37a6..92ef090cd 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx +++ b/src/Microsoft.PowerShell.ConsoleHost/resources/CommandLineParameterParserStrings.resx @@ -252,7 +252,7 @@ EXAMPLES Processing -File '{0}' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again. - The argument '{0}' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter. + The argument '{0}' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Cannot process the command because the value specified with -EncodedArguments is not properly encoded. The value must be Base64 encoded. @@ -269,4 +269,7 @@ EXAMPLES Cannot process the command because -Configuration requires an argument that is a remote endpoint configuration name. Specify this argument and try again. + + Invalid argument '{0}', did you mean: +