Skip to content

Instantly share code, notes, and snippets.

@peterbraden
Created December 1, 2010 20:52
Show Gist options
  • Save peterbraden/724202 to your computer and use it in GitHub Desktop.
Save peterbraden/724202 to your computer and use it in GitHub Desktop.
Option Parsing for node.js scripts
/*
* returns [{opts}, [args]]
*/
var optParse = function(argv){
var opts = {}
, args = []
for(var i in argv){
var x = argv[i]
if (x[0] == '-'){
// either flag, in which case store true, or opts
if (x.indexOf('=') == -1){
opts[x] = true
} else {
var flag = x.slice(0, x.indexOf('='))
, val = x.slice(x.indexOf('=')+1);
opts[flag] = val;
}
} else {
args.push(x)
}
}
return [opts, args]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment