evennia/game/gamesrc/commands/examples/command.py

133 lines
5 KiB
Python
Raw Normal View History

"""
2012-03-30 23:57:04 +02:00
Example command module template
Copy this module up one level to gamesrc/commands/ and name it as
befits your use. You can then use it as a template to define your new
commands. To use them you also need to group them in a CommandSet (see
examples/cmdset.py)
"""
from ev import Command as BaseCommand
from ev import default_cmds
from ev import utils
class Command(BaseCommand):
"""
Inherit from this if you want to create your own
2012-03-30 23:57:04 +02:00
command styles. Note that Evennia's default commands
use MuxCommand instead (next in this module)
Note that the class's __doc__ string (this text) is
used by Evennia to create the automatic help entry for
2012-03-30 23:57:04 +02:00
the command, so make sure to document consistently here.
"""
2012-03-30 23:57:04 +02:00
# these need to be specified
key = "MyCommand"
2012-03-30 23:57:04 +02:00
aliases = ["mycmd", "myc"]
locks = "cmd:all()"
help_category = "General"
# auto_help = False # uncomment to deactive auto-help for this command.
2012-03-30 23:57:04 +02:00
# arg_regex = r"\s.*?|$" # optional regex detailing how the part after
# the cmdname must look to match this command.
# (we don't implement hook method access() here, you don't need to
# modify that unless you want to change how the lock system works
# (in that case see src.commands.command.Command))
2012-03-30 23:57:04 +02:00
def at_pre_cmd(self):
"""
This hook is called before self.parse() on all commands
"""
pass
def parse(self):
"""
This method is called by the cmdhandler once the command name
has been identified. It creates a new set of member variables
that can be later accessed from self.func() (see below)
2012-03-30 23:57:04 +02:00
The following variables are available to us:
# class variables:
self.key - the name of this command ('mycommand')
self.aliases - the aliases of this cmd ('mycmd','myc')
self.locks - lock string for this command ("cmd:all()")
self.help_category - overall category of command ("General")
2012-03-30 23:57:04 +02:00
# added at run-time by cmdhandler:
self.caller - the object calling this command
self.cmdstring - the actual command name used to call this
(this allows you to know which alias was used,
for example)
self.args - the raw input; everything following self.cmdstring.
self.cmdset - the cmdset from which this command was picked. Not
2012-03-30 23:57:04 +02:00
often used (useful for commands like 'help' or to
list all available commands etc)
self.obj - the object on which this command was defined. It is often
2012-03-30 23:57:04 +02:00
the same as self.caller.
"""
pass
def func(self):
"""
This is the hook function that actually does all the work. It is called
by the cmdhandler right after self.parser() finishes, and so has access
2012-03-30 23:57:04 +02:00
to all the variables defined therein.
"""
self.caller.msg("Command called!")
def at_post_cmd(self):
"""
2012-03-30 23:57:04 +02:00
This hook is called after self.func().
"""
pass
class MuxCommand(default_cmds.MuxCommand):
"""
This sets up the basis for a Evennia's 'MUX-like' command
style. The idea is that most other Mux-related commands should
just inherit from this and don't have to implement parsing of
their own unless they do something particularly advanced.
A MUXCommand command understands the following possible syntax:
name[ with several words][/switch[/switch..]] arg1[,arg2,...] [[=|,] arg[,..]]
The 'name[ with several words]' part is already dealt with by the
2012-03-30 23:57:04 +02:00
cmdhandler at this point, and stored in self.cmdname. The rest is stored
in self.args.
The MuxCommand parser breaks self.args into its constituents and stores them
in the following variables:
self.switches = optional list of /switches (without the /)
self.raw = This is the raw argument input, including switches
self.args = This is re-defined to be everything *except* the switches
2012-03-30 23:57:04 +02:00
self.lhs = Everything to the left of = (lhs:'left-hand side'). If
no = is found, this is identical to self.args.
2012-03-30 23:57:04 +02:00
self.rhs: Everything to the right of = (rhs:'right-hand side').
If no '=' is found, this is None.
self.lhslist - self.lhs split into a list by comma
self.rhslist - list of self.rhs split into a list by comma
self.arglist = list of space-separated args (including '=' if it exists)
2012-03-30 23:57:04 +02:00
All args and list members are stripped of excess whitespace around the
strings, but case is preserved.
"""
2012-03-30 23:57:04 +02:00
def func(self):
"""
This is the hook function that actually does all the work. It is called
by the cmdhandler right after self.parser() finishes, and so has access
2012-03-30 23:57:04 +02:00
to all the variables defined therein.
"""
# this can be removed in your child class, it's just
# printing the ingoing variables as a demo.
super(MuxCommand, self).func()