I am using optparse to process the command line arguments and I am running into a problem of multiple space lines for the optparse help message.
group.add_option(
'-H',
'--hostname',
action='store',
dest='hostname',
help='Specify the hostname or service/hostname you want to connect to\
If specified -f/--hostfile will be ignored',
metavar='HOSTNAME',)
So I am getting a couple of spaces in the help message after "to" in the help message (because of the indentation).
Specify the hostname or service/hostname you want to connect
to If specified -f/--hostfile will be ignored
I could remove the leading whitespaces in the second line of the help message but that would be unpythonic.
Is there is some pythonic way for removing the whitespaces in the help message.
Austin Phillips' answer covers the case where you want your strings concatenated. If you want to keep the newline there(ie, you want multiline help strings). Check out the textwrap module. Specifically, the dedent function.
Example usage:
>>> from textwrap import dedent
>>> def print_help():
... help = """\
... Specify the hostname or service/hostname you want to connect to
... If specified -f/--hostfile will be ignored
... Some more multiline text here
... and more to demonstrate"""
... print dedent(help)
...
>>> print_help()
Specify the hostname or service/hostname you want to connect to
If specified -f/--hostfile will be ignored
Some more multiline text here
and more to demonstrate
>>>
From the documentation:
textwrap.dedent(text)
Remove any common leading whitespace from every line in text.
This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.