Parses Unix (-pewpew
, --pew-pew=value
, --pewpew value
) and Dos style (/x
, /x value
) argv strings
Input
"node server.js -h 0.0.0.0 -p 80 -s -a --vvvv --r-x /v 123 /x --debug-enabled=yes"
Output
{
"h": "0.0.0.0",
"p": "80",
"s": true,
"a": true,
"vvvv": true,
"r-x": true,
"v": 123,
"x": true,
"debug-enabled": "yes"
}
Demo: http://jsfiddle.net/p3mc9/3/
Thx @maettig for help
This parser can have a strange behavior if you pass full path names e.g. -location /home/user/path/to/file.txt
it thinks that /home
is dos-style argument name... Fix it if your file accepts paths - \B[\/-]
-> \B-
One byte less:
function(a,b,c,d){d=c={};for(a=a.split(/\s*\B[\/-]+([\w-]+)[\s=]*/);b=a[d|=1];c[b]=a[++d]||!0);return c}
Update:
function(a){for(var b=a.split(/\s*\B[\/-]+([\w-]+)[\s=]*/),c={},d;a=b[d|=1];c[a]=b[++d]||!0);return c}