5
5
import asyncio
6
6
import functools
7
7
import os
8
+ import subprocess
8
9
import sys
9
10
from typing import Any , Optional , Union
10
11
25
26
" - To view a argument group: --help=ModelConfig\n "
26
27
" - To view a single argument: --help=max-num-seqs\n "
27
28
" - To search by keyword: --help=max\n "
28
- " - To list all groups: --help=listgroup" )
29
+ " - To list all groups: --help=listgroup\n "
30
+ " - To view help with pager: --help=page" )
29
31
30
32
31
33
async def listen_for_disconnect (request : Request ) -> None :
@@ -190,6 +192,24 @@ def _validate_truncation_size(
190
192
return truncate_prompt_tokens
191
193
192
194
195
+ def _output_with_pager (text : str ):
196
+ """Output text using scrolling view if available and appropriate."""
197
+
198
+ pagers = ['less -R' , 'more' ]
199
+ for pager_cmd in pagers :
200
+ try :
201
+ proc = subprocess .Popen (pager_cmd .split (),
202
+ stdin = subprocess .PIPE ,
203
+ text = True )
204
+ proc .communicate (input = text )
205
+ return
206
+ except (subprocess .SubprocessError , OSError , FileNotFoundError ):
207
+ continue
208
+
209
+ # No pager worked, fall back to normal print
210
+ print (text )
211
+
212
+
193
213
def show_filtered_argument_or_group_from_help (parser : argparse .ArgumentParser ,
194
214
subcommand_name : list [str ]):
195
215
@@ -208,16 +228,24 @@ def show_filtered_argument_or_group_from_help(parser: argparse.ArgumentParser,
208
228
if arg .startswith ('--help=' ):
209
229
search_keyword = arg .split ('=' , 1 )[1 ]
210
230
231
+ # Enable paged view for full help
232
+ if search_keyword == 'page' :
233
+ help_text = parser .format_help ()
234
+ _output_with_pager (help_text )
235
+ sys .exit (0 )
236
+
211
237
# List available groups
212
238
if search_keyword == 'listgroup' :
213
- print ( "\n Available argument groups:" )
239
+ output_lines = [ "\n Available argument groups:" ]
214
240
for group in parser ._action_groups :
215
241
if group .title and not group .title .startswith (
216
242
"positional arguments" ):
217
- print (f" - { group .title } " )
243
+ output_lines . append (f" - { group .title } " )
218
244
if group .description :
219
- print (" " + group .description .strip ())
220
- print ()
245
+ output_lines .append (" " +
246
+ group .description .strip ())
247
+ output_lines .append ("" )
248
+ _output_with_pager ("\n " .join (output_lines ))
221
249
sys .exit (0 )
222
250
223
251
# For group search
@@ -229,7 +257,7 @@ def show_filtered_argument_or_group_from_help(parser: argparse.ArgumentParser,
229
257
formatter .add_text (group .description )
230
258
formatter .add_arguments (group ._group_actions )
231
259
formatter .end_section ()
232
- print (formatter .format_help ())
260
+ _output_with_pager (formatter .format_help ())
233
261
sys .exit (0 )
234
262
235
263
# For single arg
@@ -243,10 +271,10 @@ def show_filtered_argument_or_group_from_help(parser: argparse.ArgumentParser,
243
271
matched_actions .append (action )
244
272
245
273
if matched_actions :
246
- print ( f"\n Parameters matching '{ search_keyword } ':\n " )
274
+ header = f"\n Parameters matching '{ search_keyword } ':\n "
247
275
formatter = parser ._get_formatter ()
248
276
formatter .add_arguments (matched_actions )
249
- print ( formatter .format_help ())
277
+ _output_with_pager ( header + formatter .format_help ())
250
278
sys .exit (0 )
251
279
252
280
print (f"\n No group or parameter matching '{ search_keyword } '" )
0 commit comments