-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from eveningkid/fix-args-separation-regex
fix: args separation regex
- Loading branch information
Showing
3 changed files
with
38 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
"use strict"; | ||
'use strict'; | ||
|
||
/* | ||
Straight-forward node.js arguments parser | ||
Author: eveningkid | ||
License: Apache-2.0 | ||
*/ | ||
|
||
function Parse (argv) { | ||
// Removing node/bin and called script name | ||
argv = argv.slice(2); | ||
|
||
// Returned object | ||
var args = {}; | ||
var argName, argValue; | ||
|
||
// For each argument | ||
argv.forEach(function (arg, index) { | ||
// Seperate argument, for a key/value return | ||
arg = arg.split('='); | ||
|
||
// 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; | ||
|
||
// Finally add the argument to the args set | ||
args[argName] = argValue; | ||
}); | ||
|
||
return args; | ||
const ARGUMENT_SEPARATION_REGEX = /([^=\s]+)=?([^\s]*)/; | ||
|
||
function Parse(argv) { | ||
// Removing node/bin and called script name | ||
argv = argv.slice(2); | ||
|
||
const parsedArgs = {}; | ||
let argName, argValue; | ||
|
||
argv.forEach(function (arg) { | ||
// Separate argument for a key/value return | ||
arg = arg.match(ARGUMENT_SEPARATION_REGEX); | ||
arg.splice(0, 1); | ||
|
||
// Retrieve the argument name | ||
argName = arg[0]; | ||
|
||
// 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; |