Last active
December 27, 2015 08:29
-
-
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.
This file contains 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
// 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