-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
58 lines (43 loc) · 1.34 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import argparse
class Command:
"""An abstract class for commands"""
def __init__(self, name, description=""):
self.name = name
self.description = description
def execute(self, str_args):
pass
def print_help(self):
print("Unrecognised command")
class ParentCommand(Command):
"""A command that has other commands as children and will delegate arguments to them"""
def __init__(self, name, description=""):
super().__init__(name, description)
self.children = []
def add_child(self, command):
self.children.append(command)
def execute(self, str_args):
if len(str_args) == 0:
self.print_help()
return
for child in self.children:
if child.name == str_args[0]:
child.execute(str_args[1:])
return
self.print_help()
def print_help(self):
print("sub commands:")
for child in self.children:
print((" " + child.name).ljust(23), child.description)
class ArgCommand(Command):
"""A command that interprets arguments with an ArgumentParser"""
def __init__(self, name, method, description=""):
super().__init__(name, description)
self.method = method
self.parser = argparse.ArgumentParser(description)
def get_parser(self):
return self.parser
def execute(self, str_args):
if not self.method(self.parser.parse_args(str_args)):
self.parser.print_help()
def print_help(self):
self.parser.print_help()