2020-09-28 13:50:56 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
export 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() {
|
2021-03-24 12:12:45 +00:00
|
|
|
console.log('Cmdline args: [options] [log-file-name]\n' +
|
2020-09-28 13:50:56 +00:00
|
|
|
'Default log file name is "' +
|
|
|
|
this.result_.logFileName + '".\n');
|
2021-03-24 12:12:45 +00:00
|
|
|
console.log('Options:');
|
2020-11-10 11:47:40 +00:00
|
|
|
for (const arg in this.argsDispatch_) {
|
|
|
|
const synonyms = [arg];
|
|
|
|
const dispatch = this.argsDispatch_[arg];
|
|
|
|
for (const synArg in this.argsDispatch_) {
|
2020-09-28 13:50:56 +00:00
|
|
|
if (arg !== synArg && dispatch === this.argsDispatch_[synArg]) {
|
|
|
|
synonyms.push(synArg);
|
|
|
|
delete this.argsDispatch_[synArg];
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 12:12:45 +00:00
|
|
|
console.log(` ${synonyms.join(', ').padEnd(20)} ${dispatch[2]}`);
|
2020-09-28 13:50:56 +00:00
|
|
|
}
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
parse() {
|
|
|
|
while (this.args_.length) {
|
2020-11-10 11:47:40 +00:00
|
|
|
let arg = this.args_.shift();
|
2020-09-28 13:50:56 +00:00
|
|
|
if (arg.charAt(0) != '-') {
|
|
|
|
this.result_.logFileName = arg;
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-10 11:47:40 +00:00
|
|
|
let userValue = null;
|
|
|
|
const eqPos = arg.indexOf('=');
|
2020-09-28 13:50:56 +00:00
|
|
|
if (eqPos != -1) {
|
|
|
|
userValue = arg.substr(eqPos + 1);
|
|
|
|
arg = arg.substr(0, eqPos);
|
|
|
|
}
|
|
|
|
if (arg in this.argsDispatch_) {
|
2020-11-10 11:47:40 +00:00
|
|
|
const dispatch = this.argsDispatch_[arg];
|
|
|
|
const property = dispatch[0];
|
|
|
|
const defaultValue = dispatch[1];
|
2020-09-28 13:50:56 +00:00
|
|
|
if (typeof defaultValue == "function") {
|
|
|
|
userValue = defaultValue(userValue);
|
|
|
|
} else if (userValue == null) {
|
|
|
|
userValue = defaultValue;
|
|
|
|
}
|
|
|
|
this.result_[property] = userValue;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseBool(str) {
|
|
|
|
if (str == "true" || str == "1") return true;
|
|
|
|
return false;
|
|
|
|
}
|