From c14db202d63e80d53e67aca3a32c0331053109b9 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 10 May 2016 20:34:03 +0200 Subject: [PATCH] Use @cmdutils.argument to hide arguments --- CONTRIBUTING.asciidoc | 1 + qutebrowser/browser/commands.py | 4 ++-- qutebrowser/commands/command.py | 16 +++++++++------- tests/unit/commands/test_cmdutils.py | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.asciidoc b/CONTRIBUTING.asciidoc index abaa3b04c..372fa9ef1 100644 --- a/CONTRIBUTING.asciidoc +++ b/CONTRIBUTING.asciidoc @@ -456,6 +456,7 @@ The following arguments are supported for `@cmdutils.argument`: - `flag`: Customize the short flag (`-x`) the argument will get. - `win_id=True`: Mark the argument as special window ID argument - `count=True`: Mark the argument as special count argument +- `hide=True`: Hide the argument from the documentation The name of an argument will always be the parameter name, with any trailing underscores stripped. diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 5c44c94fe..06f287384 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1198,8 +1198,8 @@ class CommandDispatcher: cur.inspector.show() @cmdutils.register(instance='command-dispatcher', scope='window') - def download(self, url=None, dest_old: {'hide': True}=None, *, - mhtml_=False, dest=None): + @cmdutils.argument('dest_old', hide=True) + def download(self, url=None, dest_old=None, *, mhtml_=False, dest=None): """Download a given URL, or current page if no URL given. The form `:download [url] [dest]` is deprecated, use `:download --dest diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index 0e76cfd11..c62c47120 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -39,21 +39,23 @@ class ArgInfo: """Information about an argument.""" - def __init__(self, win_id=False, count=False, flag=None): + def __init__(self, win_id=False, count=False, flag=None, hide=False): if win_id and count: raise TypeError("Argument marked as both count/win_id!") self.win_id = win_id self.count = count self.flag = flag + self.hide = hide def __eq__(self, other): return (self.win_id == other.win_id and self.count == other.count and - self.flag == other.flag) + self.flag == other.flag and + self.hide == other.hide) def __repr__(self): return utils.get_repr(self, win_id=self.win_id, count=self.count, - flag=self.flag, constructor=True) + flag=self.flag, hide=self.hide, constructor=True) class Command: @@ -87,7 +89,7 @@ class Command: """ AnnotationInfo = collections.namedtuple( - 'AnnotationInfo', ['type', 'hide', 'metavar']) + 'AnnotationInfo', ['type', 'metavar']) def __init__(self, *, handler, name, instance=None, maxsplit=None, hide=False, completion=None, modes=None, not_modes=None, @@ -339,7 +341,7 @@ class Command: if typ is not bool: self.flags_with_args += [short_flag, long_flag] else: - if not annotation_info.hide: + if not arg_info.hide: self.pos_args.append((param.name, name)) return args @@ -354,11 +356,11 @@ class Command: typ: The type to use for this argument. name: The long name if overridden. """ - info = {'type': None, 'hide': False, 'metavar': None} + info = {'type': None, 'metavar': None} if param.annotation is not inspect.Parameter.empty: log.commands.vdebug("Parsing annotation {}".format( param.annotation)) - for field in ('type', 'name', 'hide', 'metavar'): + for field in ('type', 'name', 'metavar'): if field in param.annotation: info[field] = param.annotation[field] return self.AnnotationInfo(**info) diff --git a/tests/unit/commands/test_cmdutils.py b/tests/unit/commands/test_cmdutils.py index 1fa47d556..961e19d02 100644 --- a/tests/unit/commands/test_cmdutils.py +++ b/tests/unit/commands/test_cmdutils.py @@ -254,6 +254,20 @@ class TestRegister: expected = "fun: handler has count parameter without default!" assert str(excinfo.value) == expected + @pytest.mark.parametrize('hide', [True, False]) + def test_pos_args(self, hide): + @cmdutils.register() + @cmdutils.argument('arg', hide=hide) + def fun(arg): + """Blah.""" + pass + + pos_args = cmdutils.cmd_dict['fun'].pos_args + if hide: + assert pos_args == [] + else: + assert pos_args == [('arg', 'arg')] + class TestArgument: