yargs 3.7.2 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +27 -8
- package/index.js +31 -7
- package/lib/parser.js +50 -3
- package/lib/usage.js +92 -179
- package/package.json +12 -22
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
## Change Log
|
|
2
2
|
|
|
3
|
+
### v3.10.0 (2015/05/29 04:25 +00:00)
|
|
4
|
+
|
|
5
|
+
- [#165](https://github.com/bcoe/yargs/pull/165) expose yargs.terminalWidth() thanks @ensonic (@bcoe)
|
|
6
|
+
- [#164](https://github.com/bcoe/yargs/pull/164) better array handling thanks @getify (@bcoe)
|
|
7
|
+
|
|
8
|
+
### v3.9.1 (2015/05/20 05:14 +00:00)
|
|
9
|
+
- [b6662b6](https://github.com/bcoe/yargs/commit/b6662b6774cfeab4876f41ec5e2f67b7698f4e2f) clarify .config() docs (@linclark)
|
|
10
|
+
- [0291360](https://github.com/bcoe/yargs/commit/02913606285ce31ce81d7f12c48d8a3029776ec7) fixed tests, switched to nyc for coverage, fixed security issue, added Lin as collaborator (@bcoe)
|
|
11
|
+
|
|
12
|
+
### v3.9.0 (2015/05/10 18:32 +00:00)
|
|
13
|
+
- [#157](https://github.com/bcoe/yargs/pull/157) Merge pull request #157 from bcoe/command-yargs. allows handling of command specific arguments. Thanks for the suggestion @ohjames (@bcoe)
|
|
14
|
+
- [#158](https://github.com/bcoe/yargs/pull/158) Merge pull request #158 from kemitchell/spdx-license. Update license format (@kemitchell)
|
|
15
|
+
|
|
16
|
+
### v3.8.0 (2015/04/24 23:10 +00:00)
|
|
17
|
+
- [#154](https://github.com/bcoe/yargs/pull/154) showHelp's method signature was misleading fixes #153 (@bcoe)
|
|
18
|
+
- [#151](https://github.com/bcoe/yargs/pull/151) refactor yargs' table layout logic to use new helper library (@bcoe)
|
|
19
|
+
- [#150](https://github.com/bcoe/yargs/pull/150) Fix README example in argument requirements (@annonymouse)
|
|
20
|
+
|
|
3
21
|
### v3.7.2 (2015/04/13 11:52 -07:00)
|
|
4
22
|
|
|
5
23
|
* [679fbbf](https://github.com/bcoe/yargs/commit/679fbbf55904030ccee8a2635e8e5f46551ab2f0) updated yargs to use the [standard](https://github.com/feross/standard) style guide (agokjr)
|
package/README.md
CHANGED
|
@@ -167,7 +167,7 @@ console.log("The area is:", argv.w * argv.h);
|
|
|
167
167
|
|
|
168
168
|
***
|
|
169
169
|
|
|
170
|
-
$ ./area.js -w 55 -
|
|
170
|
+
$ ./area.js -w 55 -h 11
|
|
171
171
|
605
|
|
172
172
|
|
|
173
173
|
$ node ./area.js -w 4.91 -w 2.51
|
|
@@ -501,18 +501,33 @@ present script similar to how `$0` works in bash or perl.
|
|
|
501
501
|
|
|
502
502
|
`opts` is optional and acts like calling `.options(opts)`.
|
|
503
503
|
|
|
504
|
-
.command(cmd, desc)
|
|
504
|
+
.command(cmd, desc, [fn])
|
|
505
505
|
-------------------
|
|
506
506
|
|
|
507
|
-
Document the commands exposed by your application
|
|
507
|
+
Document the commands exposed by your application.
|
|
508
508
|
|
|
509
|
-
|
|
509
|
+
use `desc` to provide a description for each command your application accepts (the
|
|
510
|
+
values stored in `argv._`).
|
|
511
|
+
|
|
512
|
+
Optionally, you can provide a handler `fn` which will be executed when
|
|
513
|
+
a given command is provided. The handler will be executed with an instance
|
|
514
|
+
of `yargs`, which can be used to compose nested commands.
|
|
515
|
+
|
|
516
|
+
Here's an example of top-level and nested commands in action:
|
|
510
517
|
|
|
511
518
|
```js
|
|
512
519
|
var argv = require('yargs')
|
|
513
520
|
.usage('npm <command>')
|
|
514
521
|
.command('install', 'tis a mighty fine package to install')
|
|
515
|
-
.command('publish', 'shiver me timbers, should you be sharing all that')
|
|
522
|
+
.command('publish', 'shiver me timbers, should you be sharing all that', function (yargs) {
|
|
523
|
+
argv = yargs.option('f', {
|
|
524
|
+
alias: 'force',
|
|
525
|
+
description: 'yar, it usually be a bad idea'
|
|
526
|
+
})
|
|
527
|
+
.help('help')
|
|
528
|
+
.argv
|
|
529
|
+
})
|
|
530
|
+
.help('help')
|
|
516
531
|
.argv;
|
|
517
532
|
```
|
|
518
533
|
|
|
@@ -580,7 +595,7 @@ regardless of whether they resemble numbers.
|
|
|
580
595
|
----------
|
|
581
596
|
|
|
582
597
|
Tell the parser to interpret `key` as an array. If `.array('foo')` is set,
|
|
583
|
-
`--foo bar` will be parsed as `['bar']` rather than as `'bar'`.
|
|
598
|
+
`--foo foo bar` will be parsed as `['foo', 'bar']` rather than as `'bar'`.
|
|
584
599
|
|
|
585
600
|
.nargs(key, count)
|
|
586
601
|
-----------
|
|
@@ -603,8 +618,9 @@ Optionally `.nargs()` can take an object of `key`/`narg` pairs.
|
|
|
603
618
|
.config(key)
|
|
604
619
|
------------
|
|
605
620
|
|
|
606
|
-
Tells the parser
|
|
607
|
-
|
|
621
|
+
Tells the parser that if the option specified by `key` is passed in, it
|
|
622
|
+
should be interpreted as a path to a JSON config file. The file is loaded
|
|
623
|
+
and parsed, and its properties are set as arguments.
|
|
608
624
|
|
|
609
625
|
.wrap(columns)
|
|
610
626
|
--------------
|
|
@@ -614,6 +630,9 @@ Format usage output to wrap at `columns` many columns.
|
|
|
614
630
|
By default wrap will be set to `Math.min(80, windowWidth)`. Use `.wrap(null)` to
|
|
615
631
|
specify no column limit.
|
|
616
632
|
|
|
633
|
+
`yargs.wrap(yargs.terminalWidth())` can be used to maximize the width
|
|
634
|
+
of yargs' usage instructions.
|
|
635
|
+
|
|
617
636
|
.strict()
|
|
618
637
|
---------
|
|
619
638
|
|
package/index.js
CHANGED
|
@@ -67,6 +67,8 @@ function Argv (processArgs, cwd) {
|
|
|
67
67
|
helpOpt = null
|
|
68
68
|
versionOpt = null
|
|
69
69
|
completionOpt = null
|
|
70
|
+
commandHandlers = {}
|
|
71
|
+
self.parsed = false
|
|
70
72
|
|
|
71
73
|
return self
|
|
72
74
|
}
|
|
@@ -108,11 +110,17 @@ function Argv (processArgs, cwd) {
|
|
|
108
110
|
return self
|
|
109
111
|
}
|
|
110
112
|
|
|
111
|
-
self.command = function (cmd, description) {
|
|
113
|
+
self.command = function (cmd, description, fn) {
|
|
112
114
|
usage.command(cmd, description)
|
|
115
|
+
if (fn) commandHandlers[cmd] = fn
|
|
113
116
|
return self
|
|
114
117
|
}
|
|
115
118
|
|
|
119
|
+
var commandHandlers = {}
|
|
120
|
+
self.getCommandHandlers = function () {
|
|
121
|
+
return commandHandlers
|
|
122
|
+
}
|
|
123
|
+
|
|
116
124
|
self.string = function (strings) {
|
|
117
125
|
options.string.push.apply(options.string, [].concat(strings))
|
|
118
126
|
return self
|
|
@@ -287,9 +295,9 @@ function Argv (processArgs, cwd) {
|
|
|
287
295
|
return strict
|
|
288
296
|
}
|
|
289
297
|
|
|
290
|
-
self.showHelp = function (
|
|
298
|
+
self.showHelp = function (level) {
|
|
291
299
|
if (!self.parsed) parseArgs(processArgs) // run parser, if it has not already been executed.
|
|
292
|
-
usage.showHelp(
|
|
300
|
+
usage.showHelp(level)
|
|
293
301
|
return self
|
|
294
302
|
}
|
|
295
303
|
|
|
@@ -297,6 +305,7 @@ function Argv (processArgs, cwd) {
|
|
|
297
305
|
self.version = function (ver, opt, msg) {
|
|
298
306
|
versionOpt = opt || 'version'
|
|
299
307
|
usage.version(ver)
|
|
308
|
+
self.boolean(versionOpt)
|
|
300
309
|
self.describe(versionOpt, msg || 'Show version number')
|
|
301
310
|
return self
|
|
302
311
|
}
|
|
@@ -304,6 +313,7 @@ function Argv (processArgs, cwd) {
|
|
|
304
313
|
var helpOpt = null
|
|
305
314
|
self.addHelpOpt = function (opt, msg) {
|
|
306
315
|
helpOpt = opt
|
|
316
|
+
self.boolean(opt)
|
|
307
317
|
self.describe(opt, msg || 'Show help')
|
|
308
318
|
return self
|
|
309
319
|
}
|
|
@@ -369,6 +379,10 @@ function Argv (processArgs, cwd) {
|
|
|
369
379
|
return validation
|
|
370
380
|
}
|
|
371
381
|
|
|
382
|
+
self.terminalWidth = function () {
|
|
383
|
+
return require('window-size').width
|
|
384
|
+
}
|
|
385
|
+
|
|
372
386
|
Object.defineProperty(self, 'argv', {
|
|
373
387
|
get: function () {
|
|
374
388
|
var args = null
|
|
@@ -386,8 +400,8 @@ function Argv (processArgs, cwd) {
|
|
|
386
400
|
|
|
387
401
|
function parseArgs (args) {
|
|
388
402
|
var parsed = Parser(args, options),
|
|
389
|
-
|
|
390
|
-
|
|
403
|
+
argv = parsed.argv,
|
|
404
|
+
aliases = parsed.aliases
|
|
391
405
|
|
|
392
406
|
argv.$0 = self.$0
|
|
393
407
|
|
|
@@ -401,13 +415,23 @@ function Argv (processArgs, cwd) {
|
|
|
401
415
|
}
|
|
402
416
|
}
|
|
403
417
|
|
|
418
|
+
// if there's a handler associated with a
|
|
419
|
+
// command defer processing to it.
|
|
420
|
+
var handlerKeys = Object.keys(self.getCommandHandlers())
|
|
421
|
+
for (var i = 0, command; (command = handlerKeys[i]) !== undefined; i++) {
|
|
422
|
+
if (~argv._.indexOf(command)) {
|
|
423
|
+
self.getCommandHandlers()[command](self.reset())
|
|
424
|
+
return self.argv
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
404
428
|
Object.keys(argv).forEach(function (key) {
|
|
405
|
-
if (key === helpOpt) {
|
|
429
|
+
if (key === helpOpt && argv[key]) {
|
|
406
430
|
self.showHelp('log')
|
|
407
431
|
if (exitProcess) {
|
|
408
432
|
process.exit(0)
|
|
409
433
|
}
|
|
410
|
-
} else if (key === versionOpt) {
|
|
434
|
+
} else if (key === versionOpt && argv[key]) {
|
|
411
435
|
usage.showVersion()
|
|
412
436
|
if (exitProcess) {
|
|
413
437
|
process.exit(0)
|
package/lib/parser.js
CHANGED
|
@@ -69,7 +69,8 @@ module.exports = function (args, opts) {
|
|
|
69
69
|
key,
|
|
70
70
|
letters,
|
|
71
71
|
m,
|
|
72
|
-
next
|
|
72
|
+
next,
|
|
73
|
+
value
|
|
73
74
|
|
|
74
75
|
// -- seperated by =
|
|
75
76
|
if (arg.match(/^--.+=/)) {
|
|
@@ -77,7 +78,18 @@ module.exports = function (args, opts) {
|
|
|
77
78
|
// 'dotall' regex modifier. See:
|
|
78
79
|
// http://stackoverflow.com/a/1068308/13216
|
|
79
80
|
m = arg.match(/^--([^=]+)=([\s\S]*)$/)
|
|
80
|
-
|
|
81
|
+
|
|
82
|
+
// nargs format = '--f=monkey washing cat'
|
|
83
|
+
if (checkAllAliases(m[1], opts.narg)) {
|
|
84
|
+
args.splice(i + 1, m[1], m[2])
|
|
85
|
+
i = eatNargs(i, m[1], args)
|
|
86
|
+
// arrays format = '--f=a b c'
|
|
87
|
+
} else if (checkAllAliases(m[1], flags.arrays) && args.length > i + 1) {
|
|
88
|
+
args.splice(i + 1, m[1], m[2])
|
|
89
|
+
i = eatArray(i, m[1], args)
|
|
90
|
+
} else {
|
|
91
|
+
setArg(m[1], m[2])
|
|
92
|
+
}
|
|
81
93
|
} else if (arg.match(/^--no-.+/)) {
|
|
82
94
|
key = arg.match(/^--no-(.+)/)[1]
|
|
83
95
|
setArg(key, false)
|
|
@@ -86,8 +98,12 @@ module.exports = function (args, opts) {
|
|
|
86
98
|
} else if (arg.match(/^--.+/)) {
|
|
87
99
|
key = arg.match(/^--(.+)/)[1]
|
|
88
100
|
|
|
101
|
+
// nargs format = '--foo a b c'
|
|
89
102
|
if (checkAllAliases(key, opts.narg)) {
|
|
90
103
|
i = eatNargs(i, key, args)
|
|
104
|
+
// array format = '--foo a b c'
|
|
105
|
+
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
|
|
106
|
+
i = eatArray(i, key, args)
|
|
91
107
|
} else {
|
|
92
108
|
next = args[i + 1]
|
|
93
109
|
|
|
@@ -130,7 +146,21 @@ module.exports = function (args, opts) {
|
|
|
130
146
|
next = arg.slice(j + 2)
|
|
131
147
|
|
|
132
148
|
if (letters[j + 1] && letters[j + 1] === '=') {
|
|
133
|
-
|
|
149
|
+
value = arg.slice(j + 3)
|
|
150
|
+
key = letters[j]
|
|
151
|
+
|
|
152
|
+
// nargs format = '-f=monkey washing cat'
|
|
153
|
+
if (checkAllAliases(letters[j], opts.narg)) {
|
|
154
|
+
args.splice(i + 1, 0, value)
|
|
155
|
+
i = eatNargs(i, key, args)
|
|
156
|
+
// array format = '-f=a b c'
|
|
157
|
+
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
|
|
158
|
+
args.splice(i + 1, 0, value)
|
|
159
|
+
i = eatArray(i, key, args)
|
|
160
|
+
} else {
|
|
161
|
+
setArg(key, value)
|
|
162
|
+
}
|
|
163
|
+
|
|
134
164
|
broken = true
|
|
135
165
|
break
|
|
136
166
|
}
|
|
@@ -159,8 +189,12 @@ module.exports = function (args, opts) {
|
|
|
159
189
|
key = arg.slice(-1)[0]
|
|
160
190
|
|
|
161
191
|
if (!broken && key !== '-') {
|
|
192
|
+
// nargs format = '-f a b c'
|
|
162
193
|
if (checkAllAliases(key, opts.narg)) {
|
|
163
194
|
i = eatNargs(i, key, args)
|
|
195
|
+
// array format = '-f a b c'
|
|
196
|
+
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
|
|
197
|
+
i = eatArray(i, key, args)
|
|
164
198
|
} else {
|
|
165
199
|
if (args[i + 1] && !/^(-|--)[^-]/.test(args[i + 1])
|
|
166
200
|
&& !checkAllAliases(key, flags.bools)
|
|
@@ -207,6 +241,19 @@ module.exports = function (args, opts) {
|
|
|
207
241
|
return (i + toEat)
|
|
208
242
|
}
|
|
209
243
|
|
|
244
|
+
// if an option is an array, eat all non-hyphenated arguments
|
|
245
|
+
// following it... YUM!
|
|
246
|
+
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
|
|
247
|
+
function eatArray (i, key, args) {
|
|
248
|
+
for (var ii = i + 1; ii < args.length; ii++) {
|
|
249
|
+
if (/^-/.test(args[ii])) break
|
|
250
|
+
i = ii
|
|
251
|
+
setArg(key, args[ii])
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return i
|
|
255
|
+
}
|
|
256
|
+
|
|
210
257
|
function setArg (key, val) {
|
|
211
258
|
// handle parsing boolean arguments --foo=true --bar false.
|
|
212
259
|
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
|
package/lib/usage.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// this file handles outputting usage instructions,
|
|
2
2
|
// failures, etc. keeps logging in one place.
|
|
3
|
-
var
|
|
4
|
-
|
|
3
|
+
var cliui = require('cliui'),
|
|
4
|
+
decamelize = require('decamelize'),
|
|
5
5
|
wsize = require('window-size')
|
|
6
6
|
|
|
7
7
|
module.exports = function (yargs) {
|
|
@@ -94,40 +94,40 @@ module.exports = function (yargs) {
|
|
|
94
94
|
normalizeAliases()
|
|
95
95
|
|
|
96
96
|
var demanded = yargs.getDemanded(),
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
97
|
+
options = yargs.getOptions(),
|
|
98
|
+
keys = Object.keys(
|
|
99
|
+
Object.keys(descriptions)
|
|
100
|
+
.concat(Object.keys(demanded))
|
|
101
|
+
.concat(Object.keys(options.default))
|
|
102
|
+
.reduce(function (acc, key) {
|
|
103
|
+
if (key !== '_') acc[key] = true
|
|
104
|
+
return acc
|
|
105
|
+
}, {})
|
|
106
|
+
),
|
|
107
|
+
ui = cliui({
|
|
108
|
+
width: wrap,
|
|
109
|
+
wrap: !!wrap
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
// the usage string.
|
|
113
|
+
if (usage) {
|
|
114
|
+
var u = usage.replace(/\$0/g, yargs.$0)
|
|
115
|
+
ui.div(u + '\n')
|
|
116
|
+
}
|
|
109
117
|
|
|
110
118
|
// your application's commands, i.e., non-option
|
|
111
119
|
// arguments populated in '_'.
|
|
112
120
|
if (commands.length) {
|
|
113
|
-
|
|
121
|
+
ui.div('Commands:')
|
|
114
122
|
|
|
115
|
-
var commandsTable = {}
|
|
116
123
|
commands.forEach(function (command) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
124
|
+
ui.div(
|
|
125
|
+
{text: command[0], padding: [0, 2, 0, 2], width: maxWidth(commands) + 4},
|
|
126
|
+
{text: command[1]}
|
|
127
|
+
)
|
|
121
128
|
})
|
|
122
129
|
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// the usage string.
|
|
127
|
-
if (usage) {
|
|
128
|
-
var u = usage.replace(/\$0/g, yargs.$0)
|
|
129
|
-
if (wrap) u = wordwrap(0, wrap)(u)
|
|
130
|
-
help.unshift(u, '')
|
|
130
|
+
ui.div()
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
// the options table.
|
|
@@ -150,58 +150,87 @@ module.exports = function (yargs) {
|
|
|
150
150
|
return acc
|
|
151
151
|
}, {})
|
|
152
152
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
153
|
+
if (keys.length) {
|
|
154
|
+
ui.div('Options:')
|
|
155
|
+
|
|
156
|
+
keys.forEach(function (key) {
|
|
157
|
+
var kswitch = switches[key]
|
|
158
|
+
var desc = descriptions[key] || ''
|
|
159
|
+
var type = null
|
|
160
|
+
|
|
161
|
+
if (~options.boolean.indexOf(key)) type = '[boolean]'
|
|
162
|
+
if (~options.count.indexOf(key)) type = '[count]'
|
|
163
|
+
if (~options.string.indexOf(key)) type = '[string]'
|
|
164
|
+
if (~options.normalize.indexOf(key)) type = '[string]'
|
|
165
|
+
if (~options.array.indexOf(key)) type = '[array]'
|
|
166
|
+
|
|
167
|
+
var extra = [
|
|
168
|
+
type,
|
|
169
|
+
demanded[key] ? '[required]' : null,
|
|
170
|
+
defaultString(options.default[key], options.defaultDescription[key])
|
|
171
|
+
].filter(Boolean).join(' ')
|
|
172
|
+
|
|
173
|
+
ui.span(
|
|
174
|
+
{text: kswitch, padding: [0, 2, 0, 2], width: maxWidth(switches) + 4},
|
|
175
|
+
desc
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
if (extra) ui.div({text: extra, padding: [0, 0, 0, 2], align: 'right'})
|
|
179
|
+
else ui.div()
|
|
180
|
+
})
|
|
177
181
|
|
|
178
|
-
|
|
182
|
+
ui.div()
|
|
183
|
+
}
|
|
179
184
|
|
|
180
185
|
// describe some common use-cases for your application.
|
|
181
186
|
if (examples.length) {
|
|
187
|
+
ui.div('Examples:')
|
|
188
|
+
|
|
182
189
|
examples.forEach(function (example) {
|
|
183
190
|
example[0] = example[0].replace(/\$0/g, yargs.$0)
|
|
184
191
|
})
|
|
185
192
|
|
|
186
|
-
var examplesTable = {}
|
|
187
193
|
examples.forEach(function (example) {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
194
|
+
ui.div(
|
|
195
|
+
{text: example[0], padding: [0, 2, 0, 2], width: maxWidth(examples) + 4},
|
|
196
|
+
example[1]
|
|
197
|
+
)
|
|
192
198
|
})
|
|
193
199
|
|
|
194
|
-
|
|
200
|
+
ui.div()
|
|
195
201
|
}
|
|
196
202
|
|
|
197
203
|
// the usage string.
|
|
198
204
|
if (epilog) {
|
|
199
205
|
var e = epilog.replace(/\$0/g, yargs.$0)
|
|
200
|
-
|
|
201
|
-
help.push(e, '')
|
|
206
|
+
ui.div(e + '\n')
|
|
202
207
|
}
|
|
203
208
|
|
|
204
|
-
return
|
|
209
|
+
return ui.toString()
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// return the maximum width of a string
|
|
213
|
+
// in the left-hand column of a table.
|
|
214
|
+
function maxWidth (table) {
|
|
215
|
+
var width = 0
|
|
216
|
+
|
|
217
|
+
// table might be of the form [leftColumn],
|
|
218
|
+
// or {key: leftColumn}}
|
|
219
|
+
if (!Array.isArray(table)) {
|
|
220
|
+
table = Object.keys(table).map(function (key) {
|
|
221
|
+
return [table[key]]
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
table.forEach(function (v) {
|
|
226
|
+
width = Math.max(v[0].length, width)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
// if we've enabled 'wrap' we should limit
|
|
230
|
+
// the max-width of the left-column.
|
|
231
|
+
if (wrap) width = Math.min(width, parseInt(wrap * 0.5, 10))
|
|
232
|
+
|
|
233
|
+
return width
|
|
205
234
|
}
|
|
206
235
|
|
|
207
236
|
// make sure any options set for aliases,
|
|
@@ -265,123 +294,7 @@ module.exports = function (yargs) {
|
|
|
265
294
|
return string + ']'
|
|
266
295
|
}
|
|
267
296
|
|
|
268
|
-
//
|
|
269
|
-
// examples, options, commands.
|
|
270
|
-
function formatTable (table, padding) {
|
|
271
|
-
var output = []
|
|
272
|
-
|
|
273
|
-
// size of left-hand-column.
|
|
274
|
-
var llen = longest(Object.keys(table))
|
|
275
|
-
|
|
276
|
-
// don't allow the left-column to take up
|
|
277
|
-
// more than half of the screen.
|
|
278
|
-
if (wrap) {
|
|
279
|
-
llen = Math.min(llen, parseInt(wrap / 2, 10))
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// size of right-column.
|
|
283
|
-
var desclen = longest(Object.keys(table).map(function (k) {
|
|
284
|
-
return table[k].desc
|
|
285
|
-
}))
|
|
286
|
-
|
|
287
|
-
Object.keys(table).forEach(function (left) {
|
|
288
|
-
var desc = table[left].desc,
|
|
289
|
-
extra = table[left].extra,
|
|
290
|
-
leftLines = null
|
|
291
|
-
|
|
292
|
-
if (wrap) {
|
|
293
|
-
desc = wordwrap(llen + padding + 1, wrap)(desc)
|
|
294
|
-
.slice(llen + padding + 1)
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// if we need to wrap the left-hand-column,
|
|
298
|
-
// split it on to multiple lines.
|
|
299
|
-
if (wrap && left.length > llen) {
|
|
300
|
-
leftLines = wordwrap(2, llen)(left.trim()).split('\n')
|
|
301
|
-
left = ''
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
var lpadding = new Array(
|
|
305
|
-
Math.max(llen - left.length + padding, 0)
|
|
306
|
-
).join(' ')
|
|
307
|
-
|
|
308
|
-
var dpadding = new Array(
|
|
309
|
-
Math.max(desclen - desc.length + 1, 0)
|
|
310
|
-
).join(' ')
|
|
311
|
-
|
|
312
|
-
if (!wrap && dpadding.length > 0) {
|
|
313
|
-
desc += dpadding
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
var prelude = ' ' + left + lpadding
|
|
317
|
-
|
|
318
|
-
var body = [ desc, extra ].filter(Boolean).join(' ')
|
|
319
|
-
|
|
320
|
-
if (wrap) {
|
|
321
|
-
var dlines = desc.split('\n')
|
|
322
|
-
var dlen = dlines.slice(-1)[0].length
|
|
323
|
-
+ (dlines.length === 1 ? prelude.length : 0)
|
|
324
|
-
|
|
325
|
-
if (extra.length > wrap) {
|
|
326
|
-
body = desc + '\n' + wordwrap(llen + 4, wrap)(extra)
|
|
327
|
-
} else {
|
|
328
|
-
body = desc + (dlen + extra.length > wrap - 2
|
|
329
|
-
? '\n'
|
|
330
|
-
+ new Array(wrap - extra.length + 1).join(' ')
|
|
331
|
-
+ extra
|
|
332
|
-
: new Array(wrap - extra.length - dlen + 1).join(' ')
|
|
333
|
-
+ extra
|
|
334
|
-
)
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
if (leftLines) { // handle word-wrapping the left-hand-column.
|
|
339
|
-
var rightLines = body.split('\n'),
|
|
340
|
-
firstLine = prelude + rightLines[0],
|
|
341
|
-
lineCount = Math.max(leftLines.length, rightLines.length)
|
|
342
|
-
|
|
343
|
-
for (var i = 0; i < lineCount; i++) {
|
|
344
|
-
var l = leftLines[i],
|
|
345
|
-
r = i ? rightLines[i] : firstLine
|
|
346
|
-
|
|
347
|
-
output.push(strcpy(l, r, firstLine.length))
|
|
348
|
-
}
|
|
349
|
-
} else {
|
|
350
|
-
output.push(prelude + body)
|
|
351
|
-
}
|
|
352
|
-
})
|
|
353
|
-
|
|
354
|
-
return output
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// find longest string in array of strings.
|
|
358
|
-
function longest (xs) {
|
|
359
|
-
return Math.max.apply(
|
|
360
|
-
null,
|
|
361
|
-
xs.map(function (x) { return x.length })
|
|
362
|
-
)
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// copy one string into another, used when
|
|
366
|
-
// formatting usage table.
|
|
367
|
-
function strcpy (source, destination, width) {
|
|
368
|
-
var str = ''
|
|
369
|
-
|
|
370
|
-
source = source || ''
|
|
371
|
-
destination = destination || new Array(width).join(' ')
|
|
372
|
-
|
|
373
|
-
for (var i = 0; i < destination.length; i++) {
|
|
374
|
-
var char = destination.charAt(i)
|
|
375
|
-
|
|
376
|
-
if (char === ' ') char = source.charAt(i) || char
|
|
377
|
-
|
|
378
|
-
str += char
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
return str
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// guess the width of the console window, max-width 100.
|
|
297
|
+
// guess the width of the console window, max-width 80.
|
|
385
298
|
function windowWidth () {
|
|
386
299
|
return wsize.width ? Math.min(80, wsize.width) : null
|
|
387
300
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yargs",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"description": "Light-weight option parsing with an argv hash. No optstrings attached.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"files": [
|
|
@@ -11,40 +11,26 @@
|
|
|
11
11
|
],
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"camelcase": "^1.0.2",
|
|
14
|
+
"cliui": "^2.1.0",
|
|
14
15
|
"decamelize": "^1.0.0",
|
|
15
|
-
"window-size": "0.1.0"
|
|
16
|
-
"wordwrap": "0.0.2"
|
|
16
|
+
"window-size": "0.1.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"chai": "^2.2.0",
|
|
20
20
|
"coveralls": "^2.11.2",
|
|
21
21
|
"hashish": "0.0.4",
|
|
22
22
|
"mocha": "^2.2.1",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"patched-blanket": "^1.0.1",
|
|
26
|
-
"standard": "^3.6.0"
|
|
23
|
+
"nyc": "^2.2.1",
|
|
24
|
+
"standard": "^3.11.1"
|
|
27
25
|
},
|
|
28
26
|
"scripts": {
|
|
29
|
-
"test": "standard && mocha --check-leaks
|
|
27
|
+
"test": "standard && nyc mocha --check-leaks && nyc report",
|
|
28
|
+
"coverage": "nyc report --reporter=text-lcov | coveralls"
|
|
30
29
|
},
|
|
31
30
|
"repository": {
|
|
32
31
|
"type": "git",
|
|
33
32
|
"url": "http://github.com/bcoe/yargs.git"
|
|
34
33
|
},
|
|
35
|
-
"config": {
|
|
36
|
-
"blanket": {
|
|
37
|
-
"pattern": [
|
|
38
|
-
"lib",
|
|
39
|
-
"index.js"
|
|
40
|
-
],
|
|
41
|
-
"data-cover-never": [
|
|
42
|
-
"node_modules",
|
|
43
|
-
"test"
|
|
44
|
-
],
|
|
45
|
-
"output-reporter": "spec"
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
34
|
"standard": {
|
|
49
35
|
"ignore": [
|
|
50
36
|
"**/example/**"
|
|
@@ -86,9 +72,13 @@
|
|
|
86
72
|
{
|
|
87
73
|
"name": "Benjamin Horsleben",
|
|
88
74
|
"url": "https://github.com/fizker"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"name": "Lin Clark",
|
|
78
|
+
"url": "https://github.com/linclark"
|
|
89
79
|
}
|
|
90
80
|
],
|
|
91
|
-
"license": "MIT
|
|
81
|
+
"license": "MIT",
|
|
92
82
|
"engine": {
|
|
93
83
|
"node": ">=0.4"
|
|
94
84
|
}
|