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
m (add category)
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
|name=splitArgs
 
|name=splitArgs
 
|author={{user profile|235259|Brian}}
 
|author={{user profile|235259|Brian}}
|version=1.0
+
|version=1.1
|lastmodified=2015-01-08}}
+
|lastmodified=2015-03-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.
 
'''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.
Line 12: Line 12:
  
 
=== Syntax ===
 
=== Syntax ===
<blockquote style="border:1px #0088ee solid;background:#eee;padding:0.5em;width: 50%">
+
{{syntaxbox top|nocat=true}}
 
str.splitArgs([''separator''])
 
str.splitArgs([''separator''])
</blockquote>
+
{{syntaxbox end}}
  
 
==== Parameters ====
 
==== Parameters ====
Line 34: Line 34:
 
});  
 
});  
 
</pre>
 
</pre>
 +
 +
=== Changelog ===
 +
{{changelog version|1.1|2015-03-08|* Update}}
 +
{{changelog version|1.0|2015-01-08|* Release}}
 +
 +
[[Category:API Meta Scripts]]

Latest revision as of 14:16, 4 January 2022

API ScriptAuthor: Brian
Version: 1.1
Last Modified: 2015-03-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.

Contents

[edit] Syntax

str.splitArgs([separator])

[edit] Parameters

separator
Optional. Specifies the character(s) to use for separating the string into arguments. The separator is treated as a string or a regular expression. If separator is omitted, the regular expression /\s/g will be used. (Match one whitespace character.)

[edit] Return Value

splitArgs returns an array of strings, split by the given separator. The separator will not appear in the array, with the exception of separators appearing between single or double quotes; a quoted section will be treated as a single argument, and the outermost quotes will not appear in the returned array.

[edit] Example

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"
}); 

[edit] Changelog

v1.1 (2015-03-08)

  • Update


v1.0 (2015-01-08)

  • Release