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')`.
* `flag`: The flag to be used, as 1-char string (default: First char of the
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
underscores stripped.

View File

@ -737,9 +737,9 @@ class HintManager(QObject):
webview.openurl(url)
@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,
*args: {'nargs': '*'}, win_id):
*args, win_id):
"""Start hinting.
Args:

View File

@ -65,15 +65,15 @@ class Command:
AnnotationInfo: Named tuple for info from an annotation.
"""
AnnotationInfo = collections.namedtuple('AnnotationInfo',
['kwargs', 'type', 'flag', 'hide',
'metavar'])
AnnotationInfo = collections.namedtuple(
'AnnotationInfo', ['type', 'flag', 'hide', 'metavar'])
def __init__(self, *, handler, name, instance=None, maxsplit=None,
hide=False, completion=None, modes=None, not_modes=None,
needs_js=False, debug=False, ignore_args=False,
deprecated=False, no_cmd_split=False, scope='global',
count=None, win_id=None):
deprecated=False, no_cmd_split=False,
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.
# pylint: disable=too-many-locals
if modes is not None and not_modes is not None:
@ -99,6 +99,7 @@ class Command:
self._not_modes = not_modes
self._scope = scope
self._needs_js = needs_js
self._star_args_optional = star_args_optional
self.debug = debug
self.ignore_args = ignore_args
self.handler = handler
@ -276,13 +277,12 @@ class Command:
kwargs['type'] = typ
if param.kind == inspect.Parameter.VAR_POSITIONAL:
kwargs['nargs'] = '+'
kwargs['nargs'] = '*' if self._star_args_optional else '+'
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
kwargs['default'] = param.default
elif typ is not bool and param.default is not inspect.Parameter.empty:
kwargs['default'] = param.default
kwargs['nargs'] = '?'
kwargs.update(annotation_info.kwargs)
return kwargs
def _param_to_argparse_args(self, param, annotation_info):
@ -324,22 +324,17 @@ class Command:
Return:
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.
flag: The short name/flag if overridden.
name: The long name if overridden.
"""
info = {'kwargs': {}, 'type': None, 'flag': None, 'hide': False,
'metavar': None}
info = {'type': None, 'flag': None, 'hide': False, 'metavar': None}
if param.annotation is not inspect.Parameter.empty:
log.commands.vdebug("Parsing annotation {}".format(
param.annotation))
for field in ('type', 'flag', 'name', 'hide', 'metavar'):
if field in param.annotation:
info[field] = param.annotation[field]
if 'nargs' in param.annotation:
info['kwargs'] = {'nargs': param.annotation['nargs']}
return self.AnnotationInfo(**info)
def _get_type(self, param, annotation_info):

View File

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