0.1.3 • Published 10 years ago

chatcommand-parser v0.1.3

Weekly downloads
15
License
-
Repository
github
Last release
10 years ago

Chatcommand Parser

A package to parse commands in text chats (IRC, Slack, ...)

Installation

npm install chatcommand-parser

Usage

Creating commands

var parser = require("chatcommand-parser");

var p = new parser.Parser("!"); //You can optionally pass a prefix string to prefix every command with, defaults to "!"
p.addCommand("test");
p.addCommand("test2");
p.addCommand("test3");

//Add arguments
p["test2"].addArgument(parser.argument.int("integer")); //Add an int type argument named 'integer'
p["test2"].addArgument("testarg"); // Defaults to the 'word' type argument - gets everything up to a space

p["test3"].addArgument(parser.argument.list("lst", "one", "two", "three");
p["test3"].addArgument(parser.argument.all("all")).setRequired(false);

//Reference the arguments with p["test3"]["lst"] for example

//Create commands with an object

var p2 = new parser.Parser({
  test: { // !test command
    integer: ["int"], //int type argument named 'integer'
    wordarg: ["word"],
    listarg: ["list?", "one", "two", "three"], //Optional list type argument
    allarg: ["all"]
  }
});

Parsing commands

var parsed = p.parse(text);
if (!parsed) return; //Returns null if no matching command found
if (parsed.command == "test") { //If, for example, text == "!test"
  //Matched the 'test' command
}
if (parsed.command == "test2") { //text == "!test2 2 test"
  //Get the argument values
  console.log(parsed.args.integer); //Prints '2'
  console.log(parsed.args.testarg); //Prints 'test'
}
0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago