Replace 'nargs' annotation by star_args_optional

Before we used a {'nargs': '*'} annotation for the respective argument
to tell qutebrowser it's optional for the commandline. Now we instead
use a star_args_optional argument for @cmdutils.register as a first step
towards freeing up argument annotations for PEP 484.

See #637.
This commit is contained in:
Florian Bruhin 2016-05-09 22:40:23 +02:00
parent e4e98c6c23
commit 73fbfb9731
4 changed files with 13 additions and 19 deletions

View File

@ -441,8 +441,6 @@ then automatically checked. Possible values:
e.g. `('foo', 'bar')` or `(int, 'foo')`. e.g. `('foo', 'bar')` or `(int, 'foo')`.
* `flag`: The flag to be used, as 1-char string (default: First char of the * `flag`: The flag to be used, as 1-char string (default: First char of the
long name). long name).
* `nargs`: Gets passed to argparse, see
https://docs.python.org/dev/library/argparse.html#nargs[its documentation].
The name of an argument will always be the parameter name, with any trailing The name of an argument will always be the parameter name, with any trailing
underscores stripped. underscores stripped.

View File

@ -737,9 +737,9 @@ class HintManager(QObject):
webview.openurl(url) webview.openurl(url)
@cmdutils.register(instance='hintmanager', scope='tab', name='hint', @cmdutils.register(instance='hintmanager', scope='tab', name='hint',
win_id='win_id') win_id='win_id', star_args_optional=True)
def start(self, rapid=False, group=webelem.Group.all, target=Target.normal, def start(self, rapid=False, group=webelem.Group.all, target=Target.normal,
*args: {'nargs': '*'}, win_id): *args, win_id):
"""Start hinting. """Start hinting.
Args: Args:

View File

@ -65,15 +65,15 @@ class Command:
AnnotationInfo: Named tuple for info from an annotation. AnnotationInfo: Named tuple for info from an annotation.
""" """
AnnotationInfo = collections.namedtuple('AnnotationInfo', AnnotationInfo = collections.namedtuple(
['kwargs', 'type', 'flag', 'hide', 'AnnotationInfo', ['type', 'flag', 'hide', 'metavar'])
'metavar'])
def __init__(self, *, handler, name, instance=None, maxsplit=None, def __init__(self, *, handler, name, instance=None, maxsplit=None,
hide=False, completion=None, modes=None, not_modes=None, hide=False, completion=None, modes=None, not_modes=None,
needs_js=False, debug=False, ignore_args=False, needs_js=False, debug=False, ignore_args=False,
deprecated=False, no_cmd_split=False, scope='global', deprecated=False, no_cmd_split=False,
count=None, win_id=None): star_args_optional=False, scope='global', count=None,
win_id=None):
# I really don't know how to solve this in a better way, I tried. # I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
if modes is not None and not_modes is not None: if modes is not None and not_modes is not None:
@ -99,6 +99,7 @@ class Command:
self._not_modes = not_modes self._not_modes = not_modes
self._scope = scope self._scope = scope
self._needs_js = needs_js self._needs_js = needs_js
self._star_args_optional = star_args_optional
self.debug = debug self.debug = debug
self.ignore_args = ignore_args self.ignore_args = ignore_args
self.handler = handler self.handler = handler
@ -276,13 +277,12 @@ class Command:
kwargs['type'] = typ kwargs['type'] = typ
if param.kind == inspect.Parameter.VAR_POSITIONAL: if param.kind == inspect.Parameter.VAR_POSITIONAL:
kwargs['nargs'] = '+' kwargs['nargs'] = '*' if self._star_args_optional else '+'
elif param.kind == inspect.Parameter.KEYWORD_ONLY: elif param.kind == inspect.Parameter.KEYWORD_ONLY:
kwargs['default'] = param.default kwargs['default'] = param.default
elif typ is not bool and param.default is not inspect.Parameter.empty: elif typ is not bool and param.default is not inspect.Parameter.empty:
kwargs['default'] = param.default kwargs['default'] = param.default
kwargs['nargs'] = '?' kwargs['nargs'] = '?'
kwargs.update(annotation_info.kwargs)
return kwargs return kwargs
def _param_to_argparse_args(self, param, annotation_info): def _param_to_argparse_args(self, param, annotation_info):
@ -324,22 +324,17 @@ class Command:
Return: Return:
An AnnotationInfo namedtuple. An AnnotationInfo namedtuple.
kwargs: A dict of keyword args to add to the
argparse.ArgumentParser.add_argument call.
typ: The type to use for this argument. typ: The type to use for this argument.
flag: The short name/flag if overridden. flag: The short name/flag if overridden.
name: The long name if overridden. name: The long name if overridden.
""" """
info = {'kwargs': {}, 'type': None, 'flag': None, 'hide': False, info = {'type': None, 'flag': None, 'hide': False, 'metavar': None}
'metavar': None}
if param.annotation is not inspect.Parameter.empty: if param.annotation is not inspect.Parameter.empty:
log.commands.vdebug("Parsing annotation {}".format( log.commands.vdebug("Parsing annotation {}".format(
param.annotation)) param.annotation))
for field in ('type', 'flag', 'name', 'hide', 'metavar'): for field in ('type', 'flag', 'name', 'hide', 'metavar'):
if field in param.annotation: if field in param.annotation:
info[field] = param.annotation[field] info[field] = param.annotation[field]
if 'nargs' in param.annotation:
info['kwargs'] = {'nargs': param.annotation['nargs']}
return self.AnnotationInfo(**info) return self.AnnotationInfo(**info)
def _get_type(self, param, annotation_info): def _get_type(self, param, annotation_info):

View File

@ -184,8 +184,9 @@ class SaveManager(QObject):
message.error('current', "Failed to auto-save {}: " message.error('current', "Failed to auto-save {}: "
"{}".format(key, e)) "{}".format(key, e))
@cmdutils.register(instance='save-manager', name='save', win_id='win_id') @cmdutils.register(instance='save-manager', name='save', win_id='win_id',
def save_command(self, win_id, *what: {'nargs': '*'}): star_args_optional=True)
def save_command(self, win_id, *what):
"""Save configs and state. """Save configs and state.
Args: Args: