Add alias/delete switch

This commit is contained in:
Chiizujin 2024-04-01 18:20:10 +11:00
parent a9e8042bbe
commit 5427817112
2 changed files with 40 additions and 5 deletions

View file

@ -218,10 +218,13 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
alias <obj> [= [alias[,alias,alias,...]]] alias <obj> [= [alias[,alias,alias,...]]]
alias <obj> = alias <obj> =
alias/category <obj> = [alias[,alias,...]:<category> alias/category <obj> = [alias[,alias,...]:<category>
alias/delete <obj> = <alias>
Switches: Switches:
category - requires ending input with :category, to store the category - requires ending input with :category, to store the
given aliases with the given category. given aliases with the given category.
delete - deletes all occurrences of the given alias, regardless
of category
Assigns aliases to an object so it can be referenced by more Assigns aliases to an object so it can be referenced by more
than one name. Assign empty to remove all aliases from object. If than one name. Assign empty to remove all aliases from object. If
@ -235,7 +238,7 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
key = "@alias" key = "@alias"
aliases = "setobjalias" aliases = "setobjalias"
switch_options = ("category",) switch_options = ("category", "delete")
locks = "cmd:perm(setobjalias) or perm(Builder)" locks = "cmd:perm(setobjalias) or perm(Builder)"
help_category = "Building" help_category = "Building"
@ -252,12 +255,12 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
return return
objname = self.lhs objname = self.lhs
# Find the object to receive aliases # Find the object to receive/delete aliases
obj = caller.search(objname) obj = caller.search(objname)
if not obj: if not obj:
return return
if self.rhs is None: if self.rhs is None and 'delete' not in self.switches:
# no =, so we just list aliases on object. # no =, and not deleting, so we just list aliases on object.
aliases = obj.aliases.all(return_key_and_category=True) aliases = obj.aliases.all(return_key_and_category=True)
if aliases: if aliases:
caller.msg( caller.msg(
@ -280,7 +283,9 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
return return
if not self.rhs: if not self.rhs:
# we have given an empty =, so delete aliases # we have given an empty =, so delete aliases.
# as a side-effect, 'alias/delete obj' and 'alias/delete obj='
# will also be caught here, which is fine
old_aliases = obj.aliases.all() old_aliases = obj.aliases.all()
if old_aliases: if old_aliases:
caller.msg( caller.msg(
@ -292,6 +297,19 @@ class CmdSetObjAlias(COMMAND_DEFAULT_CLASS):
caller.msg("No aliases to clear.") caller.msg("No aliases to clear.")
return return
if "delete" in self.switches:
# delete all matching keys, regardless of category
existed = False
for key, category in obj.aliases.all(return_key_and_category=True):
if key == self.rhs:
obj.aliases.remove(key=self.rhs, category=category)
existed = True
if existed:
caller.msg("Alias '%s' deleted from %s." % (self.rhs, obj.get_display_name(caller)))
else:
caller.msg("%s has no alias '%s'." % (obj.get_display_name(caller), self.rhs))
return
category = None category = None
if "category" in self.switches: if "category" in self.switches:
if ":" in self.rhs: if ":" in self.rhs:

View file

@ -790,6 +790,23 @@ class TestBuilding(BaseEvenniaCommandTest):
self.call(building.CmdSetObjAlias(), "Obj2 =", "Cleared aliases from Obj2") self.call(building.CmdSetObjAlias(), "Obj2 =", "Cleared aliases from Obj2")
self.call(building.CmdSetObjAlias(), "Obj2 =", "No aliases to clear.") self.call(building.CmdSetObjAlias(), "Obj2 =", "No aliases to clear.")
self.call(building.CmdSetObjAlias(), "Obj =", "Cleared aliases from Obj: testobj1b")
self.call(building.CmdSetObjAlias(),
"/category Obj = testobj1b:category1",
"Alias(es) for 'Obj' set to 'testobj1b' (category: 'category1')."
)
self.call(
building.CmdSetObjAlias(),
"/category Obj = testobj1b:category2",
"Alias(es) for 'Obj' set to 'testobj1b,testobj1b' (category: 'category2')."
)
self.call(
building.CmdSetObjAlias(), # delete both occurences of alias 'testobj1b'
"/delete Obj = testobj1b",
"Alias 'testobj1b' deleted from Obj."
)
self.call(building.CmdSetObjAlias(), "Obj =", "No aliases to clear.")
def test_copy(self): def test_copy(self):
self.call( self.call(
building.CmdCopy(), building.CmdCopy(),