remove 'add_arguments' function

This commit is contained in:
Emmanuel Garette 2019-01-16 09:28:29 +01:00 committed by Emmanuel Garette
parent 195abb4299
commit e2951f1a21
3 changed files with 21 additions and 18 deletions

View file

@ -149,8 +149,7 @@ def main():
# options) # options)
storage_type.set('sqlite3') storage_type.set('sqlite3')
config = Config(OptionDescription('root', 'root', [word, proposal_word, misses, proposals_left] + options), persistent=True, session_id='hangman') config = Config(OptionDescription('root', 'root', [word, proposal_word, misses, proposals_left] + options), persistent=True, session_id='hangman')
parser = TiramisuCmdlineParser() parser = TiramisuCmdlineParser(config)
parser.add_arguments(config)
try: try:
parser.parse_args() parser.parse_args()
except ValueError: except ValueError:

View file

@ -22,8 +22,7 @@ class RemoteConfig(Config):
def main(): def main():
config = RemoteConfig('http://localhost:8000') config = RemoteConfig('http://localhost:8000')
parser = TiramisuCmdlineParser() parser = TiramisuCmdlineParser(config)
parser.add_arguments(config)
parser.parse_args() parser.parse_args()
config = parser.get_config() config = parser.get_config()
print(config.value.dict()) print(config.value.dict())

View file

@ -71,13 +71,17 @@ class _TiramisuHelpAction(_HelpAction):
class TiramisuCmdlineParser(ArgumentParser): class TiramisuCmdlineParser(ArgumentParser):
def __init__(self, def __init__(self,
config: Union[Config, ConfigJson],
*args, *args,
fullpath: bool=True, fullpath: bool=True,
_forhelp: bool=False,
**kwargs): **kwargs):
self.fullpath = fullpath self.fullpath = fullpath
self.config = None self.config = config
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.register('action', 'help', _TiramisuHelpAction) self.register('action', 'help', _TiramisuHelpAction)
self._config_to_argparser(_forhelp,
self.config.option)
def _pop_action_class(self, kwargs, default=None): def _pop_action_class(self, kwargs, default=None):
ret = super()._pop_action_class(kwargs, default) ret = super()._pop_action_class(kwargs, default)
@ -107,9 +111,9 @@ class TiramisuCmdlineParser(ArgumentParser):
if args != args_ and args_ and args_[0].startswith(self.prefix_chars): if args != args_ and args_ and args_[0].startswith(self.prefix_chars):
# option that was disabled are no more disable # option that was disabled are no more disable
# so create a new parser # so create a new parser
new_parser = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) new_parser = TiramisuCmdlineParser(self.config,
new_parser._registries = self._registries self.prog,
new_parser.add_arguments(self.config) fullpath=self.fullpath)
namespace_, args_ = new_parser._parse_known_args(args_, namespace) namespace_, args_ = new_parser._parse_known_args(args_, namespace)
else: else:
if self._registries['action']['help'].needs: if self._registries['action']['help'].needs:
@ -125,6 +129,9 @@ class TiramisuCmdlineParser(ArgumentParser):
else: else:
raise NotImplementedError('do not use add_argument') raise NotImplementedError('do not use add_argument')
def add_arguments(self, *args, **kwargs):
raise NotImplementedError('do not use add_argument')
def add_subparsers(self, *args, **kwargs): def add_subparsers(self, *args, **kwargs):
raise NotImplementedError('do not use add_subparsers') raise NotImplementedError('do not use add_subparsers')
@ -208,12 +215,6 @@ class TiramisuCmdlineParser(ArgumentParser):
for args, kwargs in actions.values(): for args, kwargs in actions.values():
group.add_argument(*args, **kwargs) group.add_argument(*args, **kwargs)
def add_arguments(self,
tiramisu: Union[Config, ConfigJson],
_forhelp: bool=False) -> None:
self.config = tiramisu
self._config_to_argparser(_forhelp,
self.config.option)
def parse_args(self, *args, **kwargs): def parse_args(self, *args, **kwargs):
kwargs['namespace'] = TiramisuNamespace(self.config) kwargs['namespace'] = TiramisuNamespace(self.config)
@ -237,13 +238,17 @@ class TiramisuCmdlineParser(ArgumentParser):
def format_usage(self, def format_usage(self,
*args, *args,
**kwargs): **kwargs):
help_formatter = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) help_formatter = TiramisuCmdlineParser(self.config,
help_formatter.add_arguments(self.config, _forhelp=True) self.prog,
fullpath=self.fullpath,
_forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs)
def format_help(self, *args, **kwargs): def format_help(self, *args, **kwargs):
help_formatter = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) help_formatter = TiramisuCmdlineParser(self.config,
help_formatter.add_arguments(self.config, _forhelp=True) self.prog,
fullpath=self.fullpath,
_forhelp=True)
return super(TiramisuCmdlineParser, help_formatter).format_help(*args, **kwargs) return super(TiramisuCmdlineParser, help_formatter).format_help(*args, **kwargs)
def get_config(self): def get_config(self):