diff options
author | Florian Bruhin <me@the-compiler.org> | 2021-03-18 15:15:22 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2021-03-18 15:15:22 +0100 |
commit | 3d1f975391ef61a43490e93b4ce0c54fdd5c22ab (patch) | |
tree | 058b862985d6d99afc64dcdebe4994908feab061 /qutebrowser/commands/parser.py | |
parent | f3e604d7fbf2ceb8f15a92c5ba7c177c83ee4db1 (diff) | |
download | qutebrowser-3d1f975391ef61a43490e93b4ce0c54fdd5c22ab.tar.gz qutebrowser-3d1f975391ef61a43490e93b4ce0c54fdd5c22ab.zip |
Move completion fallback handling out of CommandParser
This makes CompletionParser.parse simpler and makes ParseResult.cmd and
.args non-Optional. Them being Optional would mean we would've to either
resort to more complex typing with Literal, or to check whether they are
really non-None everywhere.
Since fallback=True is only used at one point, let's just handle this at
the calling site instead.
In theory, this changes the behavior when the cmdstr is empty and
self._partial_match is set, because we now raise early and
self._completion_match isn't called anymore. In practice, I think this
shouldn't make a difference anywhere, and tests seem to agree.
If cmdstr is empty and self._partial_match is False, the behavior should
be the same, because objects.commands[''] will raise KeyError.
Diffstat (limited to 'qutebrowser/commands/parser.py')
-rw-r--r-- | qutebrowser/commands/parser.py | 29 |
1 files changed, 7 insertions, 22 deletions
diff --git a/qutebrowser/commands/parser.py b/qutebrowser/commands/parser.py index 547641281..5adbc003c 100644 --- a/qutebrowser/commands/parser.py +++ b/qutebrowser/commands/parser.py @@ -20,7 +20,7 @@ """Module for parsing commands entered into the browser.""" import dataclasses -from typing import Optional, List, Mapping, Iterator, List, Union +from typing import Optional, List, Mapping, Iterator, Union from qutebrowser.commands import cmdexc, command from qutebrowser.misc import split, objects @@ -32,8 +32,8 @@ class ParseResult: """The result of parsing a commandline.""" - cmd: Optional[command.Command] - args: Optional[List[str]] + cmd: command.Command + args: List[str] cmdline: List[str] @@ -118,27 +118,16 @@ class CommandParser: """Wrapper over _parse_all_gen.""" return list(self._parse_all_gen(text, **kwargs)) - def parse( - self, - text: str, - *, - fallback: bool = False, - keep: bool = False, - ) -> ParseResult: + def parse(self, text: str, *, keep: bool = False) -> ParseResult: """Split the commandline text into command and arguments. Args: text: Text to parse. - fallback: Whether to do a fallback splitting when the command was - unknown. - keep: Whether to keep special chars and whitespace - - Return: - A ParseResult tuple. + keep: Whether to keep special chars and whitespace. """ cmdstr, sep, argstr = text.partition(' ') - if not cmdstr and not fallback: + if not cmdstr: raise cmdexc.NoSuchCommandError("No command given") if self._partial_match: @@ -147,11 +136,7 @@ class CommandParser: try: cmd = objects.commands[cmdstr] except KeyError: - if not fallback: - raise cmdexc.NoSuchCommandError( - '{}: no such command'.format(cmdstr)) - cmdline = split.split(text, keep=keep) - return ParseResult(cmd=None, args=None, cmdline=cmdline) + raise cmdexc.NoSuchCommandError(f'{cmdstr}: no such command') args = self._split_args(cmd, argstr, keep) if keep and args: |