Skip to content

Commit 485ec44

Browse files
Add the parameter -Paged to Get-Help to support paging (PowerShell#13374)
1 parent a00741e commit 485ec44

File tree

3 files changed

+128
-6
lines changed

3 files changed

+128
-6
lines changed

assets/files.wxs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,6 +3108,9 @@
31083108
<Component Id="cmpF65478926051451688800150DBAEE4BC">
31093109
<File Id="fil99692697F55D4B1B9D28FDBD439A11BA" KeyPath="yes" Source="$(var.ProductSourcePath)\mscordaccore_$(var.FileArchitecture)_$(var.FileArchitecture)_5.0.20.36411.dll" />
31103110
</Component>
3111+
<Component Id="cmp5485813EC16244898CB9A944AC2BC5E9">
3112+
<File Id="fil6EA784763CF94299BC6E75DF068F05A3" KeyPath="yes" Source="$(var.ProductSourcePath)\Microsoft.PowerShell.Pager.dll" />
3113+
</Component>
31113114
</DirectoryRef>
31123115
</Fragment>
31133116
<Fragment>
@@ -4110,6 +4113,7 @@
41104113
<ComponentRef Id="cmp0BD7D15E0377407FA4C43BFE89F0C12E" />
41114114
<ComponentRef Id="cmpC0FFB3F4FB30438082D2DC0F4E4BF12D" />
41124115
<ComponentRef Id="cmpF65478926051451688800150DBAEE4BC" />
4116+
<ComponentRef Id="cmp5485813EC16244898CB9A944AC2BC5E9" />
41134117
</ComponentGroup>
41144118
</Fragment>
41154119
</Wix>

src/System.Management.Automation/System.Management.Automation.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<!-- the following package(s) are from the powershell org -->
3030
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
3131
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0" />
32+
<PackageReference Include="Microsoft.PowerShell.Pager" Version="1.0.0-preview.2" />
3233
</ItemGroup>
3334

3435
<PropertyGroup>

src/System.Management.Automation/help/HelpCommands.cs

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using System.Management.Automation.Internal;
1515
using System.Management.Automation.Runspaces;
1616
using System.Runtime.InteropServices;
17+
using System.Text;
18+
using Microsoft.PowerShell;
1719
#if !UNIX
1820
using Microsoft.Win32;
1921
#endif
@@ -115,15 +117,21 @@ public SwitchParameter Detailed
115117
[Parameter(ParameterSetName = "AllUsersView")]
116118
public SwitchParameter Full
117119
{
120+
private get => _full;
121+
118122
set
119123
{
120-
if (value.ToBool())
124+
_full = value.ToBool();
125+
126+
if (_full)
121127
{
122128
_viewTokenToAdd = HelpView.FullView;
123129
}
124130
}
125131
}
126132

133+
private bool _full;
134+
127135
/// <summary>
128136
/// Changes the view of HelpObject returned.
129137
/// </summary>
@@ -141,15 +149,24 @@ public SwitchParameter Full
141149
[Parameter(ParameterSetName = "Examples", Mandatory = true)]
142150
public SwitchParameter Examples
143151
{
152+
private get
153+
{
154+
return _examples;
155+
}
156+
144157
set
145158
{
146-
if (value.ToBool())
159+
_examples = value.ToBool();
160+
161+
if (_examples)
147162
{
148163
_viewTokenToAdd = HelpView.ExamplesView;
149164
}
150165
}
151166
}
152167

168+
private bool _examples;
169+
153170
/// <summary>
154171
/// Parameter name.
155172
/// </summary>
@@ -202,6 +219,23 @@ public SwitchParameter Online
202219

203220
private bool _showOnlineHelp;
204221

222+
/// <summary>
223+
/// This parameter, if true, will direct get-help cmdlet to
224+
/// display the help content using Microsoft.PowerShell.Pager on alternate screen buffer.
225+
/// </summary>
226+
[Parameter]
227+
public SwitchParameter Paged
228+
{
229+
get;
230+
set;
231+
}
232+
233+
private Microsoft.PowerShell.Pager Pager => _pager ??= new Microsoft.PowerShell.Pager();
234+
235+
private Pager _pager;
236+
237+
private string _multiItemPagerBuffer;
238+
205239
#if !UNIX
206240
private GraphicalHostReflectionWrapper graphicalHostReflectionWrapper;
207241
private bool showWindow;
@@ -336,6 +370,10 @@ protected override void ProcessRecord()
336370
{
337371
throw PSTraceSource.NewInvalidOperationException(HelpErrors.MultipleOnlineTopicsNotSupported, "Online");
338372
}
373+
else if (Paged && countOfHelpInfos > 1)
374+
{
375+
Pager.Write(_multiItemPagerBuffer);
376+
}
339377

340378
// show errors only if there is no wildcard search or VerboseHelpErrors is true.
341379
if (((countOfHelpInfos == 0) && (!WildcardPattern.ContainsWildcardCharacters(helpRequest.Target)))
@@ -496,7 +534,7 @@ private void GetAndWriteParameterInfo(HelpInfo helpInfo)
496534
{
497535
foreach (PSObject pInfo in pInfos)
498536
{
499-
WriteObject(pInfo);
537+
WriteObjectPaged(pInfo, bufferOutputWhenPaged: false);
500538
}
501539
}
502540
}
@@ -597,7 +635,7 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
597635
{
598636
PSObject objectToReturn = TransformView(helpInfo.FullHelp);
599637
objectToReturn.IsHelpObject = true;
600-
WriteObject(objectToReturn);
638+
WriteObjectPaged(objectToReturn, bufferOutputWhenPaged: false);
601639
}
602640
}
603641
else
@@ -612,12 +650,92 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
612650
}
613651
}
614652

615-
WriteObject(helpInfo.ShortHelp);
653+
WriteObjectPaged(helpInfo.ShortHelp, bufferOutputWhenPaged: true);
616654
}
617655
}
618656
}
619657
}
620658

659+
private void WriteObjectPaged(object sendToPipeline, bool bufferOutputWhenPaged)
660+
{
661+
if (Paged)
662+
{
663+
var helpText = GetHelpOutput();
664+
665+
if (bufferOutputWhenPaged)
666+
{
667+
// The output from GetHelpOutput has all the item in the output.
668+
// We just need to store it and then write to Pager later.
669+
// No need to append them.
670+
_multiItemPagerBuffer = helpText;
671+
}
672+
else
673+
{
674+
Pager.Write(helpText);
675+
}
676+
}
677+
else
678+
{
679+
WriteObject(sendToPipeline);
680+
}
681+
}
682+
683+
private string GetHelpOutput()
684+
{
685+
using System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
686+
687+
ps.AddCommand(@"Microsoft.PowerShell.Core\Get-Help");
688+
689+
if (!string.IsNullOrEmpty(this.Name))
690+
{
691+
ps.AddParameter(nameof(this.Name), this.Name);
692+
}
693+
694+
if (this.Category is not null)
695+
{
696+
ps.AddParameter(nameof(this.Category), this.Category);
697+
}
698+
699+
if (this.Component is not null)
700+
{
701+
ps.AddParameter(nameof(this.Component), this.Component);
702+
}
703+
704+
if (this.Functionality is not null)
705+
{
706+
ps.AddParameter(nameof(this.Functionality), this.Functionality);
707+
}
708+
709+
if (!string.IsNullOrEmpty(this.Path))
710+
{
711+
ps.AddParameter(nameof(this.Path), this.Path);
712+
}
713+
714+
if (this.Role is not null)
715+
{
716+
ps.AddParameter(nameof(this.Role), this.Role);
717+
}
718+
719+
if (this.Examples)
720+
{
721+
ps.AddParameter(nameof(this.Examples), this.Examples);
722+
}
723+
724+
if (this.Full)
725+
{
726+
ps.AddParameter(nameof(this.Full), this.Full);
727+
}
728+
729+
if (this.Parameter is not null)
730+
{
731+
ps.AddParameter(nameof(this.Parameter), this.Parameter);
732+
}
733+
734+
ps.AddCommand(@"Microsoft.PowerShell.Utility\Out-String");
735+
736+
return ps.Invoke<string>().FirstOrDefault();
737+
}
738+
621739
/// <summary>
622740
/// Opens the Uri. System's default application will be used
623741
/// to show the uri.
@@ -888,4 +1006,3 @@ Uri result in
8881006
}
8891007
}
8901008
}
891-

0 commit comments

Comments
 (0)