@@ -342,7 +342,12 @@ def _collect_command_values(parameters, getter, *, should_include):
342342
343343
344344def _render_api_result (result : Any , instance : Any ) -> None :
345- _render_result (result , json_mode = instance .json , jq_expression = instance .jq )
345+ _render_result (
346+ result ,
347+ json_mode = instance .json ,
348+ jq_expression = instance .jq ,
349+ max_records = getattr (instance , 'max_records' , None ),
350+ )
346351
347352
348353def _create_standard_command_call (api_class : type , target_method : str , command_parameters ):
@@ -515,9 +520,13 @@ def _format_text_item(item: Any) -> str:
515520 return '\n ' .join (lines )
516521
517522
518- def _render_result (result : Any , * , json_mode : bool , jq_expression : Optional [str ]):
523+ def _render_result (result : Any , * , json_mode : bool , jq_expression : Optional [str ], max_records : Optional [ int ] = None ):
519524 div = False
525+ emitted = 0
520526 for item in _iter_output_items (result ):
527+ if max_records is not None and emitted >= max_records :
528+ break
529+
521530 item = _apply_jq (item , jq_expression )
522531
523532 if json_mode :
@@ -529,6 +538,8 @@ def _render_result(result: Any, *, json_mode: bool, jq_expression: Optional[str]
529538 div = True
530539 click .echo (_format_text_item (item ))
531540
541+ emitted += 1
542+
532543
533544def make_api_command (api_class : type , command_name : str , target_method : str , * , parent_class : type ):
534545 raw_parameters = list (_iter_command_parameters (api_class , target_method ))
@@ -561,6 +572,15 @@ def make_api_command(api_class: type, command_name: str, target_method: str, *,
561572 default_getter = lambda item : item [1 ].default ,
562573 )
563574
575+ if target_method .endswith ('_iterator' ):
576+ namespace ['__annotations__' ]['max_records' ] = Optional [int ]
577+ namespace ['max_records' ] = classyclick .Option (
578+ '-m' ,
579+ type = click .IntRange (min = 1 ),
580+ default = None ,
581+ help = 'Maximum number of records to emit.' ,
582+ )
583+
564584 return _build_command_class (parent_class , namespace )
565585
566586
0 commit comments