Spaces:
Sleeping
Sleeping
File size: 2,959 Bytes
4cadbaf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
/** @namespace */
Opt = {
/**
* Get commandline option values.
* @param {Array} args Commandline arguments. Like ["-a=xml", "-b", "--class=new", "--debug"]
* @param {object} optNames Map short names to long names. Like {a:"accept", b:"backtrace", c:"class", d:"debug"}.
* @return {object} Short names and values. Like {a:"xml", b:true, c:"new", d:true}
*/
get: function(args, optNames) {
var opt = {"_": []}; // the unnamed option allows multiple values
for (var i = 0; i < args.length; i++) {
var arg = new String(args[i]);
var name;
var value;
if (arg.charAt(0) == "-") {
if (arg.charAt(1) == "-") { // it's a longname like --foo
arg = arg.substring(2);
var m = arg.split("=");
name = m.shift();
value = m.shift();
if (typeof value == "undefined") value = true;
for (var n in optNames) { // convert it to a shortname
if (name == optNames[n]) {
name = n;
}
}
}
else { // it's a shortname like -f
arg = arg.substring(1);
var m = arg.split("=");
name = m.shift();
value = m.shift();
if (typeof value == "undefined") value = true;
for (var n in optNames) { // find the matching key
if (name == n || name+'[]' == n) {
name = n;
break;
}
}
}
if (name.match(/(.+)\[\]$/)) { // it's an array type like n[]
name = RegExp.$1;
if (!opt[name]) opt[name] = [];
}
if (opt[name] && opt[name].push) {
opt[name].push(value);
}
else {
opt[name] = value;
}
}
else { // not associated with any optname
opt._.push(args[i]);
}
}
return opt;
}
}
/*t:
plan(11, "Testing Opt.");
is(
typeof Opt,
"object",
"Opt is an object."
);
is(
typeof Opt.get,
"function",
"Opt.get is a function."
);
var optNames = {a:"accept", b:"backtrace", c:"class", d:"debug", "e[]":"exceptions"};
var t_options = Opt.get(["-a=xml", "-b", "--class=new", "--debug", "-e=one", "-e=two", "foo", "bar"], optNames);
is(
t_options.a,
"xml",
"an option defined with a short name can be accessed by its short name."
);
is(
t_options.b,
true,
"an option defined with a short name and no value are true."
);
is(
t_options.c,
"new",
"an option defined with a long name can be accessed by its short name."
);
is(
t_options.d,
true,
"an option defined with a long name and no value are true."
);
is(
typeof t_options.e,
"object",
"an option that can accept multiple values is defined."
);
is(
t_options.e.length,
2,
"an option that can accept multiple values can have more than one value."
);
is(
t_options.e[1],
"two",
"an option that can accept multiple values can be accessed as an array."
);
is(
typeof t_options._,
"object",
"the property '_' is defined for unnamed options."
);
is(
t_options._[0],
"foo",
"the property '_' can be accessed as an array."
);
*/ |