How to set custom output handlers for argparse in Python?

后端 未结 3 1191
不思量自难忘°
不思量自难忘° 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:02

    There seems to be no way to do this through the API.

    However, you can do the following:

    class LoggingArgumentParser(argparse.ArgumentParser):
        """Custom ArgumentPaarser that overrides _print_message"""
    
        def _print_message(self, message, file=None):
            if message:
                logger.write(message)
    

提交回复
热议问题