This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse description formatting
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Phillip.M.Feldman@gmail.com, csernazs
Priority: normal Keywords:

Created on 2018-07-31 19:24 by Phillip.M.Feldman@gmail.com, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg322808 - (view) Author: Phillip M. Feldman (Phillip.M.Feldman@gmail.com) Date: 2018-07-31 19:24
With `argparse`, I'm providing a triple-quoted string via the `description` argument of the constructor.  When I invoke the script with the -h or --help argument, all formatting in the triple-quoted string is lost, i.e., all paragraphs are run together into one giant paragraph, and the result is rather hard to read.

Phillip M. Feldman
msg322811 - (view) Author: Zsolt Cserna (csernazs) * Date: 2018-07-31 19:43
You would need to use the RawTextHelpFormatter as format_class for the constructor. In this case, argparse will apply no re-wrapping of the description.

import argparse

parser = argparse.ArgumentParser(description="""foo
bar
baz""", formatter_class=argparse.RawTextHelpFormatter)
msg322822 - (view) Author: Phillip M. Feldman (Phillip.M.Feldman@gmail.com) Date: 2018-07-31 21:54
That works.  Thanks!

I think that this boils down to a documentation issue.  The following says
that the default behavior is to line-wrap the help messages.  At least to
me, this doesn't imply that whitespace is getting eaten.

RawDescriptionHelpFormatter
<https://docs.python.org/3/library/argparse.html#argparse.RawDescriptionHelpFormatter>
and RawTextHelpFormatter
<https://docs.python.org/3/library/argparse.html#argparse.RawTextHelpFormatter>
give more control over how textual descriptions are displayed. By default,
ArgumentParser
<https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser>
objects line-wrap the description
<https://docs.python.org/3/library/argparse.html#description> and epilog
<https://docs.python.org/3/library/argparse.html#epilog> texts in
command-line help messages:

On Tue, Jul 31, 2018 at 12:43 PM, Zsolt Cserna <report@bugs.python.org>
wrote:

>
> Zsolt Cserna <cserna.zsolt@gmail.com> added the comment:
>
> You would need to use the RawTextHelpFormatter as format_class for the
> constructor. In this case, argparse will apply no re-wrapping of the
> description.
>
> import argparse
>
> parser = argparse.ArgumentParser(description="""foo
> bar
> baz""", formatter_class=argparse.RawTextHelpFormatter)
>
> ----------
> nosy: +csernazs
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue34299>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78480
2018-08-03 01:08:11paul.j3setstatus: open -> closed
stage: resolved
2018-07-31 21:54:48Phillip.M.Feldman@gmail.comsetmessages: + msg322822
2018-07-31 19:43:52csernazssetnosy: + csernazs
messages: + msg322811
2018-07-31 19:24:47Phillip.M.Feldman@gmail.comcreate