【问题标题】:How to get the content of help of argparse, and not to print to console如何获取argparse的帮助内容,而不是打印到控制台
【发布时间】:2021-12-28 09:50:58
【问题描述】:

我正在使用电报机器人,而不是控制台,需要获取帮助内容以发送给用户,而不是打印到控制台,像这样

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--aaa", help="help for aaa")
cmd=["-a", "seta"]
args = parser.parse_args(cmd)
helpstring = parser.print_help() # get the help content for furthur process
telegrambot.send(helpstring)

【问题讨论】:

    标签: python argparse


    【解决方案1】:

    你可以使用parser.format_help方法:

    >>> parser.format_help()
    'usage: [-h] [-a AAA]\n\noptional arguments:\n  -h, --help         show this help message and exit\n  -a AAA, --aaa AAA  help for aaa\n'
    >>> print(parser.format_help())
    usage: [-h] [-a AAA]
    
    optional arguments:
      -h, --help         show this help message and exit
      -a AAA, --aaa AAA  help for aaa
    

    【讨论】:

    • 成功了,非常感谢
    猜你喜欢
    • 2022-01-04
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2011-05-11
    相关资源
    最近更新 更多