@@ -557,7 +557,7 @@ class _ParserThreadLocals(threading.local):
557557 # pass the destination stream to the formatter factory, this transient value
558558 # provides the context needed to synchronize Rich's rendering with the specific
559559 # capabilities of the destination file descriptor. It is managed via the
560- # _set_output_file () context manager.
560+ # output_to () context manager.
561561 current_output_file : IO [str ] | None = None
562562
563563
@@ -568,8 +568,14 @@ class Cmd2ArgumentParser(argparse.ArgumentParser):
568568 _thread_locals : ClassVar [_ParserThreadLocals ] = _ParserThreadLocals ()
569569
570570 @contextlib .contextmanager
571- def _set_output_file (self , file : IO [str ] | None ) -> Iterator [None ]:
572- """Context manager to temporarily set the current output file."""
571+ def output_to (self , file : IO [str ] | None ) -> Iterator [None ]:
572+ """Context manager to temporarily set the output stream during argparse operations.
573+
574+ This is helpful for redirecting output from functions like `parse_args()`, which
575+ default to `sys.stdout` and lack a `file` argument.
576+
577+ :param file: the file stream to use for output
578+ """
573579 previous = self ._thread_locals .current_output_file
574580 self ._thread_locals .current_output_file = file
575581 try :
@@ -635,58 +641,20 @@ def __init__(
635641 self .description : HelpContent | None # type: ignore[assignment]
636642 self .epilog : HelpContent | None # type: ignore[assignment]
637643
638- def parse_args_custom_stdout (
639- self ,
640- stdout : IO [str ],
641- args : list [str ] | None = None ,
642- namespace : argparse .Namespace | None = None ,
643- ) -> argparse .Namespace :
644- """Parse arguments while directing help and usage output to a custom stdout stream.
645-
646- This method is particularly useful when you need to capture help output without
647- globally redirecting sys.stdout.
648-
649- :param stdout: the stream to use for help and usage output
650- :param args: optional list of arguments to parse. If None, uses sys.argv[1:].
651- :param namespace: optional namespace to populate. If None, a new Namespace is created.
652- :return: the parsed namespace
653- """
654- with self ._set_output_file (stdout ):
655- return self .parse_args (args , namespace )
656-
657- def parse_known_args_custom_stdout (
658- self ,
659- stdout : IO [str ],
660- args : list [str ] | None = None ,
661- namespace : argparse .Namespace | None = None ,
662- ) -> tuple [argparse .Namespace , list [str ]]:
663- """Parse known arguments while directing help and usage output to a custom stdout stream.
664-
665- This method is particularly useful when you need to capture help output without
666- globally redirecting sys.stdout.
667-
668- :param stdout: the stream to use for help and usage output
669- :param args: optional list of arguments to parse. If None, uses sys.argv[1:].
670- :param namespace: optional namespace to populate. If None, a new Namespace is created.
671- :return: a tuple containing the parsed namespace and a list of unknown arguments
672- """
673- with self ._set_output_file (stdout ):
674- return self .parse_known_args (args , namespace )
675-
676644 def print_usage (self , file : IO [str ] | None = None ) -> None : # type:ignore[override]
677645 """Override to ensure the formatter is aware of the target file."""
678646 if file is None :
679647 file = self ._thread_locals .current_output_file
680648
681- with self ._set_output_file (file ):
649+ with self .output_to (file ):
682650 super ().print_usage (file )
683651
684652 def print_help (self , file : IO [str ] | None = None ) -> None : # type:ignore[override]
685653 """Override to ensure the formatter is aware of the target file."""
686654 if file is None :
687655 file = self ._thread_locals .current_output_file
688656
689- with self ._set_output_file (file ):
657+ with self .output_to (file ):
690658 super ().print_help (file )
691659
692660 def get_subparsers_action (self ) -> "argparse._SubParsersAction[Cmd2ArgumentParser]" :
@@ -891,7 +859,7 @@ def error(self, message: str) -> NoReturn:
891859 else :
892860 formatted_message += "\n " + line
893861
894- with self ._set_output_file (sys .stderr ):
862+ with self .output_to (sys .stderr ):
895863 self .print_usage (sys .stderr )
896864
897865 # Use console to add style since it will respect ALLOW_STYLE's value.
0 commit comments