How to set custom output handlers for argparse in Python?

后端 未结 3 1194
不思量自难忘°
不思量自难忘° 2021-02-15 17:31

I have configured logger to print both onto terminal stdout and to a file so I can have an archive of logging messages that I can refer to.

Tha

3条回答
  •  醉话见心
    2021-02-15 18:18

    While the answer given by @James Mills is great and solves the issue, there is no need for a generator in this case. Hence, the yield is redundant. Another way of achieving the same (without the generator) would be to write your own context manager without using the inbuilt contextlib.contextmanager decorator. Like the following.

    class redirect_stdout_stderr(object):
        def __init__(self, stream):
            # Save the old std streams
            self.old_stream = sys.stdout
            self.old_error_stream = sys.stderr
            self.fstream = stream
    
        def __enter__(self):
            # Change the std streams to your streams when entering
            sys.stdout = self.fstream
            sys.stderr = self.fstream
    
        def __exit__(self, exc_type, exc_value, exc_traceback):
            # Change the std streams back to the original streams while exiting
            sys.stdout = self.old_stream
            sys.stderr = self.old_error_stream
    

    In your case you can do something as follows.

    with redirect_stdout_stderr(logstream):
        # __enter__() is executed
        args = parser.parse_args()
        # __exit__() is executed
    

    Hope this helps!

提交回复
热议问题