diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index ad809b7098..fdefdc5c87 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -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); } diff --git a/test/xUnit/csharp/test_CommandLineParser.cs b/test/xUnit/csharp/test_CommandLineParser.cs index 9e942bb005..c57960797b 100644 --- a/test/xUnit/csharp/test_CommandLineParser.cs +++ b/test/xUnit/csharp/test_CommandLineParser.cs @@ -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")]