Added arg_regex, an optional Command class-method for customizing how commands are identified by using a regex to enforce a specific look/grammar to the command argument. Discussed and suggested in issue 213.
This commit is contained in:
parent
42254fa3c4
commit
690bfadd9d
4 changed files with 13 additions and 3 deletions
|
|
@ -47,9 +47,11 @@ def cmdparser(raw_string, cmdset, caller, match_index=None):
|
||||||
l_raw_string = raw_string.lower()
|
l_raw_string = raw_string.lower()
|
||||||
for cmd in cmdset:
|
for cmd in cmdset:
|
||||||
matches.extend([create_match(cmdname, raw_string, cmd)
|
matches.extend([create_match(cmdname, raw_string, cmd)
|
||||||
for cmdname in [cmd.key] + cmd.aliases
|
for cmdname in [cmd.key] + cmd.aliases
|
||||||
if cmdname and l_raw_string.startswith(cmdname.lower())])
|
if cmdname and l_raw_string.startswith(cmdname.lower())
|
||||||
if not matches:
|
and (not cmd.arg_regex or
|
||||||
|
cmd.arg_regex.match(l_raw_string[len(cmdname):]))])
|
||||||
|
if not matches:
|
||||||
# no matches found.
|
# no matches found.
|
||||||
if '-' in raw_string:
|
if '-' in raw_string:
|
||||||
# This could be due to the user trying to identify the
|
# This could be due to the user trying to identify the
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ All commands in Evennia inherit from the 'Command' class in this module.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
from src.locks.lockhandler import LockHandler
|
from src.locks.lockhandler import LockHandler
|
||||||
from src.utils.utils import is_iter
|
from src.utils.utils import is_iter
|
||||||
|
|
||||||
|
|
@ -42,6 +43,11 @@ class CommandMeta(type):
|
||||||
temp.append(lockstring)
|
temp.append(lockstring)
|
||||||
mcs.lock_storage = ";".join(temp)
|
mcs.lock_storage = ";".join(temp)
|
||||||
|
|
||||||
|
if hasattr(mcs, 'arg_regex') and isinstance(mcs.arg_regex, basestring):
|
||||||
|
mcs.arg_regex = re.compile(r"%s" % mcs.arg_regex, re.I)
|
||||||
|
else:
|
||||||
|
mcs.arg_regex = None
|
||||||
|
|
||||||
if not hasattr(mcs, 'is_exit'):
|
if not hasattr(mcs, 'is_exit'):
|
||||||
mcs.is_exit = False
|
mcs.is_exit = False
|
||||||
if not hasattr(mcs, "help_category"):
|
if not hasattr(mcs, "help_category"):
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ class CmdLook(MuxCommand):
|
||||||
key = "look"
|
key = "look"
|
||||||
aliases = ["l", "ls"]
|
aliases = ["l", "ls"]
|
||||||
locks = "cmd:all()"
|
locks = "cmd:all()"
|
||||||
|
arg_regex = r"\s.*?|$"
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -500,6 +500,7 @@ class Exit(Object):
|
||||||
"""
|
"""
|
||||||
locks = "cmd:all()" # should always be set to this.
|
locks = "cmd:all()" # should always be set to this.
|
||||||
obj = None
|
obj = None
|
||||||
|
arg_regex=r"\s.*?|$"
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"Default exit traverse if no syscommand is defined."
|
"Default exit traverse if no syscommand is defined."
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue