-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
82 lines (62 loc) · 2.32 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
""" The initial CLI command router """
import sys
import argparse
from importlib import import_module
from ..common.store import Store, StoreKeys
from ..common.logging import getLogger, loggingShutdown
log = getLogger(__name__)
MODULES = [
'init',
'test',
'compile',
'deploy',
'version',
'show',
'console',
'accounts',
'install',
'metafile',
]
IMPORTED_MODULES = {}
def parse_args(argv=None):
""" Parse command line arguments """
parser = argparse.ArgumentParser(description='SolidByte Ethereum development tools')
parser.add_argument('-d', action='store_true',
help='Print debug level messages')
parser.add_argument('-k', '--keystore', type=str, dest="keystore",
default='~/.ethereum/keystore',
help='Ethereum account keystore directory to use.')
subparsers = parser.add_subparsers(title='Submcommands', dest='command',
help='do the needful')
"""
Each module must implement the following minimum API:
- add_parser_arguments(parser) - A function that takes an argparse
parser and adds the arguments it wants for its command
implementation.
- main(parser_args) - The primary function to run that provides
parser_args as a kwarg
"""
for mod in MODULES:
IMPORTED_MODULES[mod] = import_module('solidbyte.cli.{}'.format(mod))
module_subparser = subparsers.add_parser(mod, help=IMPORTED_MODULES[mod].__doc__)
module_subparser = IMPORTED_MODULES[mod].add_parser_arguments(module_subparser)
# Help command
subparsers.add_parser('help', help='print usage')
return parser.parse_args(argv), parser
def main(argv=None):
args, parser = parse_args(argv)
if not hasattr(args, 'command') or not args.command:
log.warning('noop')
sys.exit(1)
if args.command == 'help':
parser.print_help()
sys.exit(0)
if args.command not in MODULES:
log.error('Unknown command: {}'.format(args.command))
sys.exit(2)
if args.keystore:
log.info("Using keystore at {}".format(args.keystore))
Store.set(StoreKeys.KEYSTORE_DIR, args.keystore)
IMPORTED_MODULES[args.command].main(parser_args=args)
loggingShutdown()
sys.exit(0)