Comparing version 0.2.0 to 0.2.1
75
index.js
@@ -69,4 +69,3 @@ var path = require('path'); | ||
Object.keys(x).forEach(function (key) { | ||
aliases[key] = x[key]; | ||
aliases[x[key]] = key; | ||
self.alias(key, x[key]); | ||
}); | ||
@@ -76,8 +75,9 @@ } | ||
y.forEach(function (yy) { | ||
self.alias(x, y); | ||
self.alias(x, yy); | ||
}); | ||
} | ||
else { | ||
aliases[x] = y; | ||
aliases[y] = x; | ||
var zs = (aliases[x] || []).concat(aliases[y] || []).concat(x, y); | ||
aliases[x] = zs.filter(function (z) { return z != x }); | ||
aliases[y] = zs.filter(function (z) { return z != y }); | ||
} | ||
@@ -196,8 +196,2 @@ | ||
self.help = function () { | ||
var help = []; | ||
if (usage) { | ||
help.push(usage.replace(/\$0/g, self.$0), ''); | ||
} | ||
var keys = Object.keys( | ||
@@ -213,4 +207,10 @@ Object.keys(descriptions) | ||
keys.forEach(function (key) { | ||
var switches = [ key ].concat(aliases[key] || []) | ||
var help = keys.length ? [ 'Options:' ] : []; | ||
if (usage) { | ||
help.unshift(usage.replace(/\$0/g, self.$0), ''); | ||
} | ||
var switches = keys.reduce(function (acc, key) { | ||
acc[key] = [ key ].concat(aliases[key] || []) | ||
.map(function (sw) { | ||
@@ -221,7 +221,32 @@ return (sw.length > 1 ? '--' : '-') + sw | ||
; | ||
return acc; | ||
}, {}); | ||
var switchlen = longest(Object.keys(switches).map(function (s) { | ||
return switches[s] || ''; | ||
})); | ||
var desclen = longest(Object.keys(descriptions).map(function (d) { | ||
return descriptions[d] || ''; | ||
})); | ||
keys.forEach(function (key) { | ||
var kswitch = switches[key]; | ||
var desc = descriptions[key] || ''; | ||
var dpadding = new Array( | ||
Math.max(desclen - desc.length + 1, 0) | ||
).join(' '); | ||
var spadding = new Array( | ||
Math.max(switchlen - kswitch.length + 3, 0) | ||
).join(' '); | ||
var type = null; | ||
if (flags.bools[key]) type = '[boolean]'; | ||
if (flags.strings[key]) type = '[string]'; | ||
if (dpadding.length > 0) desc += dpadding; | ||
help.push(' ' + switches + ' ' + [ | ||
var prelude = ' ' + kswitch + spadding; | ||
var body = [ | ||
desc, | ||
type, | ||
@@ -236,9 +261,8 @@ demanded[key] | ||
, | ||
].filter(Boolean).join(' ')); | ||
].filter(Boolean).join(' '); | ||
var desc = descriptions[key]; | ||
if (desc) help.push(' ' + desc); | ||
help.push(''); | ||
help.push(prelude + body); | ||
}); | ||
help.push(''); | ||
return help.join('\n'); | ||
@@ -273,5 +297,5 @@ }; | ||
if (aliases[key]) { | ||
argv[aliases[key]] = argv[key]; | ||
} | ||
(aliases[key] || []).forEach(function (x) { | ||
argv[x] = argv[key]; | ||
}); | ||
} | ||
@@ -375,2 +399,9 @@ | ||
function longest (xs) { | ||
return Math.max.apply( | ||
null, | ||
xs.map(function (x) { return x.length }) | ||
); | ||
} | ||
return self; | ||
@@ -393,2 +424,2 @@ }; | ||
return p.match(/^[.\/]/) ? p : './' + p; | ||
} | ||
}; |
{ | ||
"name" : "optimist", | ||
"version" : "0.2.0", | ||
"version" : "0.2.1", | ||
"description" : "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
@@ -5,0 +5,0 @@ "main" : "./index.js", |
@@ -220,1 +220,12 @@ var optimist = require('optimist'); | ||
}; | ||
exports.multiAlias = function () { | ||
var argv = optimist([ '-f', '11', '--zoom', '55' ]) | ||
.alias('z', [ 'zm', 'zoom' ]) | ||
.argv | ||
; | ||
assert.equal(argv.zoom, 55); | ||
assert.equal(argv.z, argv.zoom); | ||
assert.equal(argv.z, argv.zm); | ||
assert.equal(argv.f, 11); | ||
}; |
@@ -21,2 +21,3 @@ var Hash = require('hashish'); | ||
'Usage: ./usage -x NUM -y NUM', | ||
'Options:', | ||
' -x [required]', | ||
@@ -23,0 +24,0 @@ ' -y [required]', |
Sorry, the diff of this file is not supported yet
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
37003
26
990
7