开发者

Quoting long strings without newlines in Python

开发者 https://www.devze.com 2023-01-03 21:11 出处:网络
I am trying to write a long string in Python that gets displayed as the help item of an OptParser option.In my source c开发者_StackOverflow中文版ode .py file, I\'d like to place newlines so that my co

I am trying to write a long string in Python that gets displayed as the help item of an OptParser option. In my source c开发者_StackOverflow中文版ode .py file, I'd like to place newlines so that my code doesn't spend new lines. However, I don't want those newlines to affect how that string is displayed when the code is run. For example, I want to write:

   parser.add_option("--my-option", dest="my_option", nargs=2, default=None,
              help='''Here is a long description of my option.  It does many things
              but I want the shell to decide how to display this explanation. However,
              I want newlines in this string.''')

the above way of doing things will make it so when I call my program with --help, the explanation of my-option will have many gaps in it.

How can I fix this?

thanks.


You can concatenate string literals just like in C, so "foo" "bar" is the same as "foobar", meaning this should do what you want:

parser.add_option("--my-option", dest="my_option", nargs=2, default=None, 
          help="Here is a long description of my option.  It does many things "
          "but I want the shell to decide how to display this explanation. However, "
          "I want newlines in this string.") 

Note that you don't need backslash line continuation characters because the whole thing is within parentheses.


Just take advantage of string-literal juxtaposition -- in Python, like in C, if two string literals are next to each other with just whitespace in-between (including newlines), the compiler will merge them into a single string literal, ignoring the whitespace. I.e.:

parser.add_option("--my-option", dest="my_option", nargs=2, default=None,
                  help='Here is a long description of my option.  It does '
                       'many things but I want the shell to decide how to '
                       'display this explanation. However, I do NOT want '
                       'newlines in this string.')

I'm assuming that by "I want" you actually mean "I do NOT want" here;-).

Note the trailing spaces at the end of each piece of the literal you're passing as help: you have to put them there explicitly, or else the juxtaposition would make "words" such as doesmany and so on;-).

Note that, very differently from what some answers and comments claim, you most definitely don't need ugly, redundant pluses and-or backslashes here -- no pluses needed because the compiler applies juxtaposition for you, no backslashes needed either because this set of physical lines is a single logical line (thanks to the open paren not being closed yet until the end of the set of physical lines!).


Default help formatter reformats the string so you can use newlines in the help string as you like:

>>> from optparse import OptionParser
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help='''aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaa
... b
... c                            d
... e
... f''')
>>> parser.print_help()
Usage: bpython [options]

Options:
  -h, --help            show this help message and exit
  --my-option=MY_OPTION
                        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                        aaa b c                            d e f

To remove any common leading space you could use textwrap.dedent:

>>> from optparse import OptionParser
>>> from textwrap import dedent
>>> parser = OptionParser()
>>> parser.add_option('--my-option', help=dedent('''\
...     Here is a long description of my option.  It does many things
...     but I want the shell to decide how to display this
...     explanation. However, I want newlines in this string.'''))
>>> parser.print_help()
Usage:  [options]

Options:
  -h, --help            show this help message and exit
  --my-option=MY_OPTION
                        Here is a long description of my option.  It does many
                        things but I want the shell to decide how to display
                        this explanation. However, I want newlines in this
                        string.


This works:

foo = "abc" + \
    "def" + \
    "ghi"

print foo
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号