diff --git a/cccli/command/account.py b/cccli/command/account.py index 52bd7e930cd3fe7054d7e0bcf3f72b8cb4313012..52b5467d408b5774974b724db3ab3916d59c379e 100644 --- a/cccli/command/account.py +++ b/cccli/command/account.py @@ -22,7 +22,8 @@ class Command_addaccount(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: addaccount " + return "Usage: addaccount " + class Command_delaccount(Command): '''Delete an account''' @@ -36,7 +37,7 @@ class Command_delaccount(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: delaccount " + return "Usage: delaccount " class Command_close(Command): @@ -51,7 +52,7 @@ class Command_close(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: close " + return "Usage: close " class Command_declose(Command): @@ -67,7 +68,7 @@ class Command_declose(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: declose " + return "Usage: declose " class Command_passwd(Command): @@ -95,4 +96,4 @@ class Command_passwd(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: passwd [tql] [password]" + return "Usage: passwd [tql] [password]" diff --git a/cccli/command/alias.py b/cccli/command/alias.py index 7a6ac0c235f77e8c422cfab2feaf2137a84a81ec..f39163fda32fb362fe90a11d3daa07edc09161bd 100644 --- a/cccli/command/alias.py +++ b/cccli/command/alias.py @@ -10,6 +10,7 @@ from cccli.command.command import Command class Command_alias(Command): '''Show or create alias''' + def __call__(self, argv): if len(argv) == 1: for n, v in self.cli.alias.items(): @@ -25,10 +26,12 @@ class Command_alias(Command): raise cmdBadArgument() def usage(self): - return "usage: alias [name] [value]" + return "Usage: alias [name] [value]" + class Command_unalias(Command): '''Remove an alias''' + def __call__(self, argv): if len(argv) != 2: raise cmdBadArgument() @@ -38,4 +41,4 @@ class Command_unalias(Command): self.cli.alias.save(self.cli.settings.get("alias", "")) def usage(self): - return "usage: unalias [name]" + return "Usage: unalias [name]" diff --git a/cccli/command/command.py b/cccli/command/command.py index 2f2c7e4e1051fd2c57bae23dba9cb3d9ed1fbe87..5b4aa9d75492c45a5c0ac47f413247e14643920a 100644 --- a/cccli/command/command.py +++ b/cccli/command/command.py @@ -5,14 +5,30 @@ CloudControl CLI command module ''' +from optparse import OptionParser + class Command(object): - def __init__(self, cli): + def __init__(self, cli, argv0): self.cli = cli self.printer = self.cli.printer + self.name = argv0 + + def usage(self): + return "Usage: %s"%self.name + + def help(self): + return self.__doc__ + + +class OptionCommand(Command): + + def __init__(self, cli, argv0): + Command.__init__(self, cli, argv0) + self.option = OptionParser(prog=argv0) def usage(self): - return None + return self.option.format_help().strip() def help(self): return self.__doc__ diff --git a/cccli/command/connection.py b/cccli/command/connection.py index bbc5215d3f8b6ebb46b5034fd58ac3b610af0216..297c46ee9ccfa279fd449456add9a94521a7bcc2 100644 --- a/cccli/command/connection.py +++ b/cccli/command/connection.py @@ -26,7 +26,8 @@ class Command_uptime(Command): self.printer.out("%s: %ss"%(o["a"], o["con"])) def usage(self): - return "usage: uptime [tql]" + return "Usage: uptime [tql]" + class Command_remote(Command): '''Show remote command list''' @@ -38,8 +39,6 @@ class Command_remote(Command): except RpcError as e: raise cmdError("RPCError: %s"%str(e)) - def usage(self): - return "usage: remote" class Command_whoami(Command): '''Show connection login''' @@ -47,8 +46,6 @@ class Command_whoami(Command): def __call__(self, argv): self.printer.out(self.cli.settings["login"]) - def usage(self): - return "usage: whoami" class Command_kill(Command): '''Kill a server connection''' diff --git a/cccli/command/host.py b/cccli/command/host.py index 24babee387c5ee92161c7d0d68d89cc785d12fb6..5c749dd37657d482d859c7950b36c5e63e07ccad 100644 --- a/cccli/command/host.py +++ b/cccli/command/host.py @@ -19,7 +19,7 @@ class Command_exec(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: exec " + return "Usage: exec " class Command_shutdown(Command): '''Shutdown a physical host''' @@ -32,4 +32,4 @@ class Command_shutdown(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: shutdown " + return "Usage: shutdown " diff --git a/cccli/command/list.py b/cccli/command/list.py index 0ec210a48aeed6df7d28611951b7edad1b688e6d..2f520ae126bed261cb47a75a6a816c025bfe0abe 100644 --- a/cccli/command/list.py +++ b/cccli/command/list.py @@ -8,20 +8,22 @@ CloudControl list command from cccli.exception import * from sjrpc.core.exceptions import * from cccli.printer import Printer, color -from cccli.command.command import Command -from optparse import OptionParser +from cccli.command.command import OptionCommand -class Command_list(Command): +class Command_list(OptionCommand): '''List objects''' + def __init__(self, cli, argv0): + OptionCommand.__init__(self, cli, argv0) + self.option.set_usage("%prog [options] ") + self.option.add_option("-c", action="store_true", dest="table", + help="column aligment display") + self.option.add_option("-l", action="store_true", dest="align", + help="line aligment display") + def __call__(self, argv): try: - oparser = OptionParser(prog="list") - oparser.add_option("-c", action="store_true", dest="table", - help="column aligment display") - oparser.add_option("-l", action="store_true", dest="align", - help="line aligment display") - (options, args) = oparser.parse_args(argv[1:]) + (options, args) = self.option.parse_args(argv[1:]) except SystemExit: return if len(args) == 0: @@ -39,9 +41,6 @@ class Command_list(Command): else: self._list(objs) - def usage(self): - return "usage: list [-t] [-a] [--help] " - def _list(self, objs): for o in objs: id = o.pop("id") diff --git a/cccli/command/right.py b/cccli/command/right.py index 434d2c1620129c67874d7590707937486f8ad451..65c2a211f1896894af4efb1e9cdfe892de9a6df6 100644 --- a/cccli/command/right.py +++ b/cccli/command/right.py @@ -45,7 +45,7 @@ class Command_rights(Command): self.printer.out("[%s] %s"%(i,tags)) def usage(self): - return "usage: rights [--raw] [--help] [tql]" + return "Usage: rights [--raw] [--help] [tql]" class Command_addright(Command): @@ -60,7 +60,7 @@ class Command_addright(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: addright " + return "Usage: addright " class Command_delright(Command): @@ -75,4 +75,4 @@ class Command_delright(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: delright " + return "Usage: delright " diff --git a/cccli/command/shell.py b/cccli/command/shell.py index 1d85f9b8d351f3a27259890eae910b8c512c7852..5a78f54988d87948e036285d53ba510e1cf311c2 100644 --- a/cccli/command/shell.py +++ b/cccli/command/shell.py @@ -14,19 +14,12 @@ class Command_quit(Command): def __call__(self, argv): raise SystemExit() - def usage(self): - return "usage: quit" - - class Command_version(Command): '''Print cli version''' def __call__(self, argv): import cccli self.printer.out(cccli.version) - def usage(self): - return "usage: version" - class Command_history(Command): '''Show commands history''' @@ -36,8 +29,12 @@ class Command_history(Command): for l in self.printer.history: self.printer.out(l) - def usage(self): - return "usage: history" + +class Command_clear(Command): + '''Clear tty''' + + def __call__(self, argv): + self.printer.out("\033[H\033[2J", nl="") class Command_help(Command): @@ -61,7 +58,7 @@ class Command_help(Command): raise cmdBadArgument() def usage(self): - return "usage: help [command]" + return "Usage: help [command]" class Command_usage(Command): @@ -77,14 +74,4 @@ class Command_usage(Command): self.printer.out(usage) def usage(self): - return "usage: usage " - - -class Command_clear(Command): - '''Clear tty''' - - def __call__(self, argv): - self.printer.out("\033[H\033[2J", nl="") - - def usage(self): - return "usage: clear" + return "Usage: usage " diff --git a/cccli/command/tag.py b/cccli/command/tag.py index 7313e3030303cae6d14b9a11ccb1302cceb596da..f5b03f43c74513d4f4944183d1dc10267e4fafbc 100644 --- a/cccli/command/tag.py +++ b/cccli/command/tag.py @@ -44,7 +44,7 @@ class Command_tags(Command): self.printer.out("%sid:%s%s%s %s"%(color["green"], color["yellow"], id, color["reset"], tags)) def usage(self): - return "usage: tags [--raw] [--help] [tql]" + return "Usage: tags [--raw] [--help] [tql]" class Command_addtag(Command): @@ -59,7 +59,7 @@ class Command_addtag(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: addtag " + return "Usage: addtag " class Command_deltag(Command): @@ -74,4 +74,4 @@ class Command_deltag(Command): raise cmdError("RPCError: %s"%str(e)) def usage(self): - return "usage: deltag " + return "Usage: deltag " diff --git a/cccli/command/vm.py b/cccli/command/vm.py index 5f4f938aedf2c6c11f6cb9d86a883c7beb6134d3..d57e5bf30f2f8351c36760ca98edaa79c2e6e543 100644 --- a/cccli/command/vm.py +++ b/cccli/command/vm.py @@ -79,7 +79,7 @@ class Command_start(VmCommand): self._vm_action(argv, "&role=vm&status=stopped") def usage(self): - return "usage: start [--raw] [--direct] [--force] [--help] " + return "Usage: start [--raw] [--direct] [--force] [--help] " class Command_stop(VmCommand): @@ -89,7 +89,7 @@ class Command_stop(VmCommand): self._vm_action(argv, "&role=vm&status=running") def usage(self): - return "usage: stop [--raw] [--direct] [--force] [--help] " + return "Usage: stop [--raw] [--direct] [--force] [--help] " class Command_destroy(VmCommand): @@ -99,7 +99,7 @@ class Command_destroy(VmCommand): self._vm_action(argv, "&role=vm&status!=stopped") def usage(self): - return "usage: destroy [--raw] [--direct] [--force] [--help] " + return "Usage: destroy [--raw] [--direct] [--force] [--help] " class Command_pause(VmCommand): '''Pause a running vm''' @@ -108,7 +108,7 @@ class Command_pause(VmCommand): self._vm_action(argv, "&role=vm&status=running") def usage(self): - return "usage: pause [--raw] [--direct] [--force] [--help] " + return "Usage: pause [--raw] [--direct] [--force] [--help] " class Command_resume(VmCommand): @@ -118,4 +118,4 @@ class Command_resume(VmCommand): self._vm_action(argv, "&role=vm&status=stalled") def usage(self): - return "usage: resume [--raw] [--direct] [--force] [--help] " + return "Usage: resume [--raw] [--direct] [--force] [--help] " diff --git a/cccli/commands.py b/cccli/commands.py index 9f356b5761c6790b349216012442d8bfaf12de8e..3d3bb574ede64bc31e88d3487d6f2d0778d7e11d 100644 --- a/cccli/commands.py +++ b/cccli/commands.py @@ -45,19 +45,17 @@ class Commands(object): else: raise cmdBadName() # create class and call it - cmd = self.cmds[argv[0]](self.cli) - ret = cmd(argv) - del cmd - return ret + cmd = self.cmds[argv[0]](self.cli, argv[0]) + return cmd(argv) def usage(self, argv0): '''Return usage of a command''' - u = self.cmds[argv0](self.cli).usage() + u = self.cmds[argv0](self.cli, argv0).usage() return u if u is not None else "" def help(self, argv0): '''Return of a command''' - h = self.cmds[argv0](self.cli).help() + h = self.cmds[argv0](self.cli, argv0).help() return h if h is not None else "" class Alias(dict):