mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
4c4af643e5
PR-URL: https://github.com/nodejs/node/pull/17489 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
79 lines
2.2 KiB
JavaScript
79 lines
2.2 KiB
JavaScript
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
class BaseArgumentsProcessor {
|
|
constructor(args) {
|
|
this.args_ = args;
|
|
this.result_ = this.getDefaultResults();
|
|
console.assert(this.result_ !== undefined)
|
|
console.assert(this.result_.logFileName !== undefined);
|
|
this.argsDispatch_ = this.getArgsDispatch();
|
|
console.assert(this.argsDispatch_ !== undefined);
|
|
}
|
|
|
|
getDefaultResults() {
|
|
throw "Implement in getDefaultResults in subclass";
|
|
}
|
|
|
|
getArgsDispatch() {
|
|
throw "Implement getArgsDispatch in subclass";
|
|
}
|
|
|
|
result() { return this.result_ }
|
|
|
|
printUsageAndExit() {
|
|
print('Cmdline args: [options] [log-file-name]\n' +
|
|
'Default log file name is "' +
|
|
this.result_.logFileName + '".\n');
|
|
print('Options:');
|
|
for (var arg in this.argsDispatch_) {
|
|
var synonyms = [arg];
|
|
var dispatch = this.argsDispatch_[arg];
|
|
for (var synArg in this.argsDispatch_) {
|
|
if (arg !== synArg && dispatch === this.argsDispatch_[synArg]) {
|
|
synonyms.push(synArg);
|
|
delete this.argsDispatch_[synArg];
|
|
}
|
|
}
|
|
print(' ' + synonyms.join(', ').padEnd(20) + " " + dispatch[2]);
|
|
}
|
|
quit(2);
|
|
}
|
|
|
|
parse() {
|
|
while (this.args_.length) {
|
|
var arg = this.args_.shift();
|
|
if (arg.charAt(0) != '-') {
|
|
this.result_.logFileName = arg;
|
|
continue;
|
|
}
|
|
var userValue = null;
|
|
var eqPos = arg.indexOf('=');
|
|
if (eqPos != -1) {
|
|
userValue = arg.substr(eqPos + 1);
|
|
arg = arg.substr(0, eqPos);
|
|
}
|
|
if (arg in this.argsDispatch_) {
|
|
var dispatch = this.argsDispatch_[arg];
|
|
var property = dispatch[0];
|
|
var defaultValue = dispatch[1];
|
|
if (typeof defaultValue == "function") {
|
|
userValue = defaultValue(userValue);
|
|
} else if (userValue == null) {
|
|
userValue = defaultValue;
|
|
}
|
|
this.result_[property] = userValue;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
function parseBool(str) {
|
|
if (str == "true" || str == "1") return true;
|
|
return false;
|
|
}
|