Make invalid arg to -File consistent with -Command

Improve error message if ambiguous arg is passed to -File
This commit is contained in:
Steve Lee [MSFT] 2017-08-16 15:02:43 -07:00
parent 7ae955fa73
commit d9c63b7218
2 changed files with 53 additions and 5 deletions

View file

@ -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;
}

View file

@ -252,7 +252,7 @@ EXAMPLES
<value>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.</value>
</data>
<data name="ArgumentFileDoesNotExist" xml:space="preserve">
<value>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.</value>
<value>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.</value>
</data>
<data name="BadArgsValue" xml:space="preserve">
<value>Cannot process the command because the value specified with -EncodedArguments is not properly encoded. The value must be Base64 encoded.</value>
@ -269,4 +269,7 @@ EXAMPLES
<data name="MissingConfigurationNameArgument" xml:space="preserve">
<value>Cannot process the command because -Configuration requires an argument that is a remote endpoint configuration name. Specify this argument and try again.</value>
</data>
<data name="InvalidArgument" xml:space="preserve">
<value>Invalid argument '{0}', did you mean:</value>
</data>
</root>