doc.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // This file is subject to a 1-clause BSD license.
  2. // Its contents can be found in the enclosed LICENSE file.
  3. /*
  4. Package cmd allows the definition of command handlers to be called
  5. by users from either a channel or a private message. These are messages
  6. like the following:
  7. !join #test
  8. Beginning with a predefined character (! in this case), followed by the
  9. command name and an optional set of arguments. This package parses the
  10. command data, verifies it refers to an existing command and ensures that
  11. the parameter values have the correct formats.
  12. For example:
  13. join := cmd.Bind("join", true, onJoin)
  14. join.Add("channel", true, cmd.RegChannel)
  15. join.Add("password", false, cmd.RegAny)
  16. ...
  17. func onJoin(w irc.ResponseWriter, r *cmd.Request) {
  18. var c irc.Channel
  19. c.Name = r.String(0)
  20. if r.Len() > 1 {
  21. c.Password = r.String(1)
  22. }
  23. proto.Join(w, c)
  24. }
  25. The name and description texts for the command and parameters, are there for
  26. user documentation. You can bind a `!help` command to `cmd.HelpHandler`, which
  27. will present the user either with an overview of all registered commands, or
  28. get detailed help on a specific command.
  29. The `cmd.RegXXX` values passed into the parameter definitions are predefined
  30. regular expressions. You are free to pass in your own patterns. These are used
  31. to ensure a parameter value given by a user, matches your expectations.
  32. If this is not the case, the command handler is never executed and the user
  33. is presented with an appropriate error response.
  34. By the time the registered command handler is actually called, you may be
  35. certain that the parameter value matches your definition.
  36. The boolean value pass passed into each parameter definition determines if
  37. that specific parameter is optional or not. This provides for rudimentary
  38. varargs functionality.
  39. In the example listed above, the user may call the `join` command in one
  40. of two ways:
  41. !join #channel
  42. !join #channel somepassword
  43. A parameter occupying multiple whitespace separated words, is to be supplied
  44. in double quotes:
  45. !join #channel "some long password"
  46. */
  47. package cmd