[issue12806] argparse: Hybrid help text formatter

paul j3 report at bugs.python.org
Wed Jul 30 23:35:30 CEST 2014


paul j3 added the comment:

In http://bugs.python.org/issue22029 argparse
CSS white-space: like control for individual text blocks

I propose a set of `str` subclasses that can be used to define the wrapping style of individual text blocks. The idea is adapted from the HTML '<pre>' tag, and the CSS white-space: option.

`argparse.WhitespaceStyle` is a cover class that defines various utility methods, including `_str_format` which handles all of the `%` formatting.  The individual subclasses implement their own version of `_split_lines` and `_fill_text`.  I chose a standard set of classes based on the CSS white-space options:

Normal() - full white space compression and wrapping.  This is the default default of text in `argparse`.

Pre() - preformatting, the same as the `Raw` formatters

NoWrap() - Pre plus whitespace compression

PreLine()

PreWrap()

In `HelpFormatter`, `_split_lines`, `_fill_lines`, `_str_format` delegate the action to text's own methods.  Plain text is handled as `Normal()`.

I also defined a `WSList` class.  This is a list of Style class objects. It has the same API as the Style classes, iterating over the items.

Where possible these methods try to return an object of the same type as self.
------------------

Here I demonstrate two ways that these classes could be used to implement a hybrid formatter.

The first is a simple adaptation of the `PareML` formatter from `paraformatter.py`.  It shows how a custom style class could be defined.

The second is defines a `preformat` function, which converts the text block into a `WSList`, a list of style text objects.  The wrappable paragraphs are `Normal()`, the preformatted indented lines are `Pre()`. Blank lines are `Pre(' ')`.

I've explored writing a `Hanging` class, which performs a hanging indent on list item sentences.

----------
Added file: http://bugs.python.org/file36168/try_12806_4.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12806>
_______________________________________


More information about the Python-bugs-list mailing list