Added nicer error handling for group commands with arguments.
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 d227e5e · Browse files · 2 files·+53 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+53 added−7 removed
- tests:+27 added−0 removed
- other:+26 added−7 removed
Provenance
- Commit
f3ee4e5- Tree
526f06d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d227e5e
2 files changed+53 added−7 removed
modified:click/core.py+26 added-7 removed
389 unmodified lines390 if parent is not None and parent.default_map is not None:391 default_map = parent.default_map.get(info_name)392 extra['default_map'] = default_map393 ctx = Context(self, info_name=info_name, parent=parent, **extra)394+ self.parse_args(ctx, args)395+ return ctx396+397+ def parse_args(self, ctx, args):398+ """Given a context and a list of arguments this creates the parser399+ and parses the arguments, then modifies the context as necessary.400+ This is automatically invoked by :meth:`make_context`.401+ """402 parser = self.make_parser(ctx)403 opts, args, param_order = parser.parse_args(args=args)404 405 for param in iter_params_for_processing(param_order, self.params):5 unmodified lines411 ' '.join(map(make_str, args))))412 413 ctx.args = args414 407- return ctx408-415 def invoke(self, ctx):416 """Given a context, this invokes the attached callback (if it exists)417 in the right way.418 """121 unmodified lines540 if rows:541 with formatter.section('Commands'):542 formatter.write_dl(rows)543 544+ def parse_args(self, ctx, args):545+ if not args and self.no_args_is_help:546+ echo(ctx.get_help())547+ ctx.exit()548+ return Command.parse_args(self, ctx, args)549+550 def invoke(self, ctx):551 if not ctx.args:552 if self.invoke_without_command:553 return Command.invoke(self, ctx)542- elif self.no_args_is_help:543- echo(ctx.get_help())544- ctx.exit()545- ctx.fail('Missing command')554+ ctx.fail('Missing command.')555 556 cmd_name = make_str(ctx.args[0])557 cmd = self.get_command(ctx, cmd_name)558+559+ # If we don't find the command we want to show an error message560+ # to the user that it was not provided. However there is561+ # something else we should do: if the first argument looks like562+ # an option we want to kick off parsing again for arguments to563+ # resolve things like --help which now should go to the main564+ # place.565 if cmd is None:566+ if split_opt(cmd_name)[0]:567+ self.parse_args(ctx, ctx.args)550- ctx.fail('No such command "%s"' % cmd_name)568+ ctx.fail('No such command "%s".' % cmd_name)569+570 return self.invoke_subcommand(ctx, cmd, cmd_name, ctx.args[1:])571 572 def invoke_subcommand(self, ctx, cmd, cmd_name, args):573 # Whenever we dispatch to a subcommand we also invoke the regular526 unmodified linesmodified:tests/test_commands.py+27 added
74 unmodified lines75 })76 77 assert not result.exception78 assert result.output == 'changed\n'79+80+81+def test_group_with_args(runner):82+ @click.group()83+ @click.argument('obj')84+ def cli(obj):85+ click.echo('obj=%s' % obj)86+87+ @cli.command()88+ def move():89+ click.echo('move')90+91+ result = runner.invoke(cli, [])92+ assert result.exit_code == 093+ assert 'Show this message and exit.' in result.output94+95+ result = runner.invoke(cli, ['obj1'])96+ assert result.exit_code == 297+ assert 'Error: Missing command.' in result.output98+99+ result = runner.invoke(cli, ['obj1', '--help'])100+ assert result.exit_code == 0101+ assert 'Show this message and exit.' in result.output102+103+ result = runner.invoke(cli, ['obj1', 'move'])104+ assert result.exit_code == 0105+ assert result.output == 'obj=obj1\nmove\n'