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
Provenance
- Commit
8b971f7- Tree
325286f- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
f800a6f- Parent 2
17cc8d7
7 files changed+133 added−25 removed
modified:click/core.py+3 added-2 removed
362 unmodified lines363 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_prefix370 371 if color is None and parent is not None:372 color = parent.color635 unmodified lines1008 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)1013 1014 if opts:1015 with formatter.section('Options'):1016 formatter.write_dl(opts)951 unmodified linesmodified:click/formatting.py+11 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+210+ if len(lines) > 1:211+ # separate long help from next option212+ self.write("\n")213 else:214 self.write('\n')215 216 @contextmanager46 unmodified linesmodified:docs/options.rst+7 added-4 removed
591 unmodified lines592 environment variables which is supported for options only. To enable this593 feature, the ``auto_envvar_prefix`` parameter needs to be passed to the594 script that is invoked. Each command and parameter is then added as an595 uppercase underscore-separated variable. If you have a subcommand596-called ``foo`` taking an option called ``bar`` and the prefix is597-``MY_TOOL``, then the variable is ``MY_TOOL_FOO_BAR``.596+called ``run`` taking an option called ``reload`` and the prefix is597+``WEB``, then the variable is ``WEB_RUN_RELOAD``.598 599 Example usage:600 601 .. click:example::12 unmodified lines614 615 invoke(greet, env={'GREETER_USERNAME': 'john'},616 auto_envvar_prefix='GREETER')617 618-When using ``auto_envvar_prefix`` with command groups, the command name needs619-to be included in the environment variable, between the prefix and the parameter name, *i.e.* *PREFIX_COMMAND_VARIABLE*.618+When using ``auto_envvar_prefix`` with command groups, the command name619+needs to be included in the environment variable, between the prefix and620+the parameter name, *i.e.* ``PREFIX_COMMAND_VARIABLE``. If you have a621+subcommand called ``run-server`` taking an option called ``host`` and622+the prefix is ``WEB``, then the variable is ``WEB_RUN_SERVER_HOST``.623 624 Example:625 626 .. click:example::200 unmodified linesmodified:docs/upgrading.rst+31 added
4 unmodified lines5 this is not entirely possible. In case we need to break backwards6 compatibility this document gives you information about how to upgrade or7 handle backwards compatibility properly.8 9+.. _upgrade-to-7.0:10+11+Upgrading to 7.012+----------------13+14+Commands that take their name from the decorated function now replace15+underscores with dashes. For example, the Python function ``run_server``16+will get the command name ``run-server`` now. There are a few options17+to address this:18+19+- 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:25+26+ .. code-block:: python27+28+ def normalize(name):29+ return name.replace("_", "-")30+31+ @click.group(context_settings={"token_normalize_func": normalize})32+ def group():33+ ...34+35+ @group.command()36+ def run_server():37+ ...38+39+40 .. _upgrade-to-3.2:41 42 Upgrading to 3.243 ----------------88 unmodified linesmodified:tests/test_formatting.py+37 added-1 removed
310 unmodified lines311 '',312 'Options:',313 ' -f TEXT Output file name [default: out.txt]',314 ' --help Show this message and exit. [default: False]'315- ]315+ ]316+317+318+def 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+ pass323+324+ 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+ ]335+336+337+def 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+ pass342+343+ 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 lines272 assert not result.exception273 assert 'TEST_ARG1' in result.output274 275 276+def test_show_envvar_auto_prefix_dash_in_command(runner):277+ @click.group()278+ def cli():279+ pass280+281+ @cli.command()282+ @click.option('--baz', show_envvar=True)283+ def foo_bar(baz):284+ pass285+286+ result = runner.invoke(cli, ['foo-bar', '--help'],287+ auto_envvar_prefix='TEST')288+ assert not result.exception289+ assert 'TEST_FOO_BAR_BAZ' in result.output290+291+292 def 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 lines407 assert result.exit_code == 0408 assert result.output == 'Apple\n'409 410 395-def test_multiline_help(runner):411+def 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+ pass425 400- i am401-402- multiline403- """)404- def cmd(foo):405- click.echo(foo)406-407- 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.output435 436 def test_argument_custom_class(runner):437 class CustomArgument(click.Argument):438 def get_default(self, ctx):118 unmodified linesmodified:CHANGES.rst+7 added
57 unmodified lines58 - ``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`71 72 543 unmodified lines