cmdutils: Bind keyword-only arguments as flags.

This commit is contained in:
Florian Bruhin 2014-09-14 23:25:40 +02:00
parent fe08052641
commit 66f0aa8d5f

View File

@ -271,7 +271,8 @@ class register: # pylint: disable=invalid-name
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: if (self._get_type(param, annotation_info) == bool or
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)
@ -309,6 +310,8 @@ class register: # pylint: disable=invalid-name
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:
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
kwargs['nargs'] = '?' kwargs['nargs'] = '?'