-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Make error message consistent when invalid script is passed to -File, better error when passed ambiguous arg #4573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -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 term '{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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe argument
is better term
and we can be shorter:
The argument '{0}' is not recognized as the name of a script file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will change
e18b241
to
d6d9505
Compare
StringBuilder possibleParameters = new StringBuilder(); | ||
foreach (string validParameter in validParameters) | ||
{ | ||
if (validParameter.Contains(param)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be validParameter.StartsWith(param, StringComparer.OrdinalIgnoreCase)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking to cover cases like: -format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a test case for this
{ | ||
if (validParameter.Contains(param)) | ||
{ | ||
possibleParameters.Append("\n\t-"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd indent with spaces - we don't indent with tabs anywhere else that I know of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will change
WriteCommandLineError( | ||
string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidArgument, args[i]), | ||
showBanner: false); | ||
WriteCommandLineError(possibleParameters.ToString(), showBanner: true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Showing the banner after the error is weird and not like the other errors I reviewed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that was weird in general, but it seems consistent with other uses of WriteCommandLineError
. Unless you know the reason it was like this, I'll change all uses to not show the banner.
a9d0bac
to
cb97564
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave a comment
#if !CORECLR | ||
"sta", | ||
"mta", | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#if !CORECLR
is no longer relevant. Should we remove the parameters at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fixing that as part of enabling ApartmentState PR and not part of this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closed.
@@ -446,7 +446,6 @@ internal enum KeyboardFlag : uint | |||
internal const int SW_MAX = 11; | |||
|
|||
|
|||
#if !CORECLR // ProcessWindowStyle does Not exist on CoreCLR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
#if !UNIX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense
@@ -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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"did you mean:" - Do you want add something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The possible matches show up after this. See example above in the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closed.
Describe "WindowStyle argument" -Tag Feature { | ||
BeforeAll { | ||
$ExitCodeBadCommandLineParameter = 0xFFFD0000 | ||
Add-Type -Name User32 -Namespace Test -MemberDefinition @" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should exclude this on Unix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do
"@ | ||
} | ||
|
||
It "-WindowStyle <WindowStyle> should work on Windows" -Skip:(!$IsWindows) -TestCases @( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests is excluded on Unix so we could use our pattern "$PSDefaultParameterValues["it:skip"] = ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree
Describe "WindowStyle argument" -Tag Feature { | ||
BeforeAll { | ||
$defaultParamValues = $PSDefaultParameterValues.Clone() | ||
$PSDefaultParameterValues["it:skip"] = (!$IsWindows) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parentheses can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will remove
eb660da
to
951832a
Compare
Not sure why some of the new tests fail in AppVeyor but not on my desktop. Figured it out, need to redirect stderr to stdout. However, I noticed the tests fail on Mac because the exit code isn't being set, but still shows as passed overall. |
bb83bb5
to
ab1862b
Compare
Figured out why it's failing on non-Windows. It appears that exit codes from programs have to be in the range 0-255 and anything greater than 255 is mod 256. I verified this with a simple console c# app. In this case, the value for ExitCodeBadCommandLineParameter mod 256 == 0. Hmmm. Although a breaking change, I think we should conform to libc standards which means this error code should be 64 "command line usage error". We should also update all the other error codes that powershell console returns. In general, I don't think this should be a problem as typically 0 means success and non-0 means error so although a breaking change, I suspect it have limited real world impact. cc @PowerShell/powershell-committee |
be79184
to
5c6b694
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I approve the changes, but there are 2 distinct fixes in the PR, so I think there should be 2 distinct commits.
Interactive add (git add -p
) is an easy way to do that.
@lzybkr are you referring to -WindowStyle and everything else as two commits? |
Yeah, |
@lzybkr sure, I can make that two commits in the same PR. |
Improve error message if ambiguous arg is passed to -File
5c6b694
to
26f7db7
Compare
Split into 3 commits (Thanks @lzybkr for the tip on |
@PowerShell/powershell-committee reviewed this and is ok with changing the exit codes |
@iSazonov, @lzybkr and @SteveL-MSFT thanks as always! |
Since -File is now the positional parameter for powershell.exe, made the error message consistent with
-Command when passed an invalid file.
If ambiguous arg is passed, give a better error message:
Enable -WindowStyle to work
Fix #4351