57d51ad9bb
Squashed commit: - Fix getting current URL - Get rid of *args for hints. - Make enums work. - Fix moving commands to utilcmds. - Fix enums in argparse - Fix arg splitting for hints. - Fix default enum args. - Fix argument splitting for hints if None is given. - Fix set_cmd_text with flags and fix {url}. - Fix unittests - Fix tuple types for arguments. - Fix scroll-page. - Fix lint - Fix open_target. - Others
89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
|
#
|
|
# This file is part of qutebrowser.
|
|
#
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""argparse.ArgumentParser subclass to parse qutebrowser commands."""
|
|
|
|
|
|
import argparse
|
|
|
|
from qutebrowser.commands import cmdexc
|
|
from qutebrowser.utils import utils
|
|
|
|
|
|
class ArgumentParserError(Exception):
|
|
|
|
"""Exception raised when the ArgumentParser signals an error."""
|
|
|
|
|
|
class ArgumentParser(argparse.ArgumentParser):
|
|
|
|
"""Subclass ArgumentParser to be more suitable for runtime parsing."""
|
|
|
|
def __init__(self):
|
|
super().__init__(add_help=False)
|
|
|
|
def exit(self, status=0, msg=None):
|
|
raise AssertionError('exit called, this should never happen. '
|
|
'Status: {}, message: {}'.format(status, msg))
|
|
|
|
def error(self, msg):
|
|
raise ArgumentParserError(msg[0].upper() + msg[1:])
|
|
|
|
|
|
def enum_getter(enum):
|
|
"""Function factory to get an enum getter."""
|
|
|
|
def _get_enum_item(key):
|
|
"""Helper function to get an enum item.
|
|
|
|
Passes through existing items unmodified.
|
|
"""
|
|
if isinstance(key, enum):
|
|
return key
|
|
try:
|
|
return enum[key.replace('-', '_')]
|
|
except KeyError:
|
|
raise cmdexc.ArgumentTypeError("Invalid value {}.".format(key))
|
|
|
|
return _get_enum_item
|
|
|
|
|
|
def multitype_conv(tpl):
|
|
"""Function factory to get a type converter for a choice of types."""
|
|
|
|
def _convert(value):
|
|
"""Convert a value according to an iterable of possible arg types."""
|
|
for typ in tpl:
|
|
if isinstance(typ, str):
|
|
if value == typ:
|
|
return value
|
|
elif utils.is_enum(typ):
|
|
return enum_getter(typ)(value)
|
|
elif callable(typ):
|
|
# int, float, etc.
|
|
if isinstance(value, typ):
|
|
return value
|
|
try:
|
|
return typ(value)
|
|
except ValueError:
|
|
pass
|
|
raise cmdexc.ArgumentTypeError('Invalid value {}.'.format(value))
|
|
|
|
return _convert
|