mirror of
https://github.com/openSUSE/osc.git
synced 2025-01-11 16:36:14 +01:00
Improve cmdln.HelpFormatter to obey newline characters
This commit is contained in:
parent
d4ec8e7f3a
commit
ea8d2c0d16
14
osc/cmdln.py
14
osc/cmdln.py
@ -6,6 +6,7 @@ A modern, lightweight alternative to cmdln.py from https://github.com/trentm/cmd
|
||||
import argparse
|
||||
import inspect
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
|
||||
def option(*args, **kwargs):
|
||||
@ -78,6 +79,19 @@ def hide(value=True):
|
||||
|
||||
|
||||
class HelpFormatter(argparse.RawDescriptionHelpFormatter):
|
||||
def _split_lines(self, text, width):
|
||||
# remove the leading and trailing whitespaces to avoid printing unwanted blank lines
|
||||
text = text.strip()
|
||||
|
||||
result = []
|
||||
for line in text.splitlines():
|
||||
if not line.strip():
|
||||
# textwrap normally returns [] on a string that contains only whitespaces; we want [""] to print a blank line
|
||||
result.append("")
|
||||
else:
|
||||
result.extend(textwrap.wrap(line, width))
|
||||
return result
|
||||
|
||||
def _format_action(self, action):
|
||||
if isinstance(action, argparse._SubParsersAction):
|
||||
parts = []
|
||||
|
Loading…
Reference in New Issue
Block a user