Skip to content
Myr

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

4 files+56 added−3 removed

modified:click/core.py+6 added-3 removed
1515 unmodified lines
1516 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 lines
modified:docs/options.rst+23 added
199 unmodified lines
200201 if __name__ == '__main__':202 log()203204.. versionchanged:: 6.0205206If you want to define an alias for the second option only, then you will207need to use leading whitespace to disambiguate the format string:208209Example:210211.. click:example::212213 import sys214215 @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)222223.. click:run::224225 invoke(info, args=['--help'])226227Feature Switches228----------------229230In addition to boolean flags, there are also feature switches. These are
465 unmodified lines
modified:tests/test_options.py+26 added
302 unmodified lines
303304 result = runner.invoke(cmd, ['--help'])305 assert 'I am a help text' in result.output306 assert 'you wont see me' not in result.output307308309def 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)314315 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'321322 @click.command()323 @click.option('--warnings/--no-warnings', '-w', default=True)324 def cli_alt(warnings):325 click.echo(warnings)326327 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 lines
14 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.1920Version 5.121-----------22
251 unmodified lines