Customize required in arguments for nargs of -1
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 247f9a3 · Browse files · 3 files·+30 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−4 removed
- tests:+10 added−1 removed
- other:+20 added−3 removed
Provenance
- Commit
ce1a418- Tree
76da111- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
247f9a3
3 files changed+30 added−4 removed
modified:click/core.py+3 added-1 removed
1166 unmodified lines1167 All parameters are passed onwards to the parameter constructor.1168 """1169 param_type_name = 'argument'1170 1171- def __init__(self, param_decls, required=True, **attrs):1171+ def __init__(self, param_decls, required=None, **attrs):1172+ if required is None:1173+ required = attrs.get('nargs', 0) > 01174 Parameter.__init__(self, param_decls, required=required, **attrs)1175 1176 def make_metavar(self):1177 if self.metavar is not None:31 unmodified linesmodified:examples/repo/repo.py+17 added-2 removed
102 unmodified lines103 click.echo('Changed credentials.')104 105 106 @cli.command()107-@click.option('--message', '-m', required=True, multiple=True,107+@click.option('--message', '-m', multiple=True,108 help='The commit message. If provided multiple times each '109 'argument gets converted into a new line.')110 @click.argument('files', nargs=-1)111 @pass_repo5 unmodified lines117 118 If a list of files is omitted, all changes reported by "repo status"119 will be committed.120 """121+ if not message:122+ marker = '# Files to be committed:'123+ hint = ['', '', marker, '#']124+ for file in files:125+ hint.append('# U %s' % file)126+ message = click.edit('\n'.join(hint))127+ if message is None:128+ click.echo('Aborted!')129+ return130+ msg = message.split(marker)[0].rstrip()131+ if not msg:132+ click.echo('Aborted! Empty commit message')133+ return134+ else:135+ msg = '\n'.join(message)136 click.echo('Files to be committed: %s' % (files,))122- click.echo('Commit message:\n' + '\n'.join(message))137+ click.echo('Commit message:\n' + msg)138 139 140 @cli.command(short_help='Copies files.')141 @click.option('--force', is_flag=True,10 unmodified linesmodified:tests/test_arguments.py+10 added-1 removed
125 unmodified lines126 127 128 def test_empty_nargs(runner):129 @click.command()130- @click.option('--arg', nargs=-1)130+ @click.argument('arg', nargs=-1)131 def cmd(arg):132 click.echo('arg:' + '|'.join(arg))133 134 result = runner.invoke(cmd, [])135 assert result.exit_code == 0136 assert result.output == 'arg:\n'137 138+ @click.command()139+ @click.argument('arg', nargs=-1, required=True)140+ def cmd2(arg):141+ click.echo('arg:' + '|'.join(arg))142 143+ result = runner.invoke(cmd2, [])144+ assert result.exit_code == 2145+ assert 'Missing argument "arg"' in result.output146+147+148 def test_eat_options(runner):149 @click.command()150 @click.option('-f')151 @click.argument('files', nargs=-1)18 unmodified lines