args-parser
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,6 +0,1 @@ | ||
/* | ||
Straight-forward node.js arguments parser | ||
Author: eveningkid | ||
License: Apache-2.0 | ||
*/ | ||
"use strict";function Parse(a){a=a.slice(2);var b,c,d={};return a.forEach(function(a,e){a=a.split("="),b=a[0],0===b.indexOf("-")&&(b=b.slice(b.slice(0,2).lastIndexOf("-")+1)),c=2===a.length?a[1]:!0,d[b]=c}),d}module.exports=Parse; | ||
"use strict";const ARGUMENT_SEPARATION_REGEX=/([^=\s]+)=?([^\s]*)/;function Parse(s){s=s.slice(2);const e={};let t,c;return s.forEach(function(s){(s=s.match(ARGUMENT_SEPARATION_REGEX)).splice(0,1),0===(t=s[0]).indexOf("-")&&(t=t.slice(t.slice(0,2).lastIndexOf("-")+1)),c=""===s[1]||(parseFloat(s[1]).toString()===s[1]?+s[1]:s[1]),e[t]=c}),e}module.exports=Parse; |
{ | ||
"name": "args-parser", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Straight-forward node.js arguments parser", | ||
"main": "parse.js", | ||
"main": "dist/parse.min.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
60
parse.js
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -9,38 +9,38 @@ /* | ||
function Parse (argv) { | ||
// Removing node/bin and called script name | ||
argv = argv.slice(2); | ||
const ARGUMENT_SEPARATION_REGEX = /([^=\s]+)=?([^\s]*)/; | ||
// Returned object | ||
var args = {}; | ||
var argName, argValue; | ||
function Parse(argv) { | ||
// Removing node/bin and called script name | ||
argv = argv.slice(2); | ||
// For each argument | ||
argv.forEach(function (arg, index) { | ||
// Seperate argument, for a key/value return | ||
arg = arg.split('='); | ||
const parsedArgs = {}; | ||
let argName, argValue; | ||
// Retrieve the argument name | ||
argName = arg[0]; | ||
// Remove "--" or "-" | ||
if (argName.indexOf('-') === 0) { | ||
argName = argName.slice(argName.slice(0, 2).lastIndexOf('-') + 1); | ||
} | ||
// Associate defined value or initialize it to "true" state | ||
argValue = (arg.length === 2) | ||
? parseFloat(arg[1]).toString() === arg[1] // check if argument is valid number | ||
? +arg[1] | ||
: arg[1] | ||
: true; | ||
argv.forEach(function (arg) { | ||
// Separate argument for a key/value return | ||
arg = arg.match(ARGUMENT_SEPARATION_REGEX); | ||
arg.splice(0, 1); | ||
// Finally add the argument to the args set | ||
args[argName] = argValue; | ||
}); | ||
// Retrieve the argument name | ||
argName = arg[0]; | ||
return args; | ||
// Remove "--" or "-" | ||
if (argName.indexOf('-') === 0) { | ||
argName = argName.slice(argName.slice(0, 2).lastIndexOf('-') + 1); | ||
} | ||
// Parse argument value or set it to `true` if empty | ||
argValue = | ||
arg[1] !== '' | ||
? parseFloat(arg[1]).toString() === arg[1] | ||
? +arg[1] | ||
: arg[1] | ||
: true; | ||
parsedArgs[argName] = argValue; | ||
}); | ||
return parsedArgs; | ||
} | ||
module.exports = Parse; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13961
6
36