  
 
[Snippet] Command Parser - Printable Version 
 
-Shoutbox (https://shoutbox.menthix.net) 
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58) 
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4) 
+---- Forum: Scripting (/forumdisplay.php?fid=39) 
+----- Thread: [Snippet] Command Parser (/showthread.php?tid=64877)
 
[Snippet] Command Parser by Shondoit on 08-13-2006 at 06:51 PM
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 
} 
  
 
 |