Lit*_*233 3 python command-line-arguments
好吧,我正在使用argparse模块,但发现多行文本作为版本信息无法很好地显示。结果显示'\n'会变成空格''。例子:
import argparse
ver_text = 'This is the\nversion text!'
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version=ver_text)
Run Code Online (Sandbox Code Playgroud)
$ python test.py -v
Run Code Online (Sandbox Code Playgroud)
结果:
This is the version text!
Run Code Online (Sandbox Code Playgroud)
所以这就是问题所在。我想知道如何处理。非常感谢!
如果我使用
ArgumentParser(formatter_class=RawTextHelpFormatter)
Run Code Online (Sandbox Code Playgroud)
然后它显示\n
import argparse
from argparse import RawTextHelpFormatter
ver_text = 'This is the\nversion text!'
parser = argparse.ArgumentParser(formatter_class=RawTextHelpFormatter)
parser.add_argument('-v', '--version', action='version', version=ver_text)
parser.parse_args(['-v'])
Run Code Online (Sandbox Code Playgroud)
但我不知道其他字符串是否能以正确的方式工作。