From c0f2e7d8ddfc8d79395bfc1b749939df5b3430d1 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 16 Jan 2019 09:28:29 +0100 Subject: [PATCH] remove 'add_arguments' function --- examples/Hangman/example.py | 3 +-- examples/remote/example.py | 3 +-- tiramisu_cmdline_parser.py | 33 +++++++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/examples/Hangman/example.py b/examples/Hangman/example.py index e7d3fdf..ab28578 100644 --- a/examples/Hangman/example.py +++ b/examples/Hangman/example.py @@ -149,8 +149,7 @@ def main(): # options) storage_type.set('sqlite3') config = Config(OptionDescription('root', 'root', [word, proposal_word, misses, proposals_left] + options), persistent=True, session_id='hangman') - parser = TiramisuCmdlineParser() - parser.add_arguments(config) + parser = TiramisuCmdlineParser(config) try: parser.parse_args() except ValueError: diff --git a/examples/remote/example.py b/examples/remote/example.py index 6eff0eb..1814edd 100644 --- a/examples/remote/example.py +++ b/examples/remote/example.py @@ -22,8 +22,7 @@ class RemoteConfig(Config): def main(): config = RemoteConfig('http://localhost:8000') - parser = TiramisuCmdlineParser() - parser.add_arguments(config) + parser = TiramisuCmdlineParser(config) parser.parse_args() config = parser.get_config() print(config.value.dict()) diff --git a/tiramisu_cmdline_parser.py b/tiramisu_cmdline_parser.py index b4394ac..3f9b901 100644 --- a/tiramisu_cmdline_parser.py +++ b/tiramisu_cmdline_parser.py @@ -71,13 +71,17 @@ class _TiramisuHelpAction(_HelpAction): class TiramisuCmdlineParser(ArgumentParser): def __init__(self, + config: Union[Config, ConfigJson], *args, fullpath: bool=True, + _forhelp: bool=False, **kwargs): self.fullpath = fullpath - self.config = None + self.config = config super().__init__(*args, **kwargs) self.register('action', 'help', _TiramisuHelpAction) + self._config_to_argparser(_forhelp, + self.config.option) def _pop_action_class(self, kwargs, default=None): 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): # option that was disabled are no more disable # so create a new parser - new_parser = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) - new_parser._registries = self._registries - new_parser.add_arguments(self.config) + new_parser = TiramisuCmdlineParser(self.config, + self.prog, + fullpath=self.fullpath) namespace_, args_ = new_parser._parse_known_args(args_, namespace) else: if self._registries['action']['help'].needs: @@ -125,6 +129,9 @@ class TiramisuCmdlineParser(ArgumentParser): else: 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): raise NotImplementedError('do not use add_subparsers') @@ -208,12 +215,6 @@ class TiramisuCmdlineParser(ArgumentParser): for args, kwargs in actions.values(): 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): kwargs['namespace'] = TiramisuNamespace(self.config) @@ -237,13 +238,17 @@ class TiramisuCmdlineParser(ArgumentParser): def format_usage(self, *args, **kwargs): - help_formatter = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) - help_formatter.add_arguments(self.config, _forhelp=True) + help_formatter = TiramisuCmdlineParser(self.config, + self.prog, + fullpath=self.fullpath, + _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_usage(*args, **kwargs) def format_help(self, *args, **kwargs): - help_formatter = TiramisuCmdlineParser(self.prog, fullpath=self.fullpath) - help_formatter.add_arguments(self.config, _forhelp=True) + help_formatter = TiramisuCmdlineParser(self.config, + self.prog, + fullpath=self.fullpath, + _forhelp=True) return super(TiramisuCmdlineParser, help_formatter).format_help(*args, **kwargs) def get_config(self):