diff options
author | Stephen Finucane <sfinucan@redhat.com> | 2018-10-19 14:55:33 +0100 |
---|---|---|
committer | Stephen Finucane <sfinucan@redhat.com> | 2019-05-09 12:06:47 +0100 |
commit | 584352dcd008d58c433136539b22a6ae9d6c45cc (patch) | |
tree | 0c1af20a4237af114f4997235051d5edea3338da | |
parent | b39b3276ec1d8ba1e0df36791051e24ba2af2fb5 (diff) | |
download | cliff-584352dcd008d58c433136539b22a6ae9d6c45cc.tar.gz |
Stop wildcard importing argparse
There's no point in doing this when we only need two things from the
module. Better to just centralize what we need to do with argparse in
here.
Change-Id: I3e8264f689f128d48042ba88c231351e5ab02b42
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
-rw-r--r-- | cliff/_argparse.py | 25 | ||||
-rw-r--r-- | cliff/command.py | 23 |
2 files changed, 24 insertions, 24 deletions
diff --git a/cliff/_argparse.py b/cliff/_argparse.py index 5358e72..1f1be0f 100644 --- a/cliff/_argparse.py +++ b/cliff/_argparse.py @@ -10,10 +10,10 @@ # License for the specific language governing permissions and limitations # under the License. -"""Special argparse module that allows to bypass abbrev mode.""" +"""Overrides of standard argparse behavior.""" from __future__ import absolute_import -from argparse import * # noqa + import argparse import sys import warnings @@ -104,3 +104,24 @@ class _MutuallyExclusiveGroup(argparse._MutuallyExclusiveGroup): action, conflicting_actions, ) + + +class SmartHelpFormatter(argparse.HelpFormatter): + """Smart help formatter to output raw help message if help contain \n. + + Some command help messages maybe have multiple line content, the built-in + argparse.HelpFormatter wrap and split the content according to width, and + ignore \n in the raw help message, it merge multiple line content in one + line to output, that looks messy. SmartHelpFormatter keep the raw help + message format if it contain \n, and wrap long line like HelpFormatter + behavior. + """ + + def _split_lines(self, text, width): + lines = text.splitlines() if '\n' in text else [text] + wrap_lines = [] + for each_line in lines: + wrap_lines.extend( + super(SmartHelpFormatter, self)._split_lines(each_line, width) + ) + return wrap_lines diff --git a/cliff/command.py b/cliff/command.py index 760832b..c020edc 100644 --- a/cliff/command.py +++ b/cliff/command.py @@ -155,7 +155,7 @@ class Command(object): description=self.get_description(), epilog=self.get_epilog(), prog=prog_name, - formatter_class=_SmartHelpFormatter, + formatter_class=_argparse.SmartHelpFormatter, conflict_handler='ignore', ) for hook in self._hooks: @@ -221,24 +221,3 @@ class Command(object): if ret is not None: return_code = ret return return_code - - -class _SmartHelpFormatter(_argparse.HelpFormatter): - """Smart help formatter to output raw help message if help contain \n. - - Some command help messages maybe have multiple line content, the built-in - argparse.HelpFormatter wrap and split the content according to width, and - ignore \n in the raw help message, it merge multiple line content in one - line to output, that looks messy. SmartHelpFormatter keep the raw help - message format if it contain \n, and wrap long line like HelpFormatter - behavior. - """ - - def _split_lines(self, text, width): - lines = text.splitlines() if '\n' in text else [text] - wrap_lines = [] - for each_line in lines: - wrap_lines.extend( - super(_SmartHelpFormatter, self)._split_lines(each_line, width) - ) - return wrap_lines |