cmdutils: Merge _param_to_argparse_args and _param_to_argparse_kw.

This commit is contained in:
Florian Bruhin 2014-09-14 23:34:55 +02:00
parent 7e37b657f5
commit 063be350e4

View File

@ -240,13 +240,14 @@ class register: # pylint: disable=invalid-name
for param in signature.parameters.values(): for param in signature.parameters.values():
if param.name in ('self', 'count'): if param.name in ('self', 'count'):
continue continue
args = [] argparse_args = []
kwargs = {} argparse_kwargs = {}
annotation_info = self._parse_annotation(param) annotation_info = self._parse_annotation(param)
kwargs.update(self._param_to_argparse_kw( args, kwargs = self._param_to_argparse_args(
param, annotation_info)) param, annotation_info)
kwargs.update(annotation_info.kwargs) argparse_args += args
args += self._param_to_argparse_pos(param, annotation_info) argparse_kwargs.update(kwargs)
argparse_kwargs.update(annotation_info.kwargs)
typ = self._get_type(param, annotation_info) typ = self._get_type(param, annotation_info)
if utils.is_enum(typ): if utils.is_enum(typ):
type_conv[param.name] = argparser.enum_getter(typ) type_conv[param.name] = argparser.enum_getter(typ)
@ -254,25 +255,60 @@ class register: # pylint: disable=invalid-name
if param.default is not inspect.Parameter.empty: if param.default is not inspect.Parameter.empty:
typ = typ + (type(param.default),) typ = typ + (type(param.default),)
type_conv[param.name] = argparser.multitype_conv(typ) type_conv[param.name] = argparser.multitype_conv(typ)
callsig = debugutils.format_call(self.parser.add_argument, callsig = debugutils.format_call(
args, kwargs, full=False) self.parser.add_argument, argparse_args, argparse_kwargs,
full=False)
log.commands.vdebug('Adding arg {} of type {} -> {}'.format( log.commands.vdebug('Adding arg {} of type {} -> {}'.format(
param.name, typ, callsig)) param.name, typ, callsig))
self.parser.add_argument(*args, **kwargs) self.parser.add_argument(*argparse_args, **argparse_kwargs)
return has_count, desc, type_conv return has_count, desc, type_conv
def _param_to_argparse_pos(self, param, annotation_info): def _param_to_argparse_args(self, param, annotation_info):
"""Get a list of positional argparse arguments. """Get argparse arguments for a parameter.
Return:
An (args, kwargs) tuple.
Args: Args:
param: The inspect.Parameter instance for the current parameter. param: The inspect.Parameter object to get the args for.
annotation_info: An AnnotationInfo tuple for the parameter. annotation_info: An AnnotationInfo tuple for the parameter.
""" """
ParamType = usertypes.enum('ParamType', 'flag', 'positional')
kwargs = {}
typ = self._get_type(param, annotation_info)
param_type = ParamType.positional
try:
kwargs['help'] = self.docparser.arg_descs[param.name]
except KeyError:
pass
if isinstance(typ, tuple):
pass
elif utils.is_enum(typ):
kwargs['choices'] = [e.name.replace('_', '-') for e in typ]
kwargs['metavar'] = param.name
elif typ is bool:
param_type = ParamType.flag
kwargs['action'] = 'store_true'
elif typ is not None:
kwargs['type'] = typ
if param.kind == inspect.Parameter.VAR_POSITIONAL:
kwargs['nargs'] = '+'
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
param_type = ParamType.flag
kwargs['default'] = param.default
elif typ is not bool and param.default is not inspect.Parameter.empty:
kwargs['default'] = param.default
kwargs['nargs'] = '?'
args = [] args = []
name = annotation_info.name or param.name name = annotation_info.name or param.name
shortname = annotation_info.flag or param.name[0] shortname = annotation_info.flag or param.name[0]
if (self._get_type(param, annotation_info) == bool or if param_type == ParamType.flag:
param.kind == inspect.Parameter.KEYWORD_ONLY):
long_flag = '--{}'.format(name) long_flag = '--{}'.format(name)
short_flag = '-{}'.format(shortname) short_flag = '-{}'.format(shortname)
args.append(long_flag) args.append(long_flag)
@ -281,42 +317,8 @@ class register: # pylint: disable=invalid-name
else: else:
args.append(name) args.append(name)
self.pos_args.append(name) self.pos_args.append(name)
return args
def _param_to_argparse_kw(self, param, annotation_info): return args, kwargs
"""Get argparse keyword arguments for a parameter.
Args:
param: The inspect.Parameter object to get the args for.
annotation_info: An AnnotationInfo tuple for the parameter.
"""
kwargs = {}
try:
kwargs['help'] = self.docparser.arg_descs[param.name]
except KeyError:
pass
typ = self._get_type(param, annotation_info)
if isinstance(typ, tuple):
pass
elif utils.is_enum(typ):
kwargs['choices'] = [e.name.replace('_', '-') for e in typ]
kwargs['metavar'] = param.name
elif typ is bool:
kwargs['action'] = 'store_true'
elif typ is not None:
kwargs['type'] = typ
if param.kind == inspect.Parameter.VAR_POSITIONAL:
kwargs['nargs'] = '+'
elif param.kind == inspect.Parameter.KEYWORD_ONLY:
kwargs['default'] = param.default
elif typ is not bool and param.default is not inspect.Parameter.empty:
kwargs['default'] = param.default
kwargs['nargs'] = '?'
return kwargs
def _parse_annotation(self, param): def _parse_annotation(self, param):
"""Get argparse arguments and type from a parameter annotation. """Get argparse arguments and type from a parameter annotation.