Last active
December 12, 2015 08:08
-
-
Save mattbasta/4741486 to your computer and use it in GitHub Desktop.
Now with defaults
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var argv = ['damper.js', '--host', '0.0.0.0', '--port', '8675']; | |
var opts = argv.slice(1); | |
function options(opts, defaults) { | |
var out = defaults || {}, last; | |
for(var i = 0; i < opts.length; i++) { | |
var is_flag = opts[i].substr(0, 1) === '-'; | |
if (is_flag && last) { | |
out[last] = true; | |
} else if (!is_flag && last) { | |
out[last] = opts[i] | |
} | |
last = is_flag ? opts[i] : null; | |
} | |
return out; | |
} | |
var out = options(opts, {'--host': '0.0.0.0', '--port': '8675'}); | |
console.log(out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment