forked from calculatortamer/pluotsorbet-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.ts
293 lines (285 loc) · 9 KB
/
options.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Option and Argument Management
*
* Options are configuration settings sprinkled throughout the code. They can be grouped into sets of
* options called |OptionSets| which can form a hierarchy of options. For instance:
*
* var set = new OptionSet();
* var opt = set.register(new Option("v", "verbose", "boolean", false, "Enables verbose logging."));
*
* creates an option set with one option in it. The option can be changed directly using |opt.value = true| or
* automatically using the |ArgumentParser|:
*
* var parser = new ArgumentParser();
* parser.addBoundOptionSet(set);
* parser.parse(["-v"]);
*
* The |ArgumentParser| can also be used directly:
*
* var parser = new ArgumentParser();
* argumentParser.addArgument("h", "help", "boolean", {parse: function (x) {
* printUsage();
* }});
*/
module J2ME.Options {
import isObject = J2ME.isObject;
import isNullOrUndefined = J2ME.isNullOrUndefined;
import assert = J2ME.Debug.assert;
export class Argument {
shortName: string;
longName: string;
type: any;
options: any;
positional: boolean;
parseFn: any;
value: any;
constructor(shortName, longName, type, options) {
this.shortName = shortName;
this.longName = longName;
this.type = type;
options = options || {};
this.positional = options.positional;
this.parseFn = options.parse;
this.value = options.defaultValue;
}
public parse(value) {
if (this.type === "boolean") {
release || assert(typeof value === "boolean", "bad value type in Options.parse");
this.value = value;
} else if (this.type === "number") {
release || assert(!isNaN(value), value + " is not a number");
this.value = parseInt(value, 10);
} else {
this.value = value;
}
if (this.parseFn) {
this.parseFn(this.value);
}
}
}
export class ArgumentParser {
args: any [];
constructor() {
this.args = [];
}
public addArgument(shortName, longName, type, options) {
var argument = new Argument(shortName, longName, type, options);
this.args.push(argument);
return argument;
}
public addBoundOption(option) {
var options = {parse: function (x) {
option.value = x;
}};
this.args.push(new Argument(option.shortName, option.longName, option.type, options));
}
public addBoundOptionSet(optionSet) {
var self = this;
optionSet.options.forEach(function (x) {
if (x instanceof OptionSet) {
self.addBoundOptionSet(x);
} else {
release || assert(x instanceof Option, "bad option type in ArgumentParser.addBoundOptionSet");
self.addBoundOption(x);
}
});
}
public getUsage () {
var str = "";
this.args.forEach(function (x) {
if (!x.positional) {
str += "[-" + x.shortName + "|--" + x.longName + (x.type === "boolean" ? "" : " " + x.type[0].toUpperCase()) + "]";
} else {
str += x.longName;
}
str += " ";
});
return str;
}
public parse (args) {
var nonPositionalArgumentMap = {};
var positionalArgumentList = [];
this.args.forEach(function (x) {
if (x.positional) {
positionalArgumentList.push(x);
} else {
nonPositionalArgumentMap["-" + x.shortName] = x;
nonPositionalArgumentMap["--" + x.longName] = x;
}
});
var leftoverArguments = [];
while (args.length) {
var argString = args.shift();
var argument = null, value = argString;
if (argString == '--') {
leftoverArguments = leftoverArguments.concat(args);
break;
} else if (argString.slice(0, 1) == '-' || argString.slice(0, 2) == '--') {
argument = nonPositionalArgumentMap[argString];
// release || assert(argument, "Argument " + argString + " is unknown.");
if (!argument) {
continue;
}
if (argument.type !== "boolean") {
if (argument.type.indexOf("[]") > 0) {
value = [];
while (args.length && args[0][0] != '-') {
value.push(args.shift());
}
} else {
value = args.shift();
}
release || assert(value !== "-" && value !== "--", "Argument " + argString + " must have a value.");
} else {
value = true;
}
} else if (positionalArgumentList.length) {
argument = positionalArgumentList.shift();
} else {
leftoverArguments.push(value);
}
if (argument) {
argument.parse(value);
}
}
release || assert(positionalArgumentList.length === 0, "Missing positional arguments.");
return leftoverArguments;
}
}
export class OptionSet {
name: string;
settings: any;
options: any;
open: boolean = false;
constructor(name: string, settings: any = null) {
this.name = name;
this.settings = settings || {};
this.options = [];
}
public register(option) {
if (option instanceof OptionSet) {
// check for duplicate option sets (bail if found)
for (var i = 0; i < this.options.length; i++) {
var optionSet = this.options[i];
if (optionSet instanceof OptionSet && optionSet.name === option.name) {
return optionSet;
}
}
}
this.options.push(option);
if (this.settings) {
if (option instanceof OptionSet) {
var optionSettings = this.settings[option.name];
if (isObject(optionSettings)) {
option.settings = optionSettings.settings;
option.open = optionSettings.open;
}
} else {
// build_bundle chokes on this:
// if (!isNullOrUndefined(this.settings[option.longName])) {
if (typeof this.settings[option.longName] !== "undefined") {
switch (option.type) {
case "boolean":
option.value = !!this.settings[option.longName];
break;
case "number":
option.value = +this.settings[option.longName];
break;
default:
option.value = this.settings[option.longName];
break;
}
}
}
}
return option;
}
public trace(writer) {
writer.enter(this.name + " {");
this.options.forEach(function (option) {
option.trace(writer);
});
writer.leave("}");
}
public getSettings() {
var settings = {};
this.options.forEach(function(option) {
if (option instanceof OptionSet) {
settings[option.name] = {
settings: option.getSettings(),
open: option.open
};
} else {
settings[option.longName] = option.value;
}
});
return settings;
}
public setSettings(settings: any) {
if (!settings) {
return;
}
this.options.forEach(function (option) {
if (option instanceof OptionSet) {
if (option.name in settings) {
option.setSettings(settings[option.name].settings);
}
} else {
if (option.longName in settings) {
option.value = settings[option.longName];
}
}
});
}
}
export class Option {
longName: string;
shortName: string;
type: string;
defaultValue: any;
value: any;
description: string;
config: any;
/**
* Dat GUI control.
*/
// TODO remove, player will not have access to the DOM
ctrl: any;
// config:
// { range: { min: 1, max: 5, step: 1 } }
// { list: [ "item 1", "item 2", "item 3" ] }
// { choices: { "choice 1": 1, "choice 2": 2, "choice 3": 3 } }
constructor(shortName, longName, type, defaultValue, description, config = null) {
this.longName = longName;
this.shortName = shortName;
this.type = type;
this.defaultValue = defaultValue;
this.value = defaultValue;
this.description = description;
this.config = config;
}
public parse (value) {
this.value = value;
}
public trace (writer) {
writer.writeLn(("-" + this.shortName + "|--" + this.longName).padRight(" ", 30) +
" = " + this.type + " " + this.value + " [" + this.defaultValue + "]" +
" (" + this.description + ")");
}
}
}