|
| 1 | +/*jshint globalstrict: true*/ |
| 2 | +/*jshint node: true */ |
| 3 | +/*jshint esversion: 6 */ |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +function fullURL(url) { |
| 7 | + if (!url.startsWith("http")) |
| 8 | + return "http://" + url; |
| 9 | + return url; |
| 10 | +} |
| 11 | + |
| 12 | +const cmdOptions = [{ |
| 13 | + name: "help", |
| 14 | + description: "Display usage guide.", |
| 15 | + alias: "h", |
| 16 | + type: Boolean, |
| 17 | + group: "main" |
| 18 | + }, |
| 19 | + { |
| 20 | + name: "debug", |
| 21 | + description: "Set debug mode.", |
| 22 | + alias: "d", |
| 23 | + type: Boolean, |
| 24 | + group: "main" |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "nesting", |
| 28 | + type: Number, |
| 29 | + alias: "n", |
| 30 | + group: "main", |
| 31 | + description: "Depth to which the starting url is to be traversed" |
| 32 | + }, |
| 33 | +/** { |
| 34 | + name: "concurrency", |
| 35 | + type: Number, |
| 36 | + alias: "c", |
| 37 | + group: "main", |
| 38 | + description: "Number of concurrent connections" |
| 39 | + },**/ |
| 40 | + { |
| 41 | + name: "url", |
| 42 | + type: url => fullURL(url), |
| 43 | + alias: "u", |
| 44 | + defaultOption: true, |
| 45 | + group: "main", |
| 46 | + description: "Web url to be traversed" |
| 47 | + } |
| 48 | +]; |
| 49 | + |
| 50 | + |
| 51 | +const sections = [{ |
| 52 | + header: "Webcrawler app", |
| 53 | + content: "Crawls the url provided and downloads page links recursively to the nesting level specified." |
| 54 | + }, |
| 55 | + { |
| 56 | + header: "Main options", |
| 57 | + optionList: cmdOptions, |
| 58 | + group: ["main"] |
| 59 | + }, |
| 60 | + { |
| 61 | + header: "Misc", |
| 62 | + optionList: cmdOptions, |
| 63 | + group: "_none" |
| 64 | + } |
| 65 | +]; |
| 66 | + |
| 67 | +const config = require("config"); |
| 68 | +const commandLineArgs = require("command-line-args"); |
| 69 | +const options = commandLineArgs(cmdOptions); |
| 70 | +const commandLineUsage = require("command-line-usage"); |
| 71 | +const usage = commandLineUsage(sections); |
| 72 | +// The command line options |
| 73 | +module.exports.options = options; |
| 74 | +// The config file options |
| 75 | +module.exports.config = config; |
| 76 | + |
| 77 | +module.exports.usage = usage; |
| 78 | +// return config values with command line values overriding configuration file values |
| 79 | +module.exports.get = function getValue(key, defaultValue) { |
| 80 | + if (key in options._all) { |
| 81 | + return options._all[key]; |
| 82 | + } |
| 83 | + if (config.has(key)) { |
| 84 | + return config.get(key); |
| 85 | + } |
| 86 | + return (defaultValue === undefined) ? null : defaultValue; |
| 87 | +}; |
0 commit comments