Skip to content
Myr

setting a default for arguments now implicitly makes it non required

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 4aef759 · Browse files · 4 files+22 added−6 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+22 added−6 removed

  • tests:+11 added−0 removed
  • other:+11 added−6 removed

4 files+22 added−6 removed

modified:click/core.py+4 added-1 removed
1168 unmodified lines
1169 param_type_name = 'argument'11701171 def __init__(self, param_decls, required=None, **attrs):1172 if required is None:1173 if attrs.get('default') is not None:1174 required = False1175 else:1173 required = attrs.get('nargs', 0) > 01176 required = attrs.get('nargs', 0) > 01177 Parameter.__init__(self, param_decls, required=required, **attrs)11781179 def make_metavar(self):1180 if self.metavar is not None:
31 unmodified lines
modified:docs/arguments.rst+6 added-5 removed
61 unmodified lines
6263 If you come from ``argparse`` you might be missing support for setting64 ``nargs`` to ``+`` to indicate that at least one argument is required.6566 Support for this is very intentionally not included as we believe67 scripts should gracefully degrade into becoming noops if a variadic68 argument is empty. The reason for this is that very often scripts are69 invoked with wildcard inputs from the command line and they should not70 error out if the wildcard is empty.66 This is supported by setting ``required=True``. However this should67 not be used if you can avoid it as we believe scripts should gracefully68 degrade into becoming noops if a variadic argument is empty. The69 reason for this is that very often scripts are invoked with wildcard70 inputs from the command line and they should not error out if the71 wildcard is empty.7273.. _file-args:7475File Arguments
125 unmodified lines
modified:tests/test_arguments.py+11 added
143 unmodified lines
144 assert result.exit_code == 2145 assert 'Missing argument "arg"' in result.output146147148def test_implicit_non_required(runner):149 @click.command()150 @click.argument('f', default='test')151 def cli(f):152 click.echo(f)153154 result = runner.invoke(cli, [])155 assert result.exit_code == 0156 assert result.output == 'test\n'157158159def test_eat_options(runner):160 @click.command()161 @click.option('-f')162 @click.argument('files', nargs=-1)
18 unmodified lines
modified:CHANGES+1 added
27 unmodified lines
28- Added an :func:`edit` function that invokes the default editor.29- Added an :func:`launch` function that launches browsers and applications.30- nargs of -1 for arguments can now be forced to be a single item through31 the required flag. It defaults to not required.32- setting a default for arguments now implicitly makes it non required.3334Version 1.135-----------36
11 unmodified lines