Skip to content
Snippets Groups Projects
Commit f2387fc0 authored by Seblu's avatar Seblu
Browse files

command auto expansion

Command are now callable with a part of it.
e.g. a, al, ali,alia, call alias
parent 2d908e50
No related branches found
No related tags found
No related merge requests found
...@@ -122,15 +122,18 @@ class Cli(object): ...@@ -122,15 +122,18 @@ class Cli(object):
# execute command # execute command
Command(argv, self).call() Command(argv, self).call()
except BadArgument, e: except BadArgument, e:
if str(e) != "": if str(e):
printer.error("Bad argument: %s."%str(e)) printer.error("Bad argument: %s."%str(e))
else: else:
printer.error("Bad argument.") printer.error("Bad argument.")
usage = Command.usage(argv[0]) usage = Command.usage(argv[0])
if usage != "": if usage != "":
printer.out("usage: %s."%usage) printer.out("usage: %s."%usage)
except BadCommand: except BadCommand, e:
printer.error("No command: %s."%argv[0]) if str(e):
printer.error("command: %s."%str(e))
else:
printer.error("No command: %s."%argv[0])
except sjrpc.core.exceptions.RpcError, e: except sjrpc.core.exceptions.RpcError, e:
if cccli.debug: raise if cccli.debug: raise
printer.error("sjRPC: %s"%str(e)) printer.error("sjRPC: %s"%str(e))
......
...@@ -10,8 +10,23 @@ from cccli.clierror import * ...@@ -10,8 +10,23 @@ from cccli.clierror import *
class Command(object): class Command(object):
def __init__(self, argv, cli): def __init__(self, argv, cli):
# check argv
if len(argv) < 1: if len(argv) < 1:
raise BadCommand() raise BadCommand()
# check valid command chars
if not re.match("^[a-zA-Z0-9]+", argv[0]):
raise BadCommmand("Invalid command name")
cmdlist = [ x[4:] for x in dir(self) if x.startswith("cmd_") ]
matchlist = [ x for x in cmdlist if re.match("%s.+"%argv[0], x) ]
if argv[0] in cmdlist:
pass
elif len(matchlist) == 1:
argv[0] = matchlist[0]
elif len(matchlist) > 1:
raise BadCommand("Too many command: %s"%", ".join(matchlist))
else:
raise BadCommand()
self._cmd = getattr(self, "cmd_%s"%argv[0])
self._argv = argv self._argv = argv
self.cli = cli self.cli = cli
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment