我正在使用CLI实用程序。在控制台中使用功能-- help
添加文档字符串来调用模块的帮助时,我遇到了这样一个事实,即所有添加的文本都显示为连续的,不中断的消息。如何将字符串彼此分开?我试图在行尾添加\n
,但这不起作用。
def createParser():
parser = argparse.ArgumentParser(
prog='samplefind',
description="""
Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file.
From command line run python sfind.py .
To view all available options: python sfind.py -h .
"""
答案 0 :(得分:2)
使用formatter_class=argparse.RawTextHelpFormatter
保留帮助字符串中的所有空格。这是argparse
应用程序帮助字符串,而不是docstring
。看起来可能有些难看:
parser = argparse.ArgumentParser(
prog='samplefind',
formatter_class=argparse.RawTextHelpFormatter,
description="""
Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file.
From command line run python sfind.py .
To view all available options: python sfind.py -h .
""")
从终端:
py bla.py -h用法:samplefind [-h]
Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file. From command line run python sfind.py . To view all available options: python sfind.py -h .
请注意,这包括从行首开始的空格,新行以及所有内容。