我想修改argparse帮助的格式。有没有办法做到这一点? 这就是它的样子:
-s [SERVER], --server [SERVER]
Expects address of server
但我想改变它:
-s, --server <SERVER> Expects address of server
line_2 of help
是否可以手动覆盖整个帮助?或者还有其他pythonic方法吗?
答案 0 :(得分:1)
argparse中有格式化程序类到原始输出帮助消息:
argparse.RawTextHelpFormatter
https://docs.python.org/2/library/argparse.html#argparse.RawTextHelpFormatter
parser = argparse.ArgumentParser(
description='My command',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-s", "--server",
default='',
help="Expects address of server\n line_2 of help")
答案 1 :(得分:1)
我正在解决类似的问题,这是我的解决方案:
parser.add_argument('-s, --server <SERVER>',
action='version',
# version is only in case someone actually tries to input
# '-s, --server <SERVER>' as parameter
version='This is a help output only, use -s or --server instead',
help="help description for server")
parser.add_argument('-s', '-server', '--server',
dest='server', # to fall under namespace.server
type=server_regex, # custom definition to validate input
help=argparse.SUPPRESS)
这些代码的输出为:
optional arguments:
-h, --help show this help message and exit
-s, --server <SERVER>
help description for server
更好的间距将需要修改帮助格式, 对我来说这已经足够了