From 87423d3cc8e3b19a7c332b8d76d407a2550edf06 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 25 Feb 2020 15:31:01 +0100 Subject: [PATCH] Keep explicit newlines with help texts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes a difference for e.g. pytest-xdist: Before: ``` --dist=distmode set mode for distributing tests to exec environments. each: … available environment. loadscope: … grouped by file to any available environment. (default) no: … ``` After: ``` --dist=distmode set mode for distributing tests to exec environments. each: send each test to all available environments. load: load balance by sending any pending test to any available environment. … (default) no: run tests inprocess, don't distribute. ``` This might also result in unexpected changes (hard wrapping), when line endings where used unintentionally, e.g. with: ``` help=""" some long help text """ ``` But the benefits from that are worth it, and it is easy to fix, as will be done for the internal `assertmode` option. --- src/_pytest/config/argparsing.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index b57db92ca..985a3fd1c 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -509,3 +509,15 @@ class DropShorterLongHelpFormatter(argparse.HelpFormatter): formatted_action_invocation = ", ".join(return_list) action._formatted_action_invocation = formatted_action_invocation # type: ignore return formatted_action_invocation + + def _split_lines(self, text, width): + """Wrap lines after splitting on original newlines. + + This allows to have explicit line breaks in the help text. + """ + import textwrap + + lines = [] + for line in text.splitlines(): + lines.extend(textwrap.wrap(line.strip(), width)) + return lines