Skip to content

Commit 20d917c

Browse files
authored
Fix issue PowerShell#12013 - not allow mixed dash and slash in command line parameter prefix (PowerShell#15142)
1 parent 2f3f399 commit 20d917c

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,8 @@ internal static string GetConfigurationNameFromGroupPolicy()
608608
return (switchKey: string.Empty, shouldBreak: false);
609609
}
610610

611-
if (!CharExtensions.IsDash(switchKey[0]) && switchKey[0] != '/')
611+
char firstChar = switchKey[0];
612+
if (!CharExtensions.IsDash(firstChar) && firstChar != '/')
612613
{
613614
// then it's a file
614615
--argIndex;
@@ -622,7 +623,7 @@ internal static string GetConfigurationNameFromGroupPolicy()
622623
switchKey = switchKey.Substring(1);
623624

624625
// chop off the second dash so we're agnostic wrt specifying - or --
625-
if (!string.IsNullOrEmpty(switchKey) && CharExtensions.IsDash(switchKey[0]))
626+
if (!string.IsNullOrEmpty(switchKey) && CharExtensions.IsDash(firstChar) && switchKey[0] == firstChar)
626627
{
627628
switchKey = switchKey.Substring(1);
628629
}

test/xUnit/csharp/test_CommandLineParser.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,47 @@ public static void TestParameterIs_Wrong_Value(params string[] commandLine)
143143
Assert.Contains("-noprofile", cpp.ErrorMessage);
144144
}
145145

146+
[Theory]
147+
[InlineData("-Version")]
148+
[InlineData("--Version")]
149+
[InlineData("/Version")]
150+
public static void TestParameter_Dash_Or_Slash(params string[] commandLine)
151+
{
152+
var cpp = new CommandLineParameterParser();
153+
154+
cpp.Parse(commandLine);
155+
156+
Assert.False(cpp.AbortStartup);
157+
Assert.False(cpp.NoExit);
158+
Assert.True(cpp.NonInteractive);
159+
Assert.False(cpp.ShowBanner);
160+
Assert.False(cpp.ShowShortHelp);
161+
Assert.False(cpp.NoPrompt);
162+
Assert.True(cpp.ShowVersion);
163+
Assert.True(cpp.SkipProfiles);
164+
Assert.Null(cpp.ErrorMessage);
165+
}
166+
167+
[Theory]
168+
[InlineData("/-Version")]
169+
[InlineData("-/Version")]
170+
public static void TestParameter_Wrong_Dash_And_Slash(params string[] commandLine)
171+
{
172+
var cpp = new CommandLineParameterParser();
173+
174+
cpp.Parse(commandLine);
175+
176+
Assert.True(cpp.AbortStartup);
177+
Assert.False(cpp.NoExit);
178+
Assert.False(cpp.NonInteractive);
179+
Assert.False(cpp.ShowBanner);
180+
Assert.True(cpp.ShowShortHelp);
181+
Assert.False(cpp.NoPrompt);
182+
Assert.False(cpp.ShowVersion);
183+
Assert.False(cpp.SkipProfiles);
184+
Assert.Contains(commandLine[0], cpp.ErrorMessage);
185+
}
186+
146187
[Theory]
147188
[InlineData("-Version")]
148189
[InlineData("-V")]

0 commit comments

Comments
 (0)