command: Clean up ParamType.

This commit is contained in:
Florian Bruhin 2014-09-15 07:43:40 +02:00
parent e1d93fa3fa
commit 8a51aa759e

View File

@ -51,10 +51,12 @@ class Command:
Class attributes: Class attributes:
AnnotationInfo: Named tuple for info from an annotation. AnnotationInfo: Named tuple for info from an annotation.
ParamType: Enum for an argparse parameter type.
""" """
AnnotationInfo = collections.namedtuple('AnnotationInfo', AnnotationInfo = collections.namedtuple('AnnotationInfo',
'kwargs, typ, name, flag') 'kwargs, typ, name, flag')
ParamType = usertypes.enum('ParamType', 'flag', 'positional')
def __init__(self, name, split, hide, instance, completion, modes, def __init__(self, name, split, hide, instance, completion, modes,
not_modes, needs_js, is_debug, ignore_args, not_modes, needs_js, is_debug, ignore_args,
@ -201,11 +203,9 @@ class Command:
annotation_info: An AnnotationInfo tuple for the parameter. annotation_info: An AnnotationInfo tuple for the parameter.
""" """
ParamType = usertypes.enum('ParamType', 'flag', 'positional')
kwargs = {} kwargs = {}
typ = self._get_type(param, annotation_info) typ = self._get_type(param, annotation_info)
param_type = ParamType.positional param_type = self.ParamType.positional
try: try:
kwargs['help'] = self.docparser.arg_descs[param.name] kwargs['help'] = self.docparser.arg_descs[param.name]
@ -218,7 +218,7 @@ class Command:
kwargs['choices'] = [e.name.replace('_', '-') for e in typ] kwargs['choices'] = [e.name.replace('_', '-') for e in typ]
kwargs['metavar'] = param.name kwargs['metavar'] = param.name
elif typ is bool: elif typ is bool:
param_type = ParamType.flag param_type = self.ParamType.flag
kwargs['action'] = 'store_true' kwargs['action'] = 'store_true'
elif typ is not None: elif typ is not None:
kwargs['type'] = typ kwargs['type'] = typ
@ -226,7 +226,7 @@ class Command:
if param.kind == inspect.Parameter.VAR_POSITIONAL: if param.kind == inspect.Parameter.VAR_POSITIONAL:
kwargs['nargs'] = '+' kwargs['nargs'] = '+'
elif param.kind == inspect.Parameter.KEYWORD_ONLY: elif param.kind == inspect.Parameter.KEYWORD_ONLY:
param_type = ParamType.flag param_type = self.ParamType.flag
kwargs['default'] = param.default kwargs['default'] = param.default
elif typ is not bool and param.default is not inspect.Parameter.empty: elif typ is not bool and param.default is not inspect.Parameter.empty:
kwargs['default'] = param.default kwargs['default'] = param.default
@ -235,15 +235,17 @@ class Command:
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 param_type == ParamType.flag: if param_type == self.ParamType.flag:
long_flag = '--{}'.format(name) long_flag = '--{}'.format(name)
short_flag = '-{}'.format(shortname) short_flag = '-{}'.format(shortname)
args.append(long_flag) args.append(long_flag)
args.append(short_flag) args.append(short_flag)
self.opt_args[param.name] = long_flag, short_flag self.opt_args[param.name] = long_flag, short_flag
else: elif param_type == self.ParamType.positional:
args.append(name) args.append(name)
self.pos_args.append((param.name, name)) self.pos_args.append((param.name, name))
else:
raise ValueError("Invalid ParamType {}!".format(param_type))
kwargs.update(annotation_info.kwargs) kwargs.update(annotation_info.kwargs)
return args, kwargs return args, kwargs