What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Snippet] Command Parser

[Snippet] Command Parser
Author: Message:
Shondoit
Full Member
***

Avatar
Hmm, Just Me...

Posts: 227
Reputation: 15
36 / Male / Flag
Joined: Jul 2006
O.P. [Snippet] Command Parser
Thought I'd help, for the newest people under us, or maybe even the more experienced ones....

code:
function CommandParser (Message) {
   //If Message is not undefined, pass it to 'Parse'
   if (Message) this.Parse(Message);

   this.Parse = function (Message) {
      //A RegExp to check if it is a command... with params if possible
      //PM me if you need any help with this step

      this.IsCommand = /^\/([^\s]+)(?:\s+(.+))?/i.test(Message)
      if (this.IsCommand) {
         //If it is a command, the commandname and params are stored
         this.Command = RegExp.$1
         this.Param = (RegExp.$2 === "") ? undefined : RegExp.$2
      } else {
         //Else, they are set to undefined
         this.Command = undefined
         this.Param = undefined
      }
   }
   //Check if the command parsed is a given command (compare)
   this.CommandIs = function (Command) {
      if (this.IsCommand) {
         return (this.Command.toLowerCase() == Command.toLowerCase())
      }
   }
   //Return the parameters if any, split when necesary, or else return undefined
   //RegExp's can be used as Delimiter, to check if the're multiple spaces
   //Like in my example

   this.Params = function(Delimiter){
      if (this.IsCommand) {
         return (this.Param == undefined) ? undefined : (Delimiter) ? this.Param.split(Delimiter) : this.Param
      }
   }
}


Here's an example...
code:
function OnEvent_ChatWndSendMessage (ChatWnd, Message) {
   var Parser = new CommandParser(Message)
   //Could also be written as:
   //var Parser = new CommandParser()
   //Parser.Parse(Message)
   //Or you could set Parser as a global var...
   if (Parser.CommandIs("ShowLog")) {
      var Emails = Parser.Params(/\s+/)
      if (Emails) {
         for (Offset in Emails) {
            //Call a custom function
            ShowLog(Emails[Offset])
         }
         Message = ""
      }
   }
   return Message
}


This post was edited on 08-13-2006 at 06:58 PM by Shondoit.
My scripts:                            [Image: shondoit.gif]
+ Timezone
+ Camelo
+ Multisearch
08-13-2006 06:51 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On