Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a multiline version text by argparse module in Python? [duplicate]

Well, I'm using argparse module but have found that a multiline text as the version information won't be shown well. The result shows that the '\n' will be changed into space ' '. Example:

import argparse
ver_text = 'This is the\nversion text!'
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--version', action='version', version=ver_text)
$ python test.py -v

Result:

This is the version text!

So this is the problem. I wonder how to handle it. Thanks very much!

like image 473
Little_Ye233 Avatar asked Oct 28 '25 22:10

Little_Ye233


1 Answers

If I use

ArgumentParser(formatter_class=RawTextHelpFormatter)

then it displays \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'])

But I don't know if other strings will work in correct way.

like image 149
furas Avatar answered Oct 31 '25 12:10

furas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!