From d318e7727e1e539446d2792d0e8a9dfea6b2d2e0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 27 Apr 2019 11:20:33 -0700 Subject: [PATCH] Allow passing just a dash as an arg to a file via pwsh (#9479) --- .../host/msh/CommandLineParameterParser.cs | 2 +- test/powershell/Host/ConsoleHost.Tests.ps1 | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs index 2f45e682f..6be2b04bf 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs @@ -1214,7 +1214,7 @@ namespace Microsoft.PowerShell _collectedArgs.Add(new CommandParameter(pendingParameter, arg)); pendingParameter = null; } - else if (!string.IsNullOrEmpty(arg) && SpecialCharacters.IsDash(arg[0])) + else if (!string.IsNullOrEmpty(arg) && SpecialCharacters.IsDash(arg[0]) && arg.Length > 1) { int offset = arg.IndexOf(':'); if (offset >= 0) diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 0c631bb66..f7d1c39dd 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -217,7 +217,7 @@ Describe "ConsoleHost unit tests" -tags "Feature" { $observed | Should -Be $BoolValue } - It "-File '' should return exit code from script" -TestCases @( + It "-File '' should return exit code from script" -TestCases @( @{Filename = "test.ps1"}, @{Filename = "test"} ) { @@ -226,6 +226,23 @@ Describe "ConsoleHost unit tests" -tags "Feature" { & $powershell $testdrive/$Filename $LASTEXITCODE | Should -Be 123 } + + It "A single dash should be passed as an arg" { + $testScript = @' + [CmdletBinding()]param( + [string]$p1, + [string]$p2, + [Parameter(ValueFromPipeline)][string]$InputObject + ) + process{ + $input.replace($p1, $p2) + } +'@ + $testFilePath = Join-Path $TestDrive "test.ps1" + Set-Content -Path $testFilePath -Value $testScript + $observed = echo hello | pwsh $testFilePath e - + $observed | Should -BeExactly "h-llo" + } } Context "-SettingsFile Commandline switch" {