support type names of spec arguments

This commit is contained in:
jesopo 2020-01-25 21:17:35 +00:00
parent 341b314104
commit 478223f88c
2 changed files with 28 additions and 21 deletions

View file

@ -38,12 +38,12 @@ class Module(ModuleManager.BaseModule):
n = 0 n = 0
error = None error = None
if spec_type.name == "time" and args: if spec_type.type == "time" and args:
time, _ = utils.parse.timed_args(args) time, _ = utils.parse.timed_args(args)
chunk = time chunk = time
n = 1 n = 1
error = "Invalid timeframe" error = "Invalid timeframe"
elif spec_type.name == "rchannel": elif spec_type.type == "rchannel":
if channel: if channel:
chunk = channel chunk = channel
elif args: elif args:
@ -53,41 +53,41 @@ class Module(ModuleManager.BaseModule):
error = "No such channel" error = "No such channel"
else: else:
error = "No channel provided" error = "No channel provided"
elif spec_type.name == "channel" and args: elif spec_type.type == "channel" and args:
if args[0] in server.channels: if args[0] in server.channels:
chunk = server.channels.get(args[0]) chunk = server.channels.get(args[0])
n = 1 n = 1
error = "No such channel" error = "No such channel"
elif spec_type.name == "cuser" and args: elif spec_type.type == "cuser" and args:
tuser = server.get_user(args[0], create=False) tuser = server.get_user(args[0], create=False)
if tuser and channel.has_user(tuser): if tuser and channel.has_user(tuser):
chunk = tuser chunk = tuser
n = 1 n = 1
error = "That user is not in this channel" error = "That user is not in this channel"
elif spec_type.name == "ruser": elif spec_type.type == "ruser":
if args: if args:
chunk = server.get_user(args[0], create=False) chunk = server.get_user(args[0], create=False)
n = 1 n = 1
else: else:
chunk = user chunk = user
error = "No such user" error = "No such user"
elif spec_type.name == "user": elif spec_type.type == "user":
if args: if args:
chunk = server.get_user(args[0], create=False) chunk = server.get_user(args[0], create=False)
n = 1 n = 1
error = "No such user" error = "No such user"
else: else:
error = "No user provided" error = "No user provided"
elif spec_type.name == "ouser" and args: elif spec_type.type == "ouser" and args:
if server.has_user_id(args[0]): if server.has_user_id(args[0]):
chunk = server.get_user(args[0]) chunk = server.get_user(args[0])
n = 1 n = 1
error = "Unknown nickname" error = "Unknown nickname"
elif spec_type.name == "word": elif spec_type.type == "word":
if args: if args:
chunk = args[0] chunk = args[0]
n = 1 n = 1
elif spec_type.name == "...": elif spec_type.type == "...":
if args: if args:
chunk = " ".join(args) chunk = " ".join(args)
n = max(1, len(args)) n = max(1, len(args))

View file

@ -157,7 +157,8 @@ def format_token_replace(s: str, vars: typing.Dict[str, str],
return s return s
class ArgumentSpecType(object): class ArgumentSpecType(object):
def __init__(self, name: str, exported: str): def __init__(self, type_name: str, name: str, exported: str):
self.type = type_name
self.name = name self.name = name
self.exported = exported self.exported = exported
@ -168,17 +169,23 @@ class ArgumentSpec(object):
def argument_spec(spec: str) -> typing.List[ArgumentSpec]: def argument_spec(spec: str) -> typing.List[ArgumentSpec]:
out: typing.List[ArgumentSpec] = [] out: typing.List[ArgumentSpec] = []
for type_names_str in spec.split(" "): for spec_argument in spec.split(" "):
optional = type_names_str[0] == "?" optional = spec_argument[0] == "?"
type_names_str = type_names_str[1:]
spec_types: typing.List[ArgumentSpecType] = [] argument_types: typing.List[ArgumentSpecType] = []
for type_name in type_names_str.split("|"): for argument_type in spec_argument[1:].split("|"):
exported_name = "" exported = ""
if "~" in type_name: if "~" in argument_type:
exported_name = type_name exported = argument_type.split("~", 1)[1]
type_name = type_name.replace("~", "", 1) argument_type = argument_type.replace("~", "", 1)
spec_types.append(ArgumentSpecType(type_name, exported_name)) argument_type_name = argument_type
out.append(ArgumentSpec(optional, spec_types)) name_end = argument_type.find(">")
if argument_type[0] == "<" and name_end > 0:
argument_type_name = argument_type[1:name_end]
argument_type = argument_type[name_end+1:]
argument_types.append(ArgumentSpecType(argument_type,
argument_type_name, exported))
out.append(ArgumentSpec(optional, argument_types))
return out return out