Calculate a suitable column width for positional arguments

Overwrite the method add_arguments in the class
OpenStackHelpFormatter to calculate a suitable columnt width
(max_help_position) for the positional arguments.

example output before this patch:

    alarm-threshold-create
                        Create a new alarm based on computed statistics.
    alarm-threshold-update
                        Update an existing alarm based on computed statistics.
    alarm-update        Update an existing alarm (Deprecated).

example output after this patch:

    alarm-threshold-create    Create a new alarm based on computed statistics.
    alarm-threshold-update    Update an existing alarm based on computed
                              statistics.
    alarm-update              Update an existing alarm (Deprecated).

Change-Id: I945e46bb9e0309b960e3322261d9d6ff9fa35b52
Closes-Bug: #1326471
This commit is contained in:
Christian Berendt
2014-06-04 21:03:42 +02:00
parent 5c8a85e386
commit d864c23aa8

View File

@@ -396,6 +396,18 @@ class CeilometerShell(object):
class HelpFormatter(argparse.HelpFormatter):
INDENT_BEFORE_ARGUMENTS = 6
MAX_WIDTH_ARGUMENTS = 32
def add_arguments(self, actions):
for action in filter(lambda x: not x.option_strings, actions):
for choice in action.choices:
length = len(choice) + self.INDENT_BEFORE_ARGUMENTS
if(length > self._max_help_position and
length <= self.MAX_WIDTH_ARGUMENTS):
self._max_help_position = length
super(HelpFormatter, self).add_arguments(actions)
def start_section(self, heading):
# Title-case the headings
heading = '%s%s' % (heading[0].upper(), heading[1:])