From 5eea57472a249f0e4b1ef78e2eced67d7517494c Mon Sep 17 00:00:00 2001 From: LeBulldoge Date: Tue, 6 Jul 2021 22:05:49 +0200 Subject: [PATCH 1/2] An example of using Enums for command switches --- commands/base_commands/jobs.py | 19 +++++++++++++++++-- commands/mixins.py | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/commands/base_commands/jobs.py b/commands/base_commands/jobs.py index e244515e6..da3fa8012 100644 --- a/commands/base_commands/jobs.py +++ b/commands/base_commands/jobs.py @@ -8,6 +8,7 @@ database migration to add in their functionality. """ +from enum import Enum from django.conf import settings from evennia.utils.create import create_object @@ -289,6 +290,9 @@ class CmdRequest(ArxPlayerCommand): +featurerequest =<message> +prprequest <title>=<question about a player run plot> + Switches: + %s + Send a message to the GMs for help. This is usually because of wanting to take some action that requires GM intervention, such as a plot idea or some other in-game activity, but can @@ -322,6 +326,16 @@ class CmdRequest(ArxPlayerCommand): help_category = "Admin" locks = "cmd:all()" + class Switches(Enum): + followup = "<#>=<message> | Add a followup message to a request." + close = "<#>=<reason> | Close a request prematurely and provide a reason." + + switch_options = Switches + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.format_docstring() + def display_ticket(self, ticket): """Display the ticket to the caller""" self.msg(ticket.display()) @@ -433,12 +447,13 @@ def comment_on_ticket(self): def func(self): """Implement the command""" + switches = [self.switch_options[string] for string in self.switches] caller = self.caller - if "followup" in self.switches or "comment" in self.switches: + if self.switch_options.followup in switches: self.comment_on_ticket() return - if "close" in self.switches: + if self.switch_options.close in switches: self.close_ticket(self.lhs, self.rhs) return diff --git a/commands/mixins.py b/commands/mixins.py index 1b5d8eba1..c2e84e327 100644 --- a/commands/mixins.py +++ b/commands/mixins.py @@ -12,6 +12,11 @@ class ArxCommmandMixin(object): error_class = CommandError help_entry_tags = [] + def format_docstring(self): + self.__doc__ = self.__doc__ % "%r ".join( + f"{switch.name} {switch.value}" for switch in self.switch_options + ) + def fail(self, msg): """Raises an error for the class with a given message""" raise self.error_class(msg) From 138878f5ab82b50b768f91cf6eb6d9e304ca680d Mon Sep 17 00:00:00 2001 From: LeBulldoge <alexey13031@gmail.com> Date: Tue, 13 Jul 2021 22:04:18 +0200 Subject: [PATCH 2/2] Utilizing the __members__ property --- commands/base_commands/jobs.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/commands/base_commands/jobs.py b/commands/base_commands/jobs.py index da3fa8012..81a710f07 100644 --- a/commands/base_commands/jobs.py +++ b/commands/base_commands/jobs.py @@ -330,7 +330,7 @@ class Switches(Enum): followup = "<#>=<message> | Add a followup message to a request." close = "<#>=<reason> | Close a request prematurely and provide a reason." - switch_options = Switches + switch_options = Switches.__members__ def __init__(self, **kwargs): super().__init__(**kwargs) @@ -447,13 +447,13 @@ def comment_on_ticket(self): def func(self): """Implement the command""" - switches = [self.switch_options[string] for string in self.switches] + switches = [self.Switches[string] for string in self.switches] caller = self.caller - if self.switch_options.followup in switches: + if self.Switches.followup in switches: self.comment_on_ticket() return - if self.switch_options.close in switches: + if self.Switches.close in switches: self.close_ticket(self.lhs, self.rhs) return