Fix command parsing for arguments containing _.
This commit is contained in:
parent
8369c74f74
commit
167faafff2
@ -29,6 +29,11 @@ from qutebrowser.utils import log, utils, message, docutils, objreg, usertypes
|
|||||||
from qutebrowser.utils import debug as debug_utils
|
from qutebrowser.utils import debug as debug_utils
|
||||||
|
|
||||||
|
|
||||||
|
def arg_name(name):
|
||||||
|
"""Get the name an argument should have based on its Python name."""
|
||||||
|
return name.rstrip('_').replace('_', '-')
|
||||||
|
|
||||||
|
|
||||||
class Command:
|
class Command:
|
||||||
|
|
||||||
"""Base skeleton for a command.
|
"""Base skeleton for a command.
|
||||||
@ -288,7 +293,7 @@ class Command:
|
|||||||
A list of args.
|
A list of args.
|
||||||
"""
|
"""
|
||||||
args = []
|
args = []
|
||||||
name = param.name.rstrip('_').replace('_', '-')
|
name = arg_name(param.name)
|
||||||
shortname = annotation_info.flag or name[0]
|
shortname = annotation_info.flag or name[0]
|
||||||
if len(shortname) != 1:
|
if len(shortname) != 1:
|
||||||
raise ValueError("Flag '{}' of parameter {} (command {}) must be "
|
raise ValueError("Flag '{}' of parameter {} (command {}) must be "
|
||||||
@ -304,7 +309,7 @@ class Command:
|
|||||||
if typ is not bool:
|
if typ is not bool:
|
||||||
self.flags_with_args += [short_flag, long_flag]
|
self.flags_with_args += [short_flag, long_flag]
|
||||||
else:
|
else:
|
||||||
args.append(name)
|
args.append(param.name)
|
||||||
if not annotation_info.hide:
|
if not annotation_info.hide:
|
||||||
self.pos_args.append((param.name, name))
|
self.pos_args.append((param.name, name))
|
||||||
return args
|
return args
|
||||||
@ -408,17 +413,16 @@ class Command:
|
|||||||
raise TypeError("{}: invalid parameter type {} for argument "
|
raise TypeError("{}: invalid parameter type {} for argument "
|
||||||
"{!r}!".format(self.name, param.kind, param.name))
|
"{!r}!".format(self.name, param.kind, param.name))
|
||||||
|
|
||||||
def _get_param_name_and_value(self, param):
|
def _get_param_value(self, param):
|
||||||
"""Get the converted name and value for an inspect.Parameter."""
|
"""Get the converted value for an inspect.Parameter."""
|
||||||
name = param.name.rstrip('_')
|
value = getattr(self.namespace, param.name)
|
||||||
value = getattr(self.namespace, name)
|
|
||||||
if param.name in self._type_conv:
|
if param.name in self._type_conv:
|
||||||
# We convert enum types after getting the values from
|
# We convert enum types after getting the values from
|
||||||
# argparse, because argparse's choices argument is
|
# argparse, because argparse's choices argument is
|
||||||
# processed after type conversation, which is not what we
|
# processed after type conversation, which is not what we
|
||||||
# want.
|
# want.
|
||||||
value = self._type_conv[param.name](value)
|
value = self._type_conv[param.name](value)
|
||||||
return name, value
|
return value
|
||||||
|
|
||||||
def _get_call_args(self, win_id):
|
def _get_call_args(self, win_id):
|
||||||
"""Get arguments for a function call.
|
"""Get arguments for a function call.
|
||||||
@ -452,14 +456,14 @@ class Command:
|
|||||||
# Special case for win_id parameter.
|
# Special case for win_id parameter.
|
||||||
self._get_win_id_arg(win_id, param, args, kwargs)
|
self._get_win_id_arg(win_id, param, args, kwargs)
|
||||||
continue
|
continue
|
||||||
name, value = self._get_param_name_and_value(param)
|
value = self._get_param_value(param)
|
||||||
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
|
if param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
|
||||||
args.append(value)
|
args.append(value)
|
||||||
elif param.kind == inspect.Parameter.VAR_POSITIONAL:
|
elif param.kind == inspect.Parameter.VAR_POSITIONAL:
|
||||||
if value is not None:
|
if value is not None:
|
||||||
args += value
|
args += value
|
||||||
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
|
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
|
||||||
kwargs[name] = value
|
kwargs[param.name] = value
|
||||||
else:
|
else:
|
||||||
raise TypeError("{}: Invalid parameter type {} for argument "
|
raise TypeError("{}: Invalid parameter type {} for argument "
|
||||||
"'{}'!".format(
|
"'{}'!".format(
|
||||||
|
@ -37,7 +37,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir))
|
|||||||
import qutebrowser.app
|
import qutebrowser.app
|
||||||
from scripts import asciidoc2html, utils
|
from scripts import asciidoc2html, utils
|
||||||
from qutebrowser import qutebrowser
|
from qutebrowser import qutebrowser
|
||||||
from qutebrowser.commands import cmdutils
|
from qutebrowser.commands import cmdutils, command
|
||||||
from qutebrowser.config import configdata
|
from qutebrowser.config import configdata
|
||||||
from qutebrowser.utils import docutils
|
from qutebrowser.utils import docutils
|
||||||
|
|
||||||
@ -54,6 +54,14 @@ class UsageFormatter(argparse.HelpFormatter):
|
|||||||
"""Override _format_usage to not add the 'usage:' prefix."""
|
"""Override _format_usage to not add the 'usage:' prefix."""
|
||||||
return super()._format_usage(usage, actions, groups, '')
|
return super()._format_usage(usage, actions, groups, '')
|
||||||
|
|
||||||
|
def _get_default_metavar_for_optional(self, action):
|
||||||
|
"""Do name transforming when getting metavar."""
|
||||||
|
return command.arg_name(action.dest.upper())
|
||||||
|
|
||||||
|
def _get_default_metavar_for_positional(self, action):
|
||||||
|
"""Do name transforming when getting metavar."""
|
||||||
|
return command.arg_name(action.dest)
|
||||||
|
|
||||||
def _metavar_formatter(self, action, default_metavar):
|
def _metavar_formatter(self, action, default_metavar):
|
||||||
"""Override _metavar_formatter to add asciidoc markup to metavars.
|
"""Override _metavar_formatter to add asciidoc markup to metavars.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user