diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index 8dd830495..5c5ab1311 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -123,6 +123,7 @@ class Command: self.pos_args = [] self.desc = None self.flags_with_args = [] + self._has_vararg = False # This is checked by future @cmdutils.argument calls so they fail # (as they'd be silently ignored otherwise) @@ -170,6 +171,8 @@ class Command: def get_pos_arg_info(self, pos): """Get an ArgInfo tuple for the given positional parameter.""" + if pos >= len(self.pos_args) and self._has_vararg: + pos = len(self.pos_args) - 1 name = self.pos_args[pos][0] return self._qute_args.get(name, ArgInfo()) @@ -233,6 +236,8 @@ class Command: log.commands.vdebug('Adding arg {} of type {} -> {}'.format( param.name, typ, callsig)) self.parser.add_argument(*args, **kwargs) + if param.kind == inspect.Parameter.VAR_POSITIONAL: + self._has_vararg = True return signature.parameters.values() def _param_to_argparse_kwargs(self, param, is_bool): diff --git a/tests/unit/completion/test_completer.py b/tests/unit/completion/test_completer.py index 51aa091b9..74313911f 100644 --- a/tests/unit/completion/test_completer.py +++ b/tests/unit/completion/test_completer.py @@ -129,12 +129,20 @@ def cmdutils_patch(monkeypatch, stubs, miscmodels_patch): """docstring.""" pass + @cmdutils.argument('option', completion=miscmodels_patch.option) + @cmdutils.argument('values', completion=miscmodels_patch.value) + def config_cycle(option, *values): + """For testing varargs.""" + pass + cmd_utils = stubs.FakeCmdUtils({ 'set': command.Command(name='set', handler=set_command), 'help': command.Command(name='help', handler=show_help), 'open': command.Command(name='open', handler=openurl, maxsplit=0), 'bind': command.Command(name='bind', handler=bind), 'tab-detach': command.Command(name='tab-detach', handler=tab_detach), + 'config-cycle': command.Command(name='config-cycle', + handler=config_cycle), }) monkeypatch.setattr(completer, 'cmdutils', cmd_utils) @@ -191,6 +199,10 @@ def _set_cmd_prompt(cmd, txt): ('/:help|', None, '', []), ('::bind|', 'command', ':bind', []), (':-w open |', None, '', []), + # varargs + (':config-cycle option |', 'value', '', ['option']), + (':config-cycle option one |', 'value', '', ['option', 'one']), + (':config-cycle option one two |', 'value', '', ['option', 'one', 'two']), ]) def test_update_completion(txt, kind, pattern, pos_args, status_command_stub, completer_obj, completion_widget_stub, config_stub,