Merge pull request #1478 from pallets/option-help-newline
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix newline wrapping in option help
authored by David Lord · committed by GitHub · parents 70c4493, e4bb120 · Browse files · 4 files·+32 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−20 removed
- tests:+21 added−13 removed
- other:+11 added−7 removed
Provenance
- Commit
3307fc0- Tree
703763b- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
70c4493- Parent 2
e4bb120
4 files changed+32 added−20 removed
modified:click/core.py+1 added-2 removed
943 unmodified lines944 opts = []945 for param in self.get_params(ctx):946 rv = param.get_help_record(ctx)947 if rv is not None:948- part_a, part_b = rv949- opts.append((part_a, part_b.replace('\n', ' ')))948+ opts.append(rv)949 950 if opts:951 with formatter.section('Options'):952 formatter.write_dl(opts)943 unmodified linesmodified:click/formatting.py+7 added-5 removed
196 unmodified lines197 self.write('\n')198 self.write(' ' * (first_col + self.current_indent))199 200 text_width = max(self.width - first_col - 2, 10)201- lines = iter(wrap_text(second, text_width).splitlines())201+ wrapped_text = wrap_text(second, text_width, preserve_paragraphs=True)202+ lines = wrapped_text.splitlines()203+204 if lines:203- self.write(next(lines) + '\n')204- for line in lines:205- self.write('%*s%s\n' % (206- first_col + self.current_indent, '', line))205+ self.write(lines[0] + '\n')206+207+ for line in lines[1:]:208+ self.write('%*s%s\n' % (first_col + self.current_indent, '', line))209 else:210 self.write('\n')211 212 @contextmanager46 unmodified linesmodified:tests/test_options.py+21 added-13 removed
406 unmodified lines407 assert result.exit_code == 0408 assert result.output == 'Apple\n'409 410 411-def test_multiline_help(runner):411+def test_option_help_preserve_paragraphs(runner):412 @click.command()413- @click.option('--foo', help="""414- hello413+ @click.option(414+ "-C",415+ "--config",416+ type=click.Path(),417+ help="""Configuration file to use.418+ 419+ If not given, the environment variable CONFIG_FILE is consulted420+ and used if set. If neither are given, a default configuration421+ file is loaded.""",422+ )423+ def cmd(config):424+ pass425 416- i am417-418- multiline419- """)420- def cmd(foo):421- click.echo(foo)422-423- result = runner.invoke(cmd, ['--help'])426+ result = runner.invoke(cmd, ['--help'], )427 assert result.exit_code == 0425- out = result.output.splitlines()426- assert ' --foo TEXT hello i am multiline' in out428+ assert (429+ " -C, --config PATH Configuration file to use.\n"430+ "{i}\n"431+ "{i}If not given, the environment variable CONFIG_FILE is\n"432+ "{i}consulted and used if set. If neither are given, a default\n"433+ "{i}configuration file is loaded.".format(i=" " * 21)434+ ) in result.output435 436 def test_argument_custom_class(runner):437 class CustomArgument(click.Argument):438 def get_default(self, ctx):118 unmodified linesmodified:CHANGES.rst+3 added
44 unmodified lines45 - Fix how default values for file-type options are shown during46 prompts. :issue:`914`47 - Fix environment variable automatic generation for commands48 containing ``-``. :issue:`1253`49+- Option help text replaces newlines with spaces when rewrapping, but50+ preserves paragraph breaks, fixing multiline formatting.51+ :issue:`834, 1066, 1397`52 53 54 Version 7.055 -----------540 unmodified lines