Comparing version 3.7.2 to 3.8.0
## Change Log | ||
### v3.8.0 (2015/04/24 23:10 +00:00) | ||
- [#154](https://github.com/bcoe/yargs/pull/154) showHelp's method signature was misleading fixes #153 (@bcoe) | ||
- [#151](https://github.com/bcoe/yargs/pull/151) refactor yargs' table layout logic to use new helper library (@bcoe) | ||
- [#150](https://github.com/bcoe/yargs/pull/150) Fix README example in argument requirements (@annonymouse) | ||
### v3.7.2 (2015/04/13 11:52 -07:00) | ||
@@ -4,0 +9,0 @@ |
@@ -290,5 +290,5 @@ var assert = require('assert'), | ||
self.showHelp = function (fn) { | ||
self.showHelp = function (level) { | ||
if (!self.parsed) parseArgs(processArgs) // run parser, if it has not already been executed. | ||
usage.showHelp(fn) | ||
usage.showHelp(level) | ||
return self | ||
@@ -295,0 +295,0 @@ } |
261
lib/usage.js
// this file handles outputting usage instructions, | ||
// failures, etc. keeps logging in one place. | ||
var decamelize = require('decamelize'), | ||
wordwrap = require('wordwrap'), | ||
var cliui = require('cliui'), | ||
decamelize = require('decamelize'), | ||
wsize = require('window-size') | ||
@@ -97,14 +97,22 @@ | ||
var demanded = yargs.getDemanded(), | ||
options = yargs.getOptions(), | ||
keys = Object.keys( | ||
Object.keys(descriptions) | ||
.concat(Object.keys(demanded)) | ||
.concat(Object.keys(options.default)) | ||
.reduce(function (acc, key) { | ||
if (key !== '_') acc[key] = true | ||
return acc | ||
}, {}) | ||
) | ||
options = yargs.getOptions(), | ||
keys = Object.keys( | ||
Object.keys(descriptions) | ||
.concat(Object.keys(demanded)) | ||
.concat(Object.keys(options.default)) | ||
.reduce(function (acc, key) { | ||
if (key !== '_') acc[key] = true | ||
return acc | ||
}, {}) | ||
), | ||
ui = cliui({ | ||
width: wrap, | ||
wrap: wrap ? true : false | ||
}) | ||
var help = keys.length ? [ 'Options:' ] : [] | ||
// the usage string. | ||
if (usage) { | ||
var u = usage.replace(/\$0/g, yargs.$0) | ||
ui.div(u + '\n') | ||
} | ||
@@ -114,22 +122,14 @@ // your application's commands, i.e., non-option | ||
if (commands.length) { | ||
help.unshift('') | ||
ui.div('Commands:') | ||
var commandsTable = {} | ||
commands.forEach(function (command) { | ||
commandsTable[command[0]] = { | ||
desc: command[1], | ||
extra: '' | ||
} | ||
ui.div( | ||
{text: command[0], padding: [0, 2, 0, 2], width: maxWidth(commands) + 4}, | ||
{text: command[1]} | ||
) | ||
}) | ||
help = ['Commands:'].concat(formatTable(commandsTable, 5), help) | ||
ui.div() | ||
} | ||
// the usage string. | ||
if (usage) { | ||
var u = usage.replace(/\$0/g, yargs.$0) | ||
if (wrap) u = wordwrap(0, wrap)(u) | ||
help.unshift(u, '') | ||
} | ||
// the options table. | ||
@@ -155,31 +155,38 @@ var aliasKeys = (Object.keys(options.alias) || []) | ||
var switchTable = {} | ||
keys.forEach(function (key) { | ||
var kswitch = switches[key] | ||
var desc = descriptions[key] || '' | ||
var type = null | ||
if (keys.length) { | ||
ui.div('Options:') | ||
if (~options.boolean.indexOf(key)) type = '[boolean]' | ||
if (~options.count.indexOf(key)) type = '[count]' | ||
if (~options.string.indexOf(key)) type = '[string]' | ||
if (~options.normalize.indexOf(key)) type = '[string]' | ||
if (~options.array.indexOf(key)) type = '[array]' | ||
keys.forEach(function (key) { | ||
var kswitch = switches[key] | ||
var desc = descriptions[key] || '' | ||
var type = null | ||
var extra = [ | ||
type, | ||
demanded[key] ? '[required]' : null, | ||
defaultString(options.default[key], options.defaultDescription[key]) | ||
].filter(Boolean).join(' ') | ||
if (~options.boolean.indexOf(key)) type = '[boolean]' | ||
if (~options.count.indexOf(key)) type = '[count]' | ||
if (~options.string.indexOf(key)) type = '[string]' | ||
if (~options.normalize.indexOf(key)) type = '[string]' | ||
if (~options.array.indexOf(key)) type = '[array]' | ||
switchTable[kswitch] = { | ||
desc: desc, | ||
extra: extra | ||
} | ||
}) | ||
help.push.apply(help, formatTable(switchTable, 3)) | ||
var extra = [ | ||
type, | ||
demanded[key] ? '[required]' : null, | ||
defaultString(options.default[key], options.defaultDescription[key]) | ||
].filter(Boolean).join(' ') | ||
if (keys.length) help.push('') | ||
ui.span( | ||
{text: kswitch, padding: [0, 2, 0, 2], width: maxWidth(switches) + 4}, | ||
desc | ||
) | ||
if (extra) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'}) | ||
else ui.div() | ||
}) | ||
ui.div() | ||
} | ||
// describe some common use-cases for your application. | ||
if (examples.length) { | ||
ui.div('Examples:') | ||
examples.forEach(function (example) { | ||
@@ -189,11 +196,10 @@ example[0] = example[0].replace(/\$0/g, yargs.$0) | ||
var examplesTable = {} | ||
examples.forEach(function (example) { | ||
examplesTable[example[0]] = { | ||
desc: example[1], | ||
extra: '' | ||
} | ||
ui.div( | ||
{text: example[0], padding: [0, 2, 0, 2], width: maxWidth(examples) + 4}, | ||
example[1] | ||
) | ||
}) | ||
help.push.apply(help, ['Examples:'].concat(formatTable(examplesTable, 5), '')) | ||
ui.div() | ||
} | ||
@@ -204,9 +210,32 @@ | ||
var e = epilog.replace(/\$0/g, yargs.$0) | ||
if (wrap) e = wordwrap(0, wrap)(e) | ||
help.push(e, '') | ||
ui.div(e + '\n') | ||
} | ||
return help.join('\n') | ||
return ui.toString() | ||
} | ||
// return the maximum width of a string | ||
// in the left-hand column of a table. | ||
function maxWidth (table) { | ||
var width = 0 | ||
// table might be of the form [leftColumn], | ||
// or {key: leftColumn}} | ||
if (!Array.isArray(table)) { | ||
table = Object.keys(table).map(function (key) { | ||
return [table[key]] | ||
}) | ||
} | ||
table.forEach(function (v) { | ||
width = Math.max(v[0].length, width) | ||
}) | ||
// if we've enabled 'wrap' we should limit | ||
// the max-width of the left-column. | ||
if (wrap) width = Math.min(width, parseInt(wrap * 0.5, 10)) | ||
return width | ||
} | ||
// make sure any options set for aliases, | ||
@@ -273,118 +302,2 @@ // are copied to the keys being aliased. | ||
// word-wrapped two-column layout used by | ||
// examples, options, commands. | ||
function formatTable (table, padding) { | ||
var output = [] | ||
// size of left-hand-column. | ||
var llen = longest(Object.keys(table)) | ||
// don't allow the left-column to take up | ||
// more than half of the screen. | ||
if (wrap) { | ||
llen = Math.min(llen, parseInt(wrap / 2, 10)) | ||
} | ||
// size of right-column. | ||
var desclen = longest(Object.keys(table).map(function (k) { | ||
return table[k].desc | ||
})) | ||
Object.keys(table).forEach(function (left) { | ||
var desc = table[left].desc, | ||
extra = table[left].extra, | ||
leftLines = null | ||
if (wrap) { | ||
desc = wordwrap(llen + padding + 1, wrap)(desc) | ||
.slice(llen + padding + 1) | ||
} | ||
// if we need to wrap the left-hand-column, | ||
// split it on to multiple lines. | ||
if (wrap && left.length > llen) { | ||
leftLines = wordwrap(2, llen)(left.trim()).split('\n') | ||
left = '' | ||
} | ||
var lpadding = new Array( | ||
Math.max(llen - left.length + padding, 0) | ||
).join(' ') | ||
var dpadding = new Array( | ||
Math.max(desclen - desc.length + 1, 0) | ||
).join(' ') | ||
if (!wrap && dpadding.length > 0) { | ||
desc += dpadding | ||
} | ||
var prelude = ' ' + left + lpadding | ||
var body = [ desc, extra ].filter(Boolean).join(' ') | ||
if (wrap) { | ||
var dlines = desc.split('\n') | ||
var dlen = dlines.slice(-1)[0].length | ||
+ (dlines.length === 1 ? prelude.length : 0) | ||
if (extra.length > wrap) { | ||
body = desc + '\n' + wordwrap(llen + 4, wrap)(extra) | ||
} else { | ||
body = desc + (dlen + extra.length > wrap - 2 | ||
? '\n' | ||
+ new Array(wrap - extra.length + 1).join(' ') | ||
+ extra | ||
: new Array(wrap - extra.length - dlen + 1).join(' ') | ||
+ extra | ||
) | ||
} | ||
} | ||
if (leftLines) { // handle word-wrapping the left-hand-column. | ||
var rightLines = body.split('\n'), | ||
firstLine = prelude + rightLines[0], | ||
lineCount = Math.max(leftLines.length, rightLines.length) | ||
for (var i = 0; i < lineCount; i++) { | ||
var l = leftLines[i], | ||
r = i ? rightLines[i] : firstLine | ||
output.push(strcpy(l, r, firstLine.length)) | ||
} | ||
} else { | ||
output.push(prelude + body) | ||
} | ||
}) | ||
return output | ||
} | ||
// find longest string in array of strings. | ||
function longest (xs) { | ||
return Math.max.apply( | ||
null, | ||
xs.map(function (x) { return x.length }) | ||
) | ||
} | ||
// copy one string into another, used when | ||
// formatting usage table. | ||
function strcpy (source, destination, width) { | ||
var str = '' | ||
source = source || '' | ||
destination = destination || new Array(width).join(' ') | ||
for (var i = 0; i < destination.length; i++) { | ||
var char = destination.charAt(i) | ||
if (char === ' ') char = source.charAt(i) || char | ||
str += char | ||
} | ||
return str | ||
} | ||
// guess the width of the console window, max-width 100. | ||
@@ -391,0 +304,0 @@ function windowWidth () { |
{ | ||
"name": "yargs", | ||
"version": "3.7.2", | ||
"version": "3.8.0", | ||
"description": "Light-weight option parsing with an argv hash. No optstrings attached.", | ||
@@ -14,5 +14,5 @@ "main": "./index.js", | ||
"camelcase": "^1.0.2", | ||
"cliui": "^2.1.0", | ||
"decamelize": "^1.0.0", | ||
"window-size": "0.1.0", | ||
"wordwrap": "0.0.2" | ||
"window-size": "0.1.0" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -170,3 +170,3 @@ yargs | ||
$ ./area.js -w 55 -w 11 | ||
$ ./area.js -w 55 -h 11 | ||
605 | ||
@@ -173,0 +173,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
98860
1226
+ Addedcliui@^2.1.0
+ Addedalign-text@0.1.4(transitive)
+ Addedcenter-align@0.1.3(transitive)
+ Addedcliui@2.1.0(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedkind-of@3.2.2(transitive)
+ Addedlazy-cache@1.0.4(transitive)
+ Addedlongest@1.0.1(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedright-align@0.1.3(transitive)
- Removedwordwrap@0.0.2