From ea8d2c0d16628be838f57c5ff9876a1fbb5bf209 Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Mon, 22 Apr 2024 13:28:55 +0200 Subject: [PATCH] Improve cmdln.HelpFormatter to obey newline characters --- osc/cmdln.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/osc/cmdln.py b/osc/cmdln.py index e3fafd7b..069677b4 100644 --- a/osc/cmdln.py +++ b/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 = []