class Command(object): def __init__(self): self.version = get_tbears_version() self._create_parser() self.cmdServer = CommandServer(self.subparsers) self.cmdScore = CommandScore(self.subparsers) self.cmdUtil = CommandUtil(self.subparsers) self.cmdWallet = CommandWallet(self.subparsers) def _create_parser(self): parser = argparse.ArgumentParser( prog='tbears', description=f'tbears v{self.version} arguments') parser.add_argument('-d', '--debug', help='Debug mode', action='store_true') subparsers = parser.add_subparsers( title='Available commands', metavar='command', description=f'If you want to see help message of commands, ' f'use "tbears command -h"') subparsers.required = True subparsers.dest = 'command' self.parser = parser self.subparsers = subparsers return parser def run(self, sys_args) -> Optional: # sys_args is list of commands splitted by space # e.g. tbears deploy project -k keystore => ['deploy', 'project', '-k', 'keystore'] try: # parse_args return the populated namespace # e.g. Namespace (command='deploy', config=None, keyStore='keystore' ...) args = self.parser.parse_args(args=sys_args) if self.cmdServer.check_command(args.command): result = self.cmdServer.run(args) elif self.cmdScore.check_command(args.command): result = self.cmdScore.run(args) elif self.cmdUtil.check_command(args.command): result = self.cmdUtil.run(args) elif self.cmdWallet.check_command(args.command): result = self.cmdWallet.run(args) except TBearsBaseException as e: print(f"{e}") return e.code.value except Exception as e: print(f"Exception: {e}") return TBearsExceptionCode.COMMAND_ERROR.value else: return result
class Command(object): def __init__(self): self.version = get_tbears_version() self._create_parser() self.cmdServer = CommandServer(self.subparsers) self.cmdScore = CommandScore(self.subparsers) self.cmdUtil = CommandUtil(self.subparsers) self.cmdWallet = CommandWallet(self.subparsers) def _create_parser(self): parser = TbearsParser(prog='tbears', description=f'tbears v{self.version} arguments') parser.add_argument('-v', '--verbose', help='Verbose mode', action='store_true') subparsers = parser.add_subparsers( title='Available commands', metavar='command', description=f'If you want to see help message of commands, ' f'use "tbears command -h"') subparsers.required = True subparsers.dest = 'command' self.parser = parser self.subparsers = subparsers return parser def run(self, sys_args) -> Optional: # sys_args is list of commands splitted by space # e.g. tbears deploy project -k keystore => ['deploy', 'project', '-k', 'keystore'] try: # parse_args return the populated namespace # e.g. Namespace (command='deploy', config=None, keyStore='keystore' ...) args = self.parser.parse_args(args=sys_args) self._init_logger(args=args) if self.cmdServer.check_command(args.command): result = self.cmdServer.run(args) elif self.cmdScore.check_command(args.command): result = self.cmdScore.run(args) elif self.cmdUtil.check_command(args.command): result = self.cmdUtil.run(args) elif self.cmdWallet.check_command(args.command): result = self.cmdWallet.run(args) except TBearsBaseException as e: print(f"{e}") return e.code.value except Exception as e: print(f"Exception: {e}") return TBearsExceptionCode.COMMAND_ERROR.value else: return result def _init_logger(self, args): from iconcommons.icon_config import IconConfig if args.verbose: conf = IconConfig(None, tbears_server_config) conf.load() if 'console' not in conf['log']['outputType']: conf['log'][ 'outputType'] = conf['log']['outputType'] + "|console" Logger.load_config(conf)