14
14
using System . Management . Automation . Internal ;
15
15
using System . Management . Automation . Runspaces ;
16
16
using System . Runtime . InteropServices ;
17
+ using System . Text ;
18
+ using Microsoft . PowerShell ;
17
19
#if ! UNIX
18
20
using Microsoft . Win32 ;
19
21
#endif
@@ -115,15 +117,21 @@ public SwitchParameter Detailed
115
117
[ Parameter ( ParameterSetName = "AllUsersView" ) ]
116
118
public SwitchParameter Full
117
119
{
120
+ private get => _full ;
121
+
118
122
set
119
123
{
120
- if ( value . ToBool ( ) )
124
+ _full = value . ToBool ( ) ;
125
+
126
+ if ( _full )
121
127
{
122
128
_viewTokenToAdd = HelpView . FullView ;
123
129
}
124
130
}
125
131
}
126
132
133
+ private bool _full ;
134
+
127
135
/// <summary>
128
136
/// Changes the view of HelpObject returned.
129
137
/// </summary>
@@ -141,15 +149,24 @@ public SwitchParameter Full
141
149
[ Parameter ( ParameterSetName = "Examples" , Mandatory = true ) ]
142
150
public SwitchParameter Examples
143
151
{
152
+ private get
153
+ {
154
+ return _examples ;
155
+ }
156
+
144
157
set
145
158
{
146
- if ( value . ToBool ( ) )
159
+ _examples = value . ToBool ( ) ;
160
+
161
+ if ( _examples )
147
162
{
148
163
_viewTokenToAdd = HelpView . ExamplesView ;
149
164
}
150
165
}
151
166
}
152
167
168
+ private bool _examples ;
169
+
153
170
/// <summary>
154
171
/// Parameter name.
155
172
/// </summary>
@@ -202,6 +219,23 @@ public SwitchParameter Online
202
219
203
220
private bool _showOnlineHelp ;
204
221
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
+
205
239
#if ! UNIX
206
240
private GraphicalHostReflectionWrapper graphicalHostReflectionWrapper ;
207
241
private bool showWindow ;
@@ -336,6 +370,10 @@ protected override void ProcessRecord()
336
370
{
337
371
throw PSTraceSource . NewInvalidOperationException ( HelpErrors . MultipleOnlineTopicsNotSupported , "Online" ) ;
338
372
}
373
+ else if ( Paged && countOfHelpInfos > 1 )
374
+ {
375
+ Pager . Write ( _multiItemPagerBuffer ) ;
376
+ }
339
377
340
378
// show errors only if there is no wildcard search or VerboseHelpErrors is true.
341
379
if ( ( ( countOfHelpInfos == 0 ) && ( ! WildcardPattern . ContainsWildcardCharacters ( helpRequest . Target ) ) )
@@ -496,7 +534,7 @@ private void GetAndWriteParameterInfo(HelpInfo helpInfo)
496
534
{
497
535
foreach ( PSObject pInfo in pInfos )
498
536
{
499
- WriteObject ( pInfo ) ;
537
+ WriteObjectPaged ( pInfo , bufferOutputWhenPaged : false ) ;
500
538
}
501
539
}
502
540
}
@@ -597,7 +635,7 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
597
635
{
598
636
PSObject objectToReturn = TransformView ( helpInfo . FullHelp ) ;
599
637
objectToReturn . IsHelpObject = true ;
600
- WriteObject ( objectToReturn ) ;
638
+ WriteObjectPaged ( objectToReturn , bufferOutputWhenPaged : false ) ;
601
639
}
602
640
}
603
641
else
@@ -612,12 +650,92 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
612
650
}
613
651
}
614
652
615
- WriteObject ( helpInfo . ShortHelp ) ;
653
+ WriteObjectPaged ( helpInfo . ShortHelp , bufferOutputWhenPaged : true ) ;
616
654
}
617
655
}
618
656
}
619
657
}
620
658
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
+
621
739
/// <summary>
622
740
/// Opens the Uri. System's default application will be used
623
741
/// to show the uri.
@@ -888,4 +1006,3 @@ Uri result in
888
1006
}
889
1007
}
890
1008
}
891
-
0 commit comments