Command line arguments
Posted: Wed Mar 18, 2015 5:22 pm
Giorgio, I think the regexp in FlashGotDMCust.makeArgs is broken.
From what I managed to understand from the commentless :P code, it was supposed to allow the user to specify an alternative/default value for a placeholder that has no value and capture it in m[4]. The problem is, m[4] is always empty thanks to its lazy (not greedy) "\S*?" followed by another \S* (both patterns match the same input and the first pattern can (allowed to) match nothing, so the parser can always move on to the second pattern), so m[5] contains everything after the placeholder name, so the alternative/default value is never used because the code looks for it in m[4].
This regexp does what I think it was supposed to do - m[4] captures a non-whitespace sequence after the placeholder name, and m[5] captures everything after it:
I just replaced (\S*?) with (\S*) for m[4].
Here's my test results using "Z" as the only placeholder, re1 as the old (current) regexp, and re2 as the new regexp.
Output:
Code: Select all
var rx = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + this.PLACEHOLDERS.join("|") + ")\\b(\\S*?)([\\s\\S]*?)\\]");
This regexp does what I think it was supposed to do - m[4] captures a non-whitespace sequence after the placeholder name, and m[5] captures everything after it:
Code: Select all
var rx = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + this.PLACEHOLDERS.join("|") + ")\\b(\\S*)([\\s\\S]*?)\\]");
Here's my test results using "Z" as the only placeholder, re1 as the old (current) regexp, and re2 as the new regexp.
Code: Select all
var re1 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z" + ")\\b(\\S*?)([\\s\\S]*?)\\]");
var re2 = new RegExp("\\[([\\s\\S]*?)(\\S*)\\b(" + "Z" + ")\\b(\\S*)([\\s\\S]*?)\\]");
var a = ["[]", "[Z]", "[Z|]", "[Z |]", "[Z| ]", "[Z|aaa]", "[Z |aaa]", "[Z| aaa]", "[Z|aaa bbb]", "[Z |aaa bbb]", "[Z| aaa bbb]"];
function format_match(name, m){return m == null ? name + "=" + m : m.map(function(o,i){return name + "[" + i + "]: [" + typeof(o) + "]: '" + o + "'";}).join("\n");}
for (var i = 0; i < a.length; ++i) {
var s = a[i], m1 = s.match(re1), m2 = s.match(re2);
console.log("s: '" + s + "'\n" + format_match("m1", m1) + "\n\n" + format_match("m2", m2));
}
Code: Select all
=====
s: '[]'
m1=null
m2=null
=====
s: '[Z]'
m1[0]: [string]: '[Z]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ''
m2[0]: [string]: '[Z]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ''
=====
s: '[Z|]'
m1[0]: [string]: '[Z|]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|'
m2[0]: [string]: '[Z|]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ''
=====
s: '[Z |]'
m1[0]: [string]: '[Z |]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |'
m2[0]: [string]: '[Z |]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |'
=====
s: '[Z| ]'
m1[0]: [string]: '[Z| ]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| '
m2[0]: [string]: '[Z| ]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' '
=====
s: '[Z|aaa]'
m1[0]: [string]: '[Z|aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa'
m2[0]: [string]: '[Z|aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa'
m2[5]: [string]: ''
=====
s: '[Z |aaa]'
m1[0]: [string]: '[Z |aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa'
m2[0]: [string]: '[Z |aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa'
=====
s: '[Z| aaa]'
m1[0]: [string]: '[Z| aaa]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa'
m2[0]: [string]: '[Z| aaa]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa'
=====
s: '[Z|aaa bbb]'
m1[0]: [string]: '[Z|aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '|aaa bbb'
m2[0]: [string]: '[Z|aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|aaa'
m2[5]: [string]: ' bbb'
=====
s: '[Z |aaa bbb]'
m1[0]: [string]: '[Z |aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: ' |aaa bbb'
m2[0]: [string]: '[Z |aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: ''
m2[5]: [string]: ' |aaa bbb'
=====
s: '[Z| aaa bbb]'
m1[0]: [string]: '[Z| aaa bbb]'
m1[1]: [string]: ''
m1[2]: [string]: ''
m1[3]: [string]: 'Z'
m1[4]: [string]: ''
m1[5]: [string]: '| aaa bbb'
m2[0]: [string]: '[Z| aaa bbb]'
m2[1]: [string]: ''
m2[2]: [string]: ''
m2[3]: [string]: 'Z'
m2[4]: [string]: '|'
m2[5]: [string]: ' aaa bbb'