()
| 103 | } |
| 104 | |
| 105 | function commandParser() { |
| 106 | var parsedCmd = { |
| 107 | "cmd": "parse", |
| 108 | "options": {}, |
| 109 | "inputStream": process.stdin |
| 110 | }; |
| 111 | |
| 112 | function parseObject(val, optional) { |
| 113 | try { |
| 114 | return JSON.parse(val); |
| 115 | } catch (e) { |
| 116 | if (optional) { |
| 117 | return val; |
| 118 | } else { |
| 119 | console.error(e); |
| 120 | process.exit(1); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | function parseBool(str, optName) { |
| 126 | str = str.toLowerCase(); |
| 127 | if (str === "true" || str === "y") { |
| 128 | return true; |
| 129 | } else if (str === "false" || str === "n") { |
| 130 | return false; |
| 131 | } |
| 132 | console.log("Unknown boolean value %s for parameter %s.", str, optName); |
| 133 | _showHelp(1); |
| 134 | } |
| 135 | process.argv.slice(2).forEach(function (item) { |
| 136 | if (item.indexOf("--") > -1) { |
| 137 | var itemArr = item.split("="); |
| 138 | var optName = itemArr[0]; |
| 139 | var key, val, type; |
| 140 | if (!opts[optName]) { |
| 141 | console.log("Option %s not supported.", optName); |
| 142 | _showHelp(1); |
| 143 | } |
| 144 | key = optName.replace('--', ''); |
| 145 | val = itemArr[1] || ''; |
| 146 | type = opts[optName].type; |
| 147 | if (type === "string") { |
| 148 | parsedCmd.options[key] = val.toString(); |
| 149 | } else if (type === "boolean") { |
| 150 | parsedCmd.options[key] = parseBool(val, optName); |
| 151 | } else if (type === "number") { |
| 152 | parsedCmd.options[key] = parseFloat(val); |
| 153 | } else if (type === "object") { |
| 154 | parsedCmd.options[key] = parseObject(val, false); |
| 155 | } else if (type === "~object") { |
| 156 | parsedCmd.options[key] = parseObject(val, true); |
| 157 | } else { |
| 158 | throw ({ |
| 159 | name: "UnimplementedException", |
| 160 | message: "Option type parsing not implemented. See bin/options.json" |
| 161 | }); |
| 162 | } |
no test coverage detected