3

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!

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Little_Ye233
  • 105
  • 1
  • 6
  • as i remember there should be some option in argparse for `\n` this but you would have to read in documentation. But it could be for formatting `help` text. – furas Feb 24 '20 at 13:16
  • 1
    Does this answer your question? [Python argparse: How to insert newline in the help text?](https://stackoverflow.com/questions/3853722/python-argparse-how-to-insert-newline-in-the-help-text) – wjandrea Feb 24 '20 at 13:26
  • 3
    tl;dr `parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)` – wjandrea Feb 24 '20 at 13:27

1 Answers1

5

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.

furas
  • 134,197
  • 12
  • 106
  • 148