4

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!

3
  • 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. Commented Feb 24, 2020 at 13:16
  • 1
    Does this answer your question? Python argparse: How to insert newline in the help text? Commented Feb 24, 2020 at 13:26
  • 3
    tl;dr parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) Commented Feb 24, 2020 at 13:27

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

All strings look well. Thanks for your answer! XD

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.