• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 class BaseArgumentsProcessor {
6   constructor(args) {
7     this.args_ = args;
8     this.result_ = this.getDefaultResults();
9     console.assert(this.result_ !== undefined)
10     console.assert(this.result_.logFileName !== undefined);
11     this.argsDispatch_ = this.getArgsDispatch();
12     console.assert(this.argsDispatch_ !== undefined);
13   }
14 
15   getDefaultResults() {
16     throw "Implement in getDefaultResults in subclass";
17   }
18 
19   getArgsDispatch() {
20     throw "Implement getArgsDispatch in subclass";
21   }
22 
23   result() { return this.result_ }
24 
25   printUsageAndExit() {
26     print('Cmdline args: [options] [log-file-name]\n' +
27           'Default log file name is "' +
28           this.result_.logFileName + '".\n');
29     print('Options:');
30     for (var arg in this.argsDispatch_) {
31       var synonyms = [arg];
32       var dispatch = this.argsDispatch_[arg];
33       for (var synArg in this.argsDispatch_) {
34         if (arg !== synArg && dispatch === this.argsDispatch_[synArg]) {
35           synonyms.push(synArg);
36           delete this.argsDispatch_[synArg];
37         }
38       }
39       print('  ' + synonyms.join(', ').padEnd(20) + " " + dispatch[2]);
40     }
41     quit(2);
42   }
43 
44   parse() {
45     while (this.args_.length) {
46       var arg = this.args_.shift();
47       if (arg.charAt(0) != '-') {
48         this.result_.logFileName = arg;
49         continue;
50       }
51       var userValue = null;
52       var eqPos = arg.indexOf('=');
53       if (eqPos != -1) {
54         userValue = arg.substr(eqPos + 1);
55         arg = arg.substr(0, eqPos);
56       }
57       if (arg in this.argsDispatch_) {
58         var dispatch = this.argsDispatch_[arg];
59         var property = dispatch[0];
60         var defaultValue = dispatch[1];
61         if (typeof defaultValue == "function") {
62           userValue = defaultValue(userValue);
63         } else if (userValue == null) {
64           userValue = defaultValue;
65         }
66         this.result_[property] = userValue;
67       } else {
68         return false;
69       }
70     }
71     return true;
72   }
73 }
74 
75 function parseBool(str) {
76   if (str == "true" || str == "1") return true;
77   return false;
78 }
79