Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

small refactor

  • Loading branch information...
commit 23c19b19551d3ca619abaa3b1c191c0f33806cf1 1 parent b1bdf3b
@kof authored
Showing with 10 additions and 13 deletions.
  1. +10 −13 lib/argsparser.js
View
23 lib/argsparser.js
@@ -8,17 +8,15 @@ exports.parse = function(args) {
// args is optional, default is process.argv
args = args || process.argv;
- var opts = {},
- curSwitch;
+ var opts = {}, curSwitch;
args.forEach(function(arg) {
- var curValType = typeof opts[curSwitch];
// its a switch
- if (/^(-|--)/.test(arg)) {
+ if (/^(-|--)/.test(arg) || !curSwitch) {
opts[arg] = true;
curSwitch = arg;
- // this arg is some data
- } else if (curSwitch) {
+ // this arg is a data
+ } else {
if (arg === 'false') {
arg = false;
} else if (arg === 'true') {
@@ -27,16 +25,15 @@ exports.parse = function(args) {
arg = Number(arg);
}
- if (curValType === 'boolean') {
+ // it was a boolean switch per default,
+ // now it has got a val
+ if (typeof opts[curSwitch] === 'boolean') {
opts[curSwitch] = arg;
- } else if (curValType === 'string' || curValType === 'number') {
- opts[curSwitch] = [opts[curSwitch], arg];
- } else {
+ } else if (Array.isArray(opts[curSwitch])) {
opts[curSwitch].push(arg);
+ } else {
+ opts[curSwitch] = [opts[curSwitch], arg];
}
- } else {
- opts[arg] = true;
- curSwitch = arg;
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.