Skip to content

Commit b4d75ad

Browse files
authored
Merge pull request #5091 from blueyed/showhelp-ini-options
Improve output of ini options in --help
2 parents ec6d0fa + b2ce6f3 commit b4d75ad

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

changelog/5091.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The output for ini options in ``--help`` has been improved.

src/_pytest/helpconfig.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,48 @@ def pytest_cmdline_main(config):
141141

142142

143143
def showhelp(config):
144+
import textwrap
145+
144146
reporter = config.pluginmanager.get_plugin("terminalreporter")
145147
tw = reporter._tw
146148
tw.write(config._parser.optparser.format_help())
147149
tw.line()
148-
tw.line()
149150
tw.line(
150151
"[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:"
151152
)
152153
tw.line()
153154

154155
columns = tw.fullwidth # costly call
156+
indent_len = 24 # based on argparse's max_help_position=24
157+
indent = " " * indent_len
155158
for name in config._parser._ininames:
156159
help, type, default = config._parser._inidict[name]
157160
if type is None:
158161
type = "string"
159-
spec = "%s (%s)" % (name, type)
160-
line = " %-24s %s" % (spec, help)
161-
tw.line(line[:columns])
162+
spec = "%s (%s):" % (name, type)
163+
tw.write(" %s" % spec)
164+
spec_len = len(spec)
165+
if spec_len > (indent_len - 3):
166+
# Display help starting at a new line.
167+
tw.line()
168+
helplines = textwrap.wrap(
169+
help,
170+
columns,
171+
initial_indent=indent,
172+
subsequent_indent=indent,
173+
break_on_hyphens=False,
174+
)
175+
176+
for line in helplines:
177+
tw.line(line)
178+
else:
179+
# Display help starting after the spec, following lines indented.
180+
tw.write(" " * (indent_len - spec_len - 2))
181+
wrapped = textwrap.wrap(help, columns - indent_len, break_on_hyphens=False)
182+
183+
tw.line(wrapped[0])
184+
for line in wrapped[1:]:
185+
tw.line(indent + line)
162186

163187
tw.line()
164188
tw.line("environment variables:")

0 commit comments

Comments
 (0)