Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

Python-Click Console Output to Logfile

I would like to capture the Python-click Console output in order to write it into a logfile for example when an argument was not specified. Is there a way I can capture the console output from click. I have tried it with this code:

@click.group(cls=DefaultGroup, default="subcmd", default_if_no_args=True)
@click.option('--verbose', '-v', is_flag=True, help="Increase output verbosity level")
@click.pass_context
def main(ctx, verbose):
    ctx.obj["verbose"] = verbose
    lines = click.get_text_stream("stderr").readlines()
    #further code to write the stderr lines into to the logfile

@main.command("subcmd", context_settings=dict(allow_extra_args=True,))
@click.argument('arg1', required=True, default="Value1", type=click.Choice(['Value1', 'Value2']))
@click.argument('arg2', required=True)
@click.pass_context
def subCmd(ctx, arg1, arg2):
    pass

if __name__ == "__main__":
    main(obj={})

Unfortunately, this gives me an error

Exception has occurred: UnsupportedOperation
not readable
question from:https://stackoverflow.com/questions/65848994/python-click-console-output-to-logfile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...