-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_parser.py
144 lines (117 loc) · 3.92 KB
/
command_parser.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!usr/bin/env python3
import argparse
import sys
import re
"""
Initial (very early) proof of concept for command parser. Not sure if this is the best route.
argparse is really useful but has a few issues for our use case, the worst of which is how it handles parsing failures.
As far as I can tell all it does it print the error the stderr and exit. We can wrap it in a try/except to prevent
the process from exiting but i'm not sure if it's possible to capture the error type/message.
I'll have to look into that further...
Good news is argparse can be fed a list in place of sys.argv, so we could use it to parse commands inside a running
client or server process.
"""
# defines a realname: a string beginning with ':', can include a space (for first name and last name)
def realNameType(arg, pattern=re.compile(r":[A-Za-z0-9]+\s*[A-Za-z0-9]")):
if not pattern.match(arg):
raise argparse.ArgumentTypeError("realname must begin with ':'")
return arg
# defines a room: a string beginning with '#'
def roomType(arg, pattern=re.compile(r"#[A-Za-z0-9]+")):
if not pattern.match(arg):
raise argparse.ArgumentTypeError("room must begin with '#'")
return arg
# Functions to set up parsers for specific IRC commands
def parseUser(userParser, argv):
userParser.add_argument("nick")
userParser.add_argument("realname", type=realNameType)
userParser.add_argument("lastname", nargs="?")
try:
args = userParser.parse_args(argv)
except:
return None
if args.lastname:
args.realname += " " + args.lastname
del args.lastname
return args
def parseJoin(joinParser, argv):
joinParser.add_argument("room", type=roomType)
try:
args = joinParser.parse_args(argv)
except:
return None
args.room = args.room.split(",")
return args
def parseList(listParser, argv):
listParser.add_argument("--rooms")
listParser.add_argument("--server")
try:
return listParser.parse_args(argv)
except:
return None
def parsePart(partParser, argv):
partParser.add_argument("rooms")
partParser.add_argument("message", nargs="*")
try:
args = partParser.parse_args(argv)
except:
return None
args.message = " ".join(args.message)
args.rooms = args.rooms.split(",")
return args
def parseQuit(quitParser, argv):
quitParser.add_argument("message", nargs="*")
try:
args = quitParser.parse_args(argv)
except:
return None
args.message = " ".join(args.message)
return args
def parseNames(namesParser, argv):
namesParser.add_argument("rooms", nargs="?")
try:
return namesParser.parse_args(argv)
except:
return None
def parsePrivmsg(privmsgParser, argv):
privmsgParser.add_argument("msgtarget")
privmsgParser.add_argument("message", nargs="+")
try:
args = privmsgParser.parse_args(argv)
except:
return None
args.message = " ".join(args.message)
args.msgtarget = args.msgtarget.split(",")
return args
ircCommands = {
"join": parseJoin,
"list": parseList,
"part": parsePart,
"quit": parseQuit,
"names": parseNames,
"user": parseUser,
"privmsg": parsePrivmsg,
}
def parseCommand(argv):
"""
parse commands according to the commandArg passed in.
commandArg must be a supported IRC command in ircCommands dict.
"""
if len(argv) > 0:
commandArg = argv[0]
else:
print("no arguments received")
return None
command = ircCommands.get(commandArg)
if command:
parser = argparse.ArgumentParser(description="parse IRC command")
parser.add_argument("command", help="a supported IRC command")
args = command(parser, argv)
parser.parse_args
else:
print("command not supported: {}".format(commandArg))
return None
return args
if __name__ == "__main__":
args = parseCommand(sys.argv[1:])
print(args)