Support a maxsplit argument for commands.

This commit is contained in:
Florian Bruhin 2014-12-11 20:25:54 +01:00
parent 81bc5dae94
commit b7ea8e7979
6 changed files with 17 additions and 14 deletions

View File

@ -602,7 +602,7 @@ class Application(QApplication):
if shutdown:
self.shutdown()
@cmdutils.register(instance='app', split=False, debug=True)
@cmdutils.register(instance='app', maxsplit=0, debug=True)
def debug_pyeval(self, s):
"""Evaluate a python string and display the results as a webpage.

View File

@ -270,7 +270,7 @@ class CommandDispatcher:
tabbar.setSelectionBehaviorOnRemove(old_selection_behavior)
@cmdutils.register(instance='command-dispatcher', name='open',
split=False, scope='window',
maxsplit=0, scope='window',
completion=[usertypes.Completion.quickmark_by_url])
def openurl(self, url, bg=False, tab=False, window=False,
count: {'special': 'count'}=None):
@ -777,7 +777,7 @@ class CommandDispatcher:
finally:
tabbed_browser.setUpdatesEnabled(True)
@cmdutils.register(instance='command-dispatcher', split=False,
@cmdutils.register(instance='command-dispatcher', maxsplit=0,
scope='window')
def spawn(self, *args):
"""Spawn a command in a shell.
@ -823,7 +823,7 @@ class CommandDispatcher:
quickmark_manager.prompt_save(self._win_id, self._current_url())
@cmdutils.register(instance='command-dispatcher', scope='window',
split=False,
maxsplit=0,
completion=[usertypes.Completion.quickmark_by_name])
def quickmark_load(self, name, tab=False, bg=False, window=False):
"""Load a quickmark.

View File

@ -110,7 +110,7 @@ class QuickmarkManager(QObject):
else:
set_mark()
@cmdutils.register(instance='quickmark-manager', split=False,
@cmdutils.register(instance='quickmark-manager', maxsplit=0,
completion=[usertypes.Completion.quickmark_by_name])
def quickmark_del(self, name):
"""Delete a quickmark.

View File

@ -101,7 +101,8 @@ class register: # pylint: disable=invalid-name
_instance: The object from the object registry to be used as "self".
_scope: The scope to get _instance for.
_name: The name (as string) or names (as list) of the command.
_split: Whether to split the arguments.
_maxsplit: The maxium amounts of splits to do for the commandline, or
None.
_hide: Whether to hide the command or not.
_completion: Which completion to use for arguments, as a list of
strings.
@ -111,7 +112,7 @@ class register: # pylint: disable=invalid-name
_ignore_args: Whether to ignore the arguments of the function.
"""
def __init__(self, instance=None, name=None, split=True, hide=False,
def __init__(self, instance=None, name=None, maxsplit=None, hide=False,
completion=None, modes=None, not_modes=None, needs_js=False,
debug=False, ignore_args=False, scope='global'):
"""Save decorator arguments.
@ -125,7 +126,7 @@ class register: # pylint: disable=invalid-name
if modes is not None and not_modes is not None:
raise ValueError("Only modes or not_modes can be given!")
self._name = name
self._split = split
self._maxsplit = maxsplit
self._hide = hide
self._instance = instance
self._scope = scope
@ -187,7 +188,7 @@ class register: # pylint: disable=invalid-name
if name in cmd_dict:
raise ValueError("{} is already registered!".format(name))
cmd = command.Command(
name=names[0], split=self._split, hide=self._hide,
name=names[0], maxsplit=self._maxsplit, hide=self._hide,
instance=self._instance, scope=self._scope,
completion=self._completion, modes=self._modes,
not_modes=self._not_modes, needs_js=self._needs_js,

View File

@ -34,7 +34,8 @@ class Command:
Attributes:
name: The main name of the command.
split: Whether to split the arguments.
maxsplit: The maximum amount of splits to do for the commandline, or
None.
hide: Whether to hide the arguments or not.
desc: The description of the command.
handler: The handler function to call.
@ -60,13 +61,13 @@ class Command:
['kwargs', 'type', 'name', 'flag',
'special'])
def __init__(self, name, split, hide, instance, completion, modes,
def __init__(self, name, maxsplit, hide, instance, completion, modes,
not_modes, needs_js, is_debug, ignore_args,
handler, scope):
# I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-arguments,too-many-locals
self.name = name
self.split = split
self.maxsplit = maxsplit
self.hide = hide
self._instance = instance
self.completion = completion

View File

@ -247,7 +247,7 @@ class CommandRunner(QObject):
"""
if not argstr:
self._args = []
elif self._cmd.split:
elif self._cmd.maxsplit is None:
self._args = split.split(argstr, keep=keep)
else:
# If split=False, we still want to split the flags, but not
@ -266,7 +266,8 @@ class CommandRunner(QObject):
arg = arg.strip()
if not arg.startswith('-'):
self._args = []
args = split.simple_split(argstr, keep=keep, maxsplit=i)
args = split.simple_split(argstr, keep=keep,
maxsplit=i + self._cmd.maxsplit)
for s in args:
# remove quotes and replace \" by "
s = re.sub(r"""(^|[^\\])["']""", r'\1', s)