--root.v => -v, --int ROOT.INT => --int INT

This commit is contained in:
2019-04-01 21:08:41 +02:00
parent 708e623107
commit f90c02282a
2 changed files with 317 additions and 15 deletions

View File

@ -15,7 +15,7 @@
from typing import Union, List, Optional
from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction
from argparse import ArgumentParser, Namespace, SUPPRESS, _HelpAction, HelpFormatter
try:
from tiramisu import Config
from tiramisu.error import PropertiesOptionError
@ -61,6 +61,15 @@ class TiramisuNamespace(Namespace):
return super().__getattribute__(key)
class TiramisuHelpFormatter(HelpFormatter):
def _get_default_metavar_for_optional(self,
action):
ret = super()._get_default_metavar_for_optional(action)
if '.' in ret:
ret = ret.split('.', 1)[1]
return ret
class _TiramisuHelpAction(_HelpAction):
needs = False
def __call__(self, *args, **kwargs):
@ -79,6 +88,7 @@ class TiramisuCmdlineParser(ArgumentParser):
**kwargs):
self.fullpath = fullpath
self.config = config
kwargs['formatter_class'] = TiramisuHelpFormatter
super().__init__(*args, **kwargs)
self.register('action', 'help', _TiramisuHelpAction)
self._config_to_argparser(_forhelp,
@ -170,7 +180,7 @@ class TiramisuCmdlineParser(ArgumentParser):
properties = obj.property.get()
kwargs = {'help': option.doc().replace('%', '%%')}
if option.issymlinkoption():
actions[option.name(follow_symlink=True)][0].insert(0, self._gen_argument(name, properties))
actions[option.name(follow_symlink=True)][0].insert(0, self._gen_argument(option.name(), properties))
continue
if 'positional' in properties:
if not 'mandatory' in properties:
@ -224,7 +234,6 @@ class TiramisuCmdlineParser(ArgumentParser):
for args, kwargs in actions.values():
group.add_argument(*args, **kwargs)
def parse_args(self,
*args,
valid_mandatory=True,
@ -236,7 +245,7 @@ class TiramisuCmdlineParser(ArgumentParser):
except PropertiesOptionError as err:
name = err._option_bag.option.impl_getname()
properties = self.config.option(name).property.get()
if 'positional' not in properties:
if self.fullpath and 'positional' not in properties:
if len(name) == 1 and 'longargument' not in properties:
name = self.prefix_chars + name
else:
@ -256,6 +265,8 @@ class TiramisuCmdlineParser(ArgumentParser):
args = self._gen_argument(name, self.config.option(key).property.get())
else:
args = key
if not self.fullpath and '.' in args:
args = args.rsplit('.', 1)[1]
self.error('the following arguments are required: {}'.format(args))
return namespaces