Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Script:splitArgs"

From Roll20 Wiki

Jump to: navigation, search
(Created page with "{{stub}}")
 
Line 1: Line 1:
{{stub}}
+
{{script overview
 +
|name=splitArgs
 +
|author={{user profile|235259|Brian}}
 +
|version=1.0
 +
|lastmodified=2015-01-08}}
 +
 
 +
'''splitArgs''' is designed to assist other scripts in handling user input. In particular, the common practice of creating an API command followed by a series of parameters means that the single string represented by <code>msg.content</code> needs to be split into multiple parts. The naive and most common means of doing this is simply using <code>msg.content.split(' ')</code>, however this solution does not allow the programmer to account for quoted parameters which contain spaces.
 +
 
 +
The <code>splitArgs</code> function is added to <code>String.prototype</code>, meaning it can be treated as a function of string objects.
 +
 
 +
Original credit for the algorithm goes to [https://github.com/elgs/splitargs Elgs Qian Chen].
 +
<br clear="all">
 +
 
 +
=== Example Use ===
 +
<pre data-language="javascript">
 +
on('chat:message', function(msg) {
 +
    var params = msg.content.splitArgs(),
 +
        command = params.shift().substring(1);
 +
   
 +
    // msg.content === "!command with parameters, \"including 'with quotes'\""
 +
    // params === ["with", "parameters,", "including 'with quotes'"]
 +
    // command === "command"
 +
});
 +
</pre>

Revision as of 23:12, 9 January 2015

API ScriptAuthor: Brian
Version: 1.0
Last Modified: 2015-01-08
Code: splitArgs
Dependencies: None
Conflicts: None

splitArgs is designed to assist other scripts in handling user input. In particular, the common practice of creating an API command followed by a series of parameters means that the single string represented by msg.content needs to be split into multiple parts. The naive and most common means of doing this is simply using msg.content.split(' '), however this solution does not allow the programmer to account for quoted parameters which contain spaces.

The splitArgs function is added to String.prototype, meaning it can be treated as a function of string objects.

Original credit for the algorithm goes to Elgs Qian Chen.

Example Use

on('chat:message', function(msg) {
    var params = msg.content.splitArgs(),
        command = params.shift().substring(1);
    
    // msg.content === "!command with parameters, \"including 'with quotes'\""
    // params === ["with", "parameters,", "including 'with quotes'"]
    // command === "command"
});