How to set custom output handlers for argparse in Python? -
i have configured logger print both onto terminal stdout , file can have archive of logging messages can refer to.
that accomplished adding filehandler logging object. easy peasy.
what want accomplish make argparse log same file along logs stdout when encounters parsing errors. far prints stdout. looked in argparse documentation can't find setting different output stream or pipe argparse.
is possible do? how?
looking @ argparse.py source code there doesn't seem way configure behaviour.
my suggestion(s) be:
- file bug report patch :)
override/patch:
print_*method(s)errormethod.
the print_* method(s) seem take optional file argument defaults _sys.stdout.
update: alternatively whereby redirect sys.stdout temporarily while parse arguments:
from contextlib import contextmanager @contextmanager def redirect_stdout_stderr(stream): old_stdout = sys.stdout old_stderr = sys.stderr sys.stdout = stream sys.stderr = stream try: yield finally: sys.stdout = old_stdout sys.stderr = old_stderr redirct_stdout_stderr(logstream): args = parser.parse_args()
Comments
Post a Comment