Skip to content
Myr

Merge branch '7.x'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parents f800a6f, 17cc8d7 · Browse files · 7 files+133 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+133 added−25 removed

  • tests:+74 added−14 removed
  • other:+59 added−11 removed

7 files+133 added−25 removed

modified:click/core.py+3 added-2 removed
362 unmodified lines
363 auto_envvar_prefix = '%s_%s' % (parent.auto_envvar_prefix,364 self.info_name.upper())365 else:366 auto_envvar_prefix = auto_envvar_prefix.upper()367 if auto_envvar_prefix is not None:368 auto_envvar_prefix = auto_envvar_prefix.replace("-", "_")369 self.auto_envvar_prefix = auto_envvar_prefix370371 if color is None and parent is not None:372 color = parent.color
635 unmodified lines
1008 opts = []1009 for param in self.get_params(ctx):1010 rv = param.get_help_record(ctx)1011 if rv is not None:1010 part_a, part_b = rv1011 opts.append((part_a, part_b.replace('\n', ' ')))1012 opts.append(rv)10131014 if opts:1015 with formatter.section('Options'):1016 formatter.write_dl(opts)
951 unmodified lines
modified:click/formatting.py+11 added-5 removed
196 unmodified lines
197 self.write('\n')198 self.write(' ' * (first_col + self.current_indent))199200 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()203204 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')206207 for line in lines[1:]:208 self.write('%*s%s\n' % (first_col + self.current_indent, '', line))209210 if len(lines) > 1:211 # separate long help from next option212 self.write("\n")213 else:214 self.write('\n')215216 @contextmanager
46 unmodified lines
modified:docs/options.rst+7 added-4 removed
591 unmodified lines
592environment variables which is supported for options only. To enable this593feature, the ``auto_envvar_prefix`` parameter needs to be passed to the594script that is invoked. Each command and parameter is then added as an595uppercase underscore-separated variable. If you have a subcommand596called ``foo`` taking an option called ``bar`` and the prefix is597``MY_TOOL``, then the variable is ``MY_TOOL_FOO_BAR``.596called ``run`` taking an option called ``reload`` and the prefix is597``WEB``, then the variable is ``WEB_RUN_RELOAD``.598599Example usage:600601.. click:example::
12 unmodified lines
614615 invoke(greet, env={'GREETER_USERNAME': 'john'},616 auto_envvar_prefix='GREETER')617618When using ``auto_envvar_prefix`` with command groups, the command name needs619to be included in the environment variable, between the prefix and the parameter name, *i.e.* *PREFIX_COMMAND_VARIABLE*.618When using ``auto_envvar_prefix`` with command groups, the command name619needs to be included in the environment variable, between the prefix and620the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a621subcommand called ``run-server`` taking an option called ``host`` and622the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.623624Example:625626.. click:example::
200 unmodified lines
modified:docs/upgrading.rst+31 added
4 unmodified lines
5this is not entirely possible. In case we need to break backwards6compatibility this document gives you information about how to upgrade or7handle backwards compatibility properly.89.. _upgrade-to-7.0:1011Upgrading to 7.012----------------1314Commands that take their name from the decorated function now replace15underscores with dashes. For example, the Python function ``run_server``16will get the command name ``run-server`` now. There are a few options17to address this:1819- To continue with the new behavior, pin your dependency to20 ``Click>=7`` and update any documentation to use dashes.21- To keep existing behavior, add an explicit command name with22 underscores, like ``@click.command("run_server")``.23- To try a name with dashes if the name with underscores was not24 found, pass a ``token_normalize_func`` to the context:2526 .. code-block:: python2728 def normalize(name):29 return name.replace("_", "-")3031 @click.group(context_settings={"token_normalize_func": normalize})32 def group():33 ...3435 @group.command()36 def run_server():37 ...383940.. _upgrade-to-3.2:4142Upgrading to 3.243----------------
88 unmodified lines
modified:tests/test_formatting.py+37 added-1 removed
310 unmodified lines
311 '',312 'Options:',313 ' -f TEXT Output file name [default: out.txt]',314 ' --help Show this message and exit. [default: False]'315 ]315 ]316317318def test_formatting_usage_multiline_option_padding(runner):319 @click.command('foo')320 @click.option('--bar', help='This help message will be padded if it wraps.')321 def cli():322 pass323324 result = runner.invoke(cli, '--help', terminal_width=45)325 assert not result.exception326 assert result.output.splitlines() == [327 'Usage: foo [OPTIONS]',328 '',329 'Options:',330 ' --bar TEXT This help message will be',331 ' padded if it wraps.',332 '',333 ' --help Show this message and exit.'334 ]335336337def test_formatting_usage_no_option_padding(runner):338 @click.command('foo')339 @click.option('--bar', help='This help message will be padded if it wraps.')340 def cli():341 pass342343 result = runner.invoke(cli, '--help', terminal_width=80)344 assert not result.exception345 assert result.output.splitlines() == [346 'Usage: foo [OPTIONS]',347 '',348 'Options:',349 ' --bar TEXT This help message will be padded if it wraps.',350 ' --help Show this message and exit.'351 ]
modified:tests/test_options.py+37 added-13 removed
271 unmodified lines
272 assert not result.exception273 assert 'TEST_ARG1' in result.output274275276def test_show_envvar_auto_prefix_dash_in_command(runner):277 @click.group()278 def cli():279 pass280281 @cli.command()282 @click.option('--baz', show_envvar=True)283 def foo_bar(baz):284 pass285286 result = runner.invoke(cli, ['foo-bar', '--help'],287 auto_envvar_prefix='TEST')288 assert not result.exception289 assert 'TEST_FOO_BAR_BAZ' in result.output290291292def test_custom_validation(runner):293 def validate_pos_int(ctx, param, value):294 if value < 0:295 raise click.BadParameter('Value needs to be positive')
111 unmodified lines
407 assert result.exit_code == 0408 assert result.output == 'Apple\n'409410395def test_multiline_help(runner):411def test_option_help_preserve_paragraphs(runner):412 @click.command()397 @click.option('--foo', help="""398 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 pass425400 i am401402 multiline403 """)404 def cmd(foo):405 click.echo(foo)406407 result = runner.invoke(cmd, ['--help'])426 result = runner.invoke(cmd, ['--help'], )427 assert result.exit_code == 0409 out = result.output.splitlines()410 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.output435436def test_argument_custom_class(runner):437 class CustomArgument(click.Argument):438 def get_default(self, ctx):
118 unmodified lines
modified:CHANGES.rst+7 added
57 unmodified lines
58- ``MissingParameter`` exceptions are printable in the Python59 interpreter. :issue:`1139`60- Fix how default values for file-type options are shown during61 prompts. :issue:`914`62- Fix environment variable automatic generation for commands63 containing ``-``. :issue:`1253`64- Option help text replaces newlines with spaces when rewrapping, but65 preserves paragraph breaks, fixing multiline formatting.66 :issue:`834, 1066, 1397`67- Option help text that is wrapped adds an extra newline at the end to68 distinguish it from the next option. :issue:`1075`69- Consider ``sensible-editor`` when determining the editor to use for70 ``click.edit()``. :pr:`1469`7172
543 unmodified lines