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.

Author GraylinKim
Recipients GraylinKim
Date 2011-08-22.00:46:16
SpamBayes Score 5.468265e-07
Marked as misclassified No
Message-id <1313973978.37.0.422477813424.issue12806@psf.upfronthosting.co.za>
In-reply-to
Content
When using argparse I frequently run into situations where my helper text is a mix of prose and bullets or options. I need the RawTextFormatter for the bullets, and I need the default formatter for the prose (so the line wraps intelligently).

The current HelpFormatter classes are marked as public by name only, so sub-classing them with overrides to get the desired functionality isn't great unless it gets pushed upstream. To that end, I've attached a subclass implementation that I've been using for the following effect:

Example:
    >>> parser = argparse.ArgumentParser(formatter_class=FlexiFormatter)
    >>> parser.add_argument('--example', help='''\
    ...     This argument's help text will have this first long line\
    ...     wrapped to fit the target window size so that your text\
    ...     remains flexible.
    ...
    ...         1. This option list
    ...         2. is still persisted
    ...         3. and the option strings get wrapped like this with an\
    ...            indent for readability.
    ...
    ...     You must use backslashes at the end of lines to indicate that\
    ...     you want the text to wrap instead of preserving the newline.
    ...    
    ...     As with docstrings, the leading space to the text block is\
    ...     ignored.
    ... ''')
    >>> parser.parse_args(['-h'])

    usage: argparse_formatter.py [-h] [--example EXAMPLE]

    optional arguments:
      -h, --help         show this help message and exit
      --example EXAMPLE  This argument's help text will have this first
                         long line wrapped to fit the target window size
                         so that your text remains flexible.

                             1. This option list
                             2. is still persisted
                             3. and the option strings get wrapped like
                                this with an indent for readability.

                         You must use backslashes at the end of lines to
                         indicate that you want the text to wrap instead
                         of preserving the newline.

                         As with docstrings, the leading space to the
                         text block is ignored.


                                 1. This option list
                                 2. is still persisted
                                 3. and the option strings get wrapped like
                                    this with an indent for readability.

                             You must use backslashes at the end of lines to
                             indicate that you want the text to wrap instead
                             of preserving the newline.

                             As with docstrings, the leading space to the
                             text block is ignored.


If there is interest in this sort of thing I'd be happy to fix it up for inclusion.
History
Date User Action Args
2011-08-22 00:46:18GraylinKimsetrecipients: + GraylinKim
2011-08-22 00:46:18GraylinKimsetmessageid: <1313973978.37.0.422477813424.issue12806@psf.upfronthosting.co.za>
2011-08-22 00:46:17GraylinKimlinkissue12806 messages
2011-08-22 00:46:17GraylinKimcreate