Move creation of ArgumentParser to caller

This commit is contained in:
Erik Johnston 2019-07-15 13:43:25 +01:00
parent 37b524f971
commit fdefb9e29a
2 changed files with 8 additions and 11 deletions

View File

@ -13,6 +13,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import argparse
import logging import logging
import sys import sys
@ -105,7 +106,8 @@ def export_data_command(hs, user_id, directory):
def start(config_options): def start(config_options):
parser = HomeServerConfig.create_argument_parser("Synapse Admin Command") parser = argparse.ArgumentParser(description="Synapse Admin Command")
HomeServerConfig.add_arguments_to_parser(parser)
subparser = parser.add_subparsers( subparser = parser.add_subparsers(
title="Admin Commands", title="Admin Commands",

View File

@ -231,27 +231,24 @@ class Config(object):
Returns: Config object. Returns: Config object.
""" """
config_parser = cls.create_argument_parser(description) config_parser = argparse.ArgumentParser(description=description)
cls.add_arguments_to_parser(config_parser)
obj, _ = cls.load_config_with_parser(config_parser, argv) obj, _ = cls.load_config_with_parser(config_parser, argv)
return obj return obj
@classmethod @classmethod
def create_argument_parser(cls, description): def add_arguments_to_parser(cls, config_parser):
"""Create an ArgumentParser instance with all the config flags. """Adds all the config flags to an ArgumentParser.
Doesn't support config-file-generation: used by the worker apps. Doesn't support config-file-generation: used by the worker apps.
Used for workers where we want to add extra flags/subcommands. Used for workers where we want to add extra flags/subcommands.
Args: Args:
description (str): App description config_parser (ArgumentParser): App description
Returns:
ArgumentParser
""" """
config_parser = argparse.ArgumentParser(description=description)
config_parser.add_argument( config_parser.add_argument(
"-c", "-c",
"--config-path", "--config-path",
@ -273,8 +270,6 @@ class Config(object):
# `add_arguments` should be side effect free so this is probably fine. # `add_arguments` should be side effect free so this is probably fine.
cls.invoke_all_static("add_arguments", config_parser) cls.invoke_all_static("add_arguments", config_parser)
return config_parser
@classmethod @classmethod
def load_config_with_parser(cls, config_parser, argv): def load_config_with_parser(cls, config_parser, argv):
"""Parse the commandline and config files with the given parser """Parse the commandline and config files with the given parser