Upload packaging folder
[platform/upstream/iotjs.git] / tools / common_js / option_parser.js
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 function Option(arg, value, default_value, help) {
17   this.arg = arg;
18   this.value = value;
19   this.default_value = default_value;
20   this.help = help;
21
22   return this;
23 }
24
25 Option.prototype.printHelp = function() {
26   console.log("\t" + this.arg + "=[" + this.value + "](default: " +
27               this.default_value + ") : " + this.help);
28 }
29
30 function OptionParser() {
31   this.options = [];
32   return this;
33 }
34
35 OptionParser.prototype.addOption = function(arg, value, default_value, help) {
36   var option  = new Option(arg, value, default_value, help);
37   this.options.push(option);
38 }
39
40 OptionParser.prototype.parse = function() {
41   var options = {};
42
43   for (var idx in this.options) {
44     var option = this.options[idx];
45     var default_value = option.default_value;
46     if (default_value !== "") {
47       options[option.arg] = default_value;
48     }
49   }
50
51   for (var aIdx = 2; aIdx < process.argv.length; aIdx++) {
52     var option = process.argv[aIdx];
53     var arg_val = option.split("=");
54
55     if (arg_val.length != 2 || !arg_val[0] || !arg_val[1]) {
56       return null;
57     }
58
59     var arg = arg_val[0];
60     var val = arg_val[1];
61     var found = false;
62
63     for (var oIdx in this.options) {
64       if (arg == this.options[oIdx].arg) {
65         options[arg] = val;
66         found = true;
67         break;
68       }
69     }
70
71     if (!found)
72       return null;
73   }
74
75   return options;
76 }
77
78 OptionParser.prototype.printHelp = function() {
79   console.log(process.argv[1]);
80   console.log("\noptional arguments");
81   for (var idx in this.options) {
82     this.options[idx].printHelp();
83   }
84 }
85
86
87
88
89  module.exports.OptionParser = OptionParser;