1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-22 12:53:00 +00:00

Parser singleton

This commit is contained in:
Jörn-Michael Miehe 2020-08-10 15:21:39 +02:00
parent e43dadd4fe
commit 65fcb91bd0
5 changed files with 32 additions and 26 deletions

View file

@ -30,7 +30,7 @@ def main():
for cmd in commands: for cmd in commands:
cmd.setup() cmd.setup()
args = kiwi.Parser.get_args() args = kiwi.Parser().get_args()
log_handler = logging.StreamHandler() log_handler = logging.StreamHandler()
logging.getLogger().addHandler(log_handler) logging.getLogger().addHandler(log_handler)

View file

@ -9,34 +9,40 @@ KIWI_CONF_NAME = os.getenv('KIWI_CONF_NAME', "kiwi.yml")
class Parser: class Parser:
__parser = None class __Parser:
__subparsers = None __parser = None
__args = None __subparsers = None
__args = None
@classmethod def __init__(self):
def get_parser(cls): self.__parser = argparse.ArgumentParser(description='kiwi-config')
if cls.__parser is None:
cls.__parser = argparse.ArgumentParser(description='kiwi-config')
cls.__parser.add_argument( self.__parser.add_argument(
'-v', '--verbosity', '-v', '--verbosity',
action='count', default=0 action='count', default=0
) )
return cls.__parser self.__subparsers = self.__parser.add_subparsers()
self.__subparsers.required = True
self.__subparsers.dest = 'command'
@classmethod def get_parser(self):
def get_subparsers(cls): return self.__parser
if cls.__subparsers is None:
cls.__subparsers = cls.get_parser().add_subparsers()
cls.__subparsers.required = True
cls.__subparsers.dest = 'command'
return cls.__subparsers def get_subparsers(self):
return self.__subparsers
@classmethod def get_args(self):
def get_args(cls): if self.__args is None:
if cls.__args is None: self.__args = self.__parser.parse_args()
cls.__args = cls.get_parser().parse_args()
return cls.__args return self.__args
__instance = None
def __init__(self):
if Parser.__instance is None:
Parser.__instance = Parser.__Parser()
def __getattr__(self, item):
return getattr(self.__instance, item)

View file

@ -36,7 +36,7 @@ class InitCommand(SubCommand):
@classmethod @classmethod
def setup(cls): def setup(cls):
parser = Parser.get_subparsers().add_parser( parser = Parser().get_subparsers().add_parser(
cls.command, cls.command,
description="Create a new kiwi-config instance" description="Create a new kiwi-config instance"
) )
@ -51,7 +51,7 @@ class InitCommand(SubCommand):
def run(cls): def run(cls):
logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'") logging.info(f"Initializing kiwi-config instance in '{os.getcwd()}'")
if Parser.get_args().force and os.path.isfile(KIWI_CONF_NAME): if Parser().get_args().force and os.path.isfile(KIWI_CONF_NAME):
logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!") logging.warning(f"Overwriting existing '{KIWI_CONF_NAME}'!")
config = DefaultConfig.get() config = DefaultConfig.get()
else: else:

View file

@ -8,7 +8,7 @@ class LogsCommand(SubCommand):
@classmethod @classmethod
def setup(cls): def setup(cls):
parser = Parser.get_subparsers().add_parser( _ = Parser().get_subparsers().add_parser(
cls.command, cls.command,
description="Show logs of a project" description="Show logs of a project"
) )

View file

@ -9,7 +9,7 @@ class ShowCommand(SubCommand):
@classmethod @classmethod
def setup(cls): def setup(cls):
parser = Parser.get_subparsers().add_parser( _ = Parser().get_subparsers().add_parser(
cls.command, cls.command,
description="Show effective kiwi.yml" description="Show effective kiwi.yml"
) )