Skip to content

Instantly share code, notes, and snippets.

@gkoehler
Last active December 27, 2015 08:29
Show Gist options
  • Save gkoehler/7296205 to your computer and use it in GitHub Desktop.
Save gkoehler/7296205 to your computer and use it in GitHub Desktop.
Very simple way to normalize arguments, just like a regular JS function would receive. Useful for Node.js shell scripts.
// very simple way to normalize arguments, just like a regular JS function would receive
// so, this:
// script.js -arg1=argval -arg2=newargval -arg3
// would translate to:
// {arg1:argval, arg2:newargval, arg3:undefined}
// (there are also other parameters Node adds)
// If the same argument is used twice with different values,
// their values are comma-delimited.
// so, this:
// script.js -file=filename1.txt -file=filename2.txt
// would translate to:
// {file:'filename1.txt,filename2.txt'}
// see: https://gist.github.com/gkoehler/7296205
var args = {};
process.argv.forEach(function(arg) {
if (args[arg.match(/[\w\.]+/g)[0]] == null)
args[arg.match(/[\w\.]+/g)[0]] = arg.match(/[\w\.]+/g)[1];
else {
args[arg.match(/[\w\.]+/g)[0]] += ',' + arg.match(/[\w\.]+/g)[1];
}
});
//console.log(args);
run(args); // here is where you run your main function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment