Add initial CLI support
This commit is contained in:
parent
e16d67e2f8
commit
22a9c68611
14 changed files with 168 additions and 26 deletions
38
trovedb/cli/_cli_main.py
Normal file
38
trovedb/cli/_cli_main.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
import argparse
|
||||
import importlib
|
||||
import pkgutil
|
||||
|
||||
import trovedb.cli as cli
|
||||
from . import cli_common
|
||||
|
||||
def _discover_commands():
|
||||
commands = {}
|
||||
for info in pkgutil.iter_modules(cli.__path__):
|
||||
if info.name.startswith('_'):
|
||||
continue
|
||||
mod = importlib.import_module(f'trovedb.cli.{info.name}')
|
||||
if hasattr(mod, 'setup') and hasattr(mod, 'run'):
|
||||
# strip trailing _ so 'import_' becomes 'import'
|
||||
name = info.name.rstrip('_')
|
||||
commands[name] = mod
|
||||
return commands
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(prog='trove')
|
||||
|
||||
cli_common.common_args(parser)
|
||||
|
||||
subparsers = parser.add_subparsers(dest='command', required=True)
|
||||
commands = _discover_commands()
|
||||
for name, mod in sorted(commands.items()):
|
||||
sub = subparsers.add_parser(name, help=getattr(mod, '__doc__', None))
|
||||
mod.setup(sub)
|
||||
|
||||
args = parser.parse_args()
|
||||
env = cli_common.get_env(args)
|
||||
|
||||
commands[args.command].run(env, args)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue