-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.py
45 lines (37 loc) · 1.29 KB
/
Command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
known_commands = {}
def default_run(options, arg):
"Command was not defined"
print "Undefined command"
pass
class Command():
def verify_options(self,options):
for option in options:
if option not in self.options:
print option, "is not a valid option"
return False
return True
def __call__(self,options,args):
if self.verify_options(options):
try:
self.run(options,args)
except:
self.__help__()
def __help__(self):
print self.description, self.usage
def __init__(self, name="",aliases="",options="", usage="", run=None):
known_commands[name] = name
if aliases != "":
for alias in aliases.split(","):
known_commands[alias] = name
self.options = options
self.usage = usage
if run == None:
self.description=default_run.__doc__
self.run = default_run
self.usage = "[OPTION] -h"
else:
self.description = run.__doc__
if self.description == None:
self.description = "No description found for this command"
self.run = run
invalid_command = Command(name="invalid_command",aliases="",options="")