From 279993bf39e7e804d73db00346ebdd5db37a5427 Mon Sep 17 00:00:00 2001 From: Sergey Vasin Date: Wed, 16 Jan 2019 03:25:49 +0300 Subject: [PATCH] Change Get-Help cmdlet -Parameter parameter so it accepts string arrays. (#8454) --- .../engine/InitialSessionState.cs | 2 +- .../help/HelpCommands.cs | 36 +++++++++++++++---- .../engine/Help/HelpSystem.Tests.ps1 | 25 +++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 2b97ba36d..ba5021082 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -4135,7 +4135,7 @@ param( ${Examples}, [Parameter(ParameterSetName='Parameters', Mandatory=$true)] - [string] + [string[]] ${Parameter}, [string[]] diff --git a/src/System.Management.Automation/help/HelpCommands.cs b/src/System.Management.Automation/help/HelpCommands.cs index 0fd25bb31..ab269c2c9 100644 --- a/src/System.Management.Automation/help/HelpCommands.cs +++ b/src/System.Management.Automation/help/HelpCommands.cs @@ -157,7 +157,7 @@ namespace Microsoft.PowerShell.Commands /// Support WildCard strings as supported by WildcardPattern class. /// [Parameter(ParameterSetName = "Parameters", Mandatory = true)] - public string Parameter { set; get; } + public string[] Parameter { get; set; } /// /// Gets and sets list of Component's to search on. @@ -433,7 +433,27 @@ namespace Microsoft.PowerShell.Commands } /// - /// Gets the parameter info for patterns identified Parameter property. + /// Gets the parameter info for patterns identified by Parameter property. + /// + /// HelpInfo object to look for the parameter. + /// Array of parameter infos. + private PSObject[] GetParameterInfo(HelpInfo helpInfo) + { + List parameterInfosList = new List(Parameter.Length); + + foreach (var parameter in Parameter) + { + foreach (var parameterInfo in helpInfo.GetParameter(parameter)) + { + parameterInfosList.Add(parameterInfo); + } + } + + return parameterInfosList.ToArray(); + } + + /// + /// Gets the parameter info for patterns identified by Parameter property. /// Writes the parameter info(s) to the output stream. An error is thrown /// if a parameter with a given pattern is not found. /// @@ -441,7 +461,8 @@ namespace Microsoft.PowerShell.Commands private void GetAndWriteParameterInfo(HelpInfo helpInfo) { s_tracer.WriteLine("Searching parameters for {0}", helpInfo.Name); - PSObject[] pInfos = helpInfo.GetParameter(Parameter); + + PSObject[] pInfos = GetParameterInfo(helpInfo); if ((pInfos == null) || (pInfos.Length == 0)) { @@ -479,7 +500,7 @@ namespace Microsoft.PowerShell.Commands if ((cat & supportedCategories) == 0) { - if (!string.IsNullOrEmpty(Parameter)) + if (Parameter != null) { throw PSTraceSource.NewArgumentException("Parameter", HelpErrors.ParamNotSupported, "-Parameter"); @@ -540,7 +561,7 @@ namespace Microsoft.PowerShell.Commands // show inline help if (showFullHelp) { - if (!string.IsNullOrEmpty(Parameter)) + if (Parameter != null) { GetAndWriteParameterInfo(helpInfo); } @@ -553,9 +574,10 @@ namespace Microsoft.PowerShell.Commands } else { - if (!string.IsNullOrEmpty(Parameter)) + if (Parameter != null) { - PSObject[] pInfos = helpInfo.GetParameter(Parameter); + PSObject[] pInfos = GetParameterInfo(helpInfo); + if ((pInfos == null) || (pInfos.Length == 0)) { return; diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index af9e1b351..54f6b7771 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -478,6 +478,31 @@ Describe 'help can be found for AllUsers Scope' -Tags @('Feature', 'RequireAdmin } } +Describe "Get-Help should accept arrays as the -Parameter parameter value" -Tags @('CI') { + + BeforeAll { + $userHelpRoot = GetCurrentUserHelpRoot + + ## Clear all help from user scope. + Remove-Item $userHelpRoot -Force -ErrorAction SilentlyContinue -Recurse + UpdateHelpFromLocalContentPath -ModuleName 'Microsoft.PowerShell.Core' -Scope 'CurrentUser' + + ## Delete help from global scope if it exists. + $currentCulture = (Get-Culture).Name + $coreHelpFilePath = Join-Path $PSHOME -ChildPath $currentCulture -AdditionalChildPath 'System.Management.Automation.dll-Help.xml' + if (Test-Path $coreHelpFilePath) { + Remove-Item $coreHelpFilePath -Force -ErrorAction SilentlyContinue + } + } + + It "Should return help objects for two parameters" { + $help = Get-Help -Name Get-Command -Parameter Verb, Noun + $help | Should -HaveCount 2 + $help[0].Name | Should -BeExactly 'Verb' + $help[1].Name | Should -BeExactly 'Noun' + } +} + Describe "Help failure cases" -Tags Feature { It "An error is returned for a topic that doesn't exist: " -TestCases @( @{ command = "help" },