Fix issue #12013 - not allow mixed dash and slash in command line parameter prefix (#15142)

This commit is contained in:
David Bar-On
2021-04-13 13:20:28 -07:00
committed by GitHub
parent 2f3f39906f
commit 20d917c086
2 changed files with 44 additions and 2 deletions
@@ -608,7 +608,8 @@ namespace Microsoft.PowerShell
return (switchKey: string.Empty, shouldBreak: false);
}
if (!CharExtensions.IsDash(switchKey[0]) && switchKey[0] != '/')
char firstChar = switchKey[0];
if (!CharExtensions.IsDash(firstChar) && firstChar != '/')
{
// then it's a file
--argIndex;
@@ -622,7 +623,7 @@ namespace Microsoft.PowerShell
switchKey = switchKey.Substring(1);
// chop off the second dash so we're agnostic wrt specifying - or --
if (!string.IsNullOrEmpty(switchKey) && CharExtensions.IsDash(switchKey[0]))
if (!string.IsNullOrEmpty(switchKey) && CharExtensions.IsDash(firstChar) && switchKey[0] == firstChar)
{
switchKey = switchKey.Substring(1);
}
@@ -143,6 +143,47 @@ namespace PSTests.Parallel
Assert.Contains("-noprofile", cpp.ErrorMessage);
}
[Theory]
[InlineData("-Version")]
[InlineData("--Version")]
[InlineData("/Version")]
public static void TestParameter_Dash_Or_Slash(params string[] commandLine)
{
var cpp = new CommandLineParameterParser();
cpp.Parse(commandLine);
Assert.False(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.True(cpp.NonInteractive);
Assert.False(cpp.ShowBanner);
Assert.False(cpp.ShowShortHelp);
Assert.False(cpp.NoPrompt);
Assert.True(cpp.ShowVersion);
Assert.True(cpp.SkipProfiles);
Assert.Null(cpp.ErrorMessage);
}
[Theory]
[InlineData("/-Version")]
[InlineData("-/Version")]
public static void TestParameter_Wrong_Dash_And_Slash(params string[] commandLine)
{
var cpp = new CommandLineParameterParser();
cpp.Parse(commandLine);
Assert.True(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.False(cpp.NonInteractive);
Assert.False(cpp.ShowBanner);
Assert.True(cpp.ShowShortHelp);
Assert.False(cpp.NoPrompt);
Assert.False(cpp.ShowVersion);
Assert.False(cpp.SkipProfiles);
Assert.Contains(commandLine[0], cpp.ErrorMessage);
}
[Theory]
[InlineData("-Version")]
[InlineData("-V")]