Make it possible to define aliases for the secondary option of boolean flags. This fixes #420
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 851ce7f · Browse files · 4 files·+56 added−3 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+56 added−3 removed
- tests:+26 added−0 removed
- other:+30 added−3 removed
Provenance
- Commit
89d3bfd- Tree
fec7d2c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
851ce7f
4 files changed+56 added−3 removed
modified:click/core.py+6 added-3 removed
1515 unmodified lines1516 split_char = decl[:1] == '/' and ';' or '/'1517 if split_char in decl:1518 first, second = decl.split(split_char, 1)1519 first = first.rstrip()1520+ if first:1520- possible_names.append(split_opt(first))1521- opts.append(first)1522- secondary_opts.append(second.lstrip())1521+ possible_names.append(split_opt(first))1522+ opts.append(first)1523+ second = second.lstrip()1524+ if second:1525+ secondary_opts.append(second.lstrip())1526 else:1527 possible_names.append(split_opt(decl))1528 opts.append(decl)1529 195 unmodified linesmodified:docs/options.rst+23 added
199 unmodified lines200 201 if __name__ == '__main__':202 log()203 204+.. versionchanged:: 6.0205+206+If you want to define an alias for the second option only, then you will207+need to use leading whitespace to disambiguate the format string:208+209+Example:210+211+.. click:example::212+213+ import sys214+215+ @click.command()216+ @click.option('--shout/--no-shout', ' /S', default=False)217+ def info(shout):218+ rv = sys.platform219+ if shout:220+ rv = rv.upper() + '!!!!111'221+ click.echo(rv)222+223+.. click:run::224+225+ invoke(info, args=['--help'])226+227 Feature Switches228 ----------------229 230 In addition to boolean flags, there are also feature switches. These are465 unmodified linesmodified:tests/test_options.py+26 added
302 unmodified lines303 304 result = runner.invoke(cmd, ['--help'])305 assert 'I am a help text' in result.output306 assert 'you wont see me' not in result.output307+308+309+def test_aliases_for_flags(runner):310+ @click.command()311+ @click.option('--warnings/--no-warnings', ' /-W', default=True)312+ def cli(warnings):313+ click.echo(warnings)314+315+ result = runner.invoke(cli, ['--warnings'])316+ assert result.output == 'True\n'317+ result = runner.invoke(cli, ['--no-warnings'])318+ assert result.output == 'False\n'319+ result = runner.invoke(cli, ['-W'])320+ assert result.output == 'False\n'321+322+ @click.command()323+ @click.option('--warnings/--no-warnings', '-w', default=True)324+ def cli_alt(warnings):325+ click.echo(warnings)326+327+ result = runner.invoke(cli_alt, ['--warnings'])328+ assert result.output == 'True\n'329+ result = runner.invoke(cli_alt, ['--no-warnings'])330+ assert result.output == 'False\n'331+ result = runner.invoke(cli_alt, ['-w'])332+ assert result.output == 'True\n'modified:CHANGES+1 added
13 unmodified lines14 cannot be supported with the current setup of how the invocation for15 subcommands works.16 - Explicitly disallow nargs=-1 with a set default.17 - The context is now closed before it's popped from the stack.18+- Added support for short aliases for the false flag on toggles.19 20 Version 5.121 -----------22 251 unmodified lines