From e3cd965179cb645bd49dbc804a2735371e144ce0 Mon Sep 17 00:00:00 2001 From: Seblu Date: Fri, 4 Feb 2011 20:00:06 +0100 Subject: [PATCH] add a new OptionCommand class which add option parser to command class --- cccli/command/account.py | 11 ++++++----- cccli/command/alias.py | 7 +++++-- cccli/command/command.py | 20 ++++++++++++++++++-- cccli/command/connection.py | 7 ++----- cccli/command/host.py | 4 ++-- cccli/command/list.py | 23 +++++++++++------------ cccli/command/right.py | 6 +++--- cccli/command/shell.py | 29 ++++++++--------------------- cccli/command/tag.py | 6 +++--- cccli/command/vm.py | 10 +++++----- cccli/commands.py | 10 ++++------ 11 files changed, 67 insertions(+), 66 deletions(-) diff --git a/cccli/command/account.py b/cccli/command/account.py index 52bd7e9..52b5467 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 7a6ac0c..f39163f 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 2f2c7e4..5b4aa9d 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 bbc5215..297c46e 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 24babee..5c749dd 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 0ec210a..2f520ae 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 434d2c1..65c2a21 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 1d85f9b..5a78f54 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 7313e30..f5b03f4 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 5f4f938..d57e5bf 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 9f356b5..3d3bb57 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): -- GitLab