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