forked from uncloud/uncloud
uncloud cli converted to argparse, code isn't beautiful yet. Would make it soom
This commit is contained in:
parent
cd2f0aaa0d
commit
50fb135726
14 changed files with 278 additions and 264 deletions
|
|
@ -1,24 +1,23 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import click
|
||||
import argparse
|
||||
import importlib
|
||||
|
||||
from uncloud.cli.commands.vm import vm
|
||||
from uncloud.cli.commands.user import user
|
||||
from uncloud.cli.commands.host import host
|
||||
from uncloud.cli.commands.image import image
|
||||
from uncloud.cli.commands.network import network
|
||||
arg_parser = argparse.ArgumentParser('cli', add_help=False)
|
||||
subparser = arg_parser.add_subparsers(dest='subcommand')
|
||||
|
||||
for component in ['user', 'host', 'image', 'network', 'vm']:
|
||||
module = importlib.import_module('uncloud.cli.{}'.format(component))
|
||||
parser = getattr(module, 'arg_parser')
|
||||
subparser.add_parser(name=parser.prog, parents=[parser])
|
||||
|
||||
|
||||
@click.group()
|
||||
def cli():
|
||||
pass
|
||||
|
||||
|
||||
cli.add_command(vm)
|
||||
cli.add_command(user)
|
||||
cli.add_command(image)
|
||||
cli.add_command(host)
|
||||
cli.add_command(network)
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
def main(**kwargs):
|
||||
if not kwargs['subcommand']:
|
||||
arg_parser.print_help()
|
||||
else:
|
||||
name = kwargs.pop('subcommand')
|
||||
kwargs.pop('debug')
|
||||
mod = importlib.import_module('uncloud.cli.{}'.format(name))
|
||||
_main = getattr(mod, 'main')
|
||||
_main(**kwargs)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue