Automatically format example text for OptionsSetting (showing options)

This commit is contained in:
jesopo 2019-06-29 21:33:26 +01:00
parent a732bcfc25
commit b4c762eb4e
4 changed files with 21 additions and 8 deletions

View file

@ -20,8 +20,7 @@ class InvalidTimeoutException(Exception):
"Set ban format ($n = nick, $u = username, $h = hostname)",
example="*!$u@$h"))
@utils.export("serverset", utils.OptionsSetting("mute-method",
["qmode", "insp", "unreal"], "Set this server's method of muting users",
example="qmode"))
["qmode", "insp", "unreal"], "Set this server's method of muting users"))
class Module(ModuleManager.BaseModule):
_name = "ChanOp"

View file

@ -84,7 +84,7 @@ class Module(ModuleManager.BaseModule):
if setting_info:
value = target.get_setting(require_setting, None)
if value == None:
example = setting_info.exaple or "<value>"
example = setting_info.get_example() or "<value>"
return "Please set %s, e.g.: %sconfig %s %s %s" % (
require_setting, event["command_prefix"], context[0],
require_setting, example)
@ -180,9 +180,9 @@ class Module(ModuleManager.BaseModule):
try:
result = self._config(export_settings, target, setting, value)
except ConfigInvalidValue:
example = export_settings[setting].example
example = export_settings[setting].get_example()
if not example == None:
raise utils.EventError("Invalid value. Example: %s" %
raise utils.EventError("Invalid value. %s" %
example)
else:
raise utils.EventError("Invalid value")

View file

@ -10,8 +10,8 @@ REQUIRES_IDENTIFY_INTERNAL = ("You need to be identified to use that command "
"(/msg %s register | /msg %s identify)")
@utils.export("serverset", utils.OptionsSetting("identity-mechanism",
["internal", "ircv3-account"], "Set the identity mechanism for this server",
example="ircv3-account"))
["internal", "ircv3-account"],
"Set the identity mechanism for this server"))
class Module(ModuleManager.BaseModule):
@utils.hook("new.user")
def new_user(self, event):

View file

@ -230,13 +230,23 @@ def is_main_thread() -> bool:
return threading.current_thread() is threading.main_thread()
class Setting(object):
example: str = None
def __init__(self, name: str, help: str=None, example: str=None):
self.name = name
self.help = help
if not example == None:
self.example = example
def parse(self, value: str) -> typing.Any:
return value
def get_example(self):
if not self.example == None:
return "Example: %s" % self.example
else:
return self._format_example()
def _format_example(self):
return None
SETTING_TRUE = ["true", "yes", "on", "y"]
SETTING_FALSE = ["false", "no", "off", "n"]
class BoolSetting(Setting):
@ -269,3 +279,7 @@ class OptionsSetting(Setting):
if option.lower() == value_lower:
return option
return None
def _format_example(self):
options = ["'%s'" % option for option in self._options]
return "Options: %s" % ", ".join(options)