yargs 8.0.0 → 9.0.1

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/lib/usage.js CHANGED
@@ -1,3 +1,4 @@
1
+ 'use strict'
1
2
  // this file handles outputting usage instructions,
2
3
  // failures, etc. keeps logging in one place.
3
4
  const stringWidth = require('string-width')
@@ -5,19 +6,19 @@ const objFilter = require('./obj-filter')
5
6
  const setBlocking = require('set-blocking')
6
7
  const YError = require('./yerror')
7
8
 
8
- module.exports = function (yargs, y18n) {
9
+ module.exports = function usage (yargs, y18n) {
9
10
  const __ = y18n.__
10
11
  const self = {}
11
12
 
12
13
  // methods for ouputting/building failure message.
13
- var fails = []
14
- self.failFn = function (f) {
14
+ const fails = []
15
+ self.failFn = function failFn (f) {
15
16
  fails.push(f)
16
17
  }
17
18
 
18
- var failMessage = null
19
- var showHelpOnFail = true
20
- self.showHelpOnFail = function (enabled, message) {
19
+ let failMessage = null
20
+ let showHelpOnFail = true
21
+ self.showHelpOnFail = function showHelpOnFailFn (enabled, message) {
21
22
  if (typeof enabled === 'string') {
22
23
  message = enabled
23
24
  enabled = true
@@ -29,12 +30,12 @@ module.exports = function (yargs, y18n) {
29
30
  return self
30
31
  }
31
32
 
32
- var failureOutput = false
33
- self.fail = function (msg, err) {
33
+ let failureOutput = false
34
+ self.fail = function fail (msg, err) {
34
35
  const logger = yargs._getLoggerInstance()
35
36
 
36
37
  if (fails.length) {
37
- for (var i = fails.length - 1; i >= 0; --i) {
38
+ for (let i = fails.length - 1; i >= 0; --i) {
38
39
  fails[i](msg, err, self)
39
40
  }
40
41
  } else {
@@ -63,56 +64,50 @@ module.exports = function (yargs, y18n) {
63
64
  }
64
65
 
65
66
  // methods for ouputting/building help (usage) message.
66
- var usage
67
- self.usage = function (msg) {
67
+ let usage
68
+ self.usage = (msg) => {
68
69
  usage = msg
69
70
  }
70
- self.getUsage = function () {
71
- return usage
72
- }
71
+ self.getUsage = () => usage
73
72
 
74
- var examples = []
75
- self.example = function (cmd, description) {
73
+ let examples = []
74
+ self.example = (cmd, description) => {
76
75
  examples.push([cmd, description || ''])
77
76
  }
78
77
 
79
- var commands = []
80
- self.command = function (cmd, description, isDefault, aliases) {
78
+ let commands = []
79
+ self.command = function command (cmd, description, isDefault, aliases) {
81
80
  // the last default wins, so cancel out any previously set default
82
81
  if (isDefault) {
83
- commands = commands.map(function (cmdArray) {
82
+ commands = commands.map((cmdArray) => {
84
83
  cmdArray[2] = false
85
84
  return cmdArray
86
85
  })
87
86
  }
88
87
  commands.push([cmd, description || '', isDefault, aliases])
89
88
  }
90
- self.getCommands = function () {
91
- return commands
92
- }
89
+ self.getCommands = () => commands
93
90
 
94
- var descriptions = {}
95
- self.describe = function (key, desc) {
91
+ let descriptions = {}
92
+ self.describe = function describe (key, desc) {
96
93
  if (typeof key === 'object') {
97
- Object.keys(key).forEach(function (k) {
94
+ Object.keys(key).forEach((k) => {
98
95
  self.describe(k, key[k])
99
96
  })
100
97
  } else {
101
98
  descriptions[key] = desc
102
99
  }
103
100
  }
104
- self.getDescriptions = function () {
105
- return descriptions
106
- }
101
+ self.getDescriptions = () => descriptions
107
102
 
108
- var epilog
109
- self.epilog = function (msg) {
103
+ let epilog
104
+ self.epilog = (msg) => {
110
105
  epilog = msg
111
106
  }
112
107
 
113
- var wrapSet = false
114
- var wrap
115
- self.wrap = function (cols) {
108
+ let wrapSet = false
109
+ let wrap
110
+ self.wrap = (cols) => {
116
111
  wrapSet = true
117
112
  wrap = cols
118
113
  }
@@ -126,41 +121,39 @@ module.exports = function (yargs, y18n) {
126
121
  return wrap
127
122
  }
128
123
 
129
- var deferY18nLookupPrefix = '__yargsString__:'
130
- self.deferY18nLookup = function (str) {
131
- return deferY18nLookupPrefix + str
132
- }
124
+ const deferY18nLookupPrefix = '__yargsString__:'
125
+ self.deferY18nLookup = str => deferY18nLookupPrefix + str
133
126
 
134
- var defaultGroup = 'Options:'
135
- self.help = function () {
127
+ const defaultGroup = 'Options:'
128
+ self.help = function help () {
136
129
  normalizeAliases()
137
130
 
138
131
  // handle old demanded API
139
- var demandedOptions = yargs.getDemandedOptions()
140
- var demandedCommands = yargs.getDemandedCommands()
141
- var groups = yargs.getGroups()
142
- var options = yargs.getOptions()
143
- var keys = Object.keys(
132
+ const demandedOptions = yargs.getDemandedOptions()
133
+ const demandedCommands = yargs.getDemandedCommands()
134
+ const groups = yargs.getGroups()
135
+ const options = yargs.getOptions()
136
+ let keys = Object.keys(
144
137
  Object.keys(descriptions)
145
138
  .concat(Object.keys(demandedOptions))
146
139
  .concat(Object.keys(demandedCommands))
147
140
  .concat(Object.keys(options.default))
148
- .reduce(function (acc, key) {
141
+ .reduce((acc, key) => {
149
142
  if (key !== '_') acc[key] = true
150
143
  return acc
151
144
  }, {})
152
145
  )
153
146
 
154
- var theWrap = getWrap()
155
- var ui = require('cliui')({
147
+ const theWrap = getWrap()
148
+ const ui = require('cliui')({
156
149
  width: theWrap,
157
150
  wrap: !!theWrap
158
151
  })
159
152
 
160
153
  // the usage string.
161
154
  if (usage) {
162
- var u = usage.replace(/\$0/g, yargs.$0)
163
- ui.div(u + '\n')
155
+ const u = usage.replace(/\$0/g, yargs.$0)
156
+ ui.div(`${u}\n`)
164
157
  }
165
158
 
166
159
  // your application's commands, i.e., non-option
@@ -168,15 +161,15 @@ module.exports = function (yargs, y18n) {
168
161
  if (commands.length) {
169
162
  ui.div(__('Commands:'))
170
163
 
171
- commands.forEach(function (command) {
164
+ commands.forEach((command) => {
172
165
  ui.span(
173
166
  {text: command[0], padding: [0, 2, 0, 2], width: maxWidth(commands, theWrap) + 4},
174
167
  {text: command[1]}
175
168
  )
176
- var hints = []
177
- if (command[2]) hints.push('[' + __('default:').slice(0, -1) + ']') // TODO hacking around i18n here
169
+ const hints = []
170
+ if (command[2]) hints.push(`[${__('default:').slice(0, -1)}]`) // TODO hacking around i18n here
178
171
  if (command[3] && command[3].length) {
179
- hints.push('[' + __('aliases:') + ' ' + command[3].join(', ') + ']')
172
+ hints.push(`[${__('aliases:')} ${command[3].join(', ')}]`)
180
173
  }
181
174
  if (hints.length) {
182
175
  ui.div({text: hints.join(' '), padding: [0, 0, 0, 2], align: 'right'})
@@ -190,14 +183,10 @@ module.exports = function (yargs, y18n) {
190
183
 
191
184
  // perform some cleanup on the keys array, making it
192
185
  // only include top-level keys not their aliases.
193
- var aliasKeys = (Object.keys(options.alias) || [])
186
+ const aliasKeys = (Object.keys(options.alias) || [])
194
187
  .concat(Object.keys(yargs.parsed.newAliases) || [])
195
188
 
196
- keys = keys.filter(function (key) {
197
- return !yargs.parsed.newAliases[key] && aliasKeys.every(function (alias) {
198
- return (options.alias[alias] || []).indexOf(key) === -1
199
- })
200
- })
189
+ keys = keys.filter(key => !yargs.parsed.newAliases[key] && aliasKeys.every(alias => (options.alias[alias] || []).indexOf(key) === -1))
201
190
 
202
191
  // populate 'Options:' group with any keys that have not
203
192
  // explicitly had a group set.
@@ -205,51 +194,49 @@ module.exports = function (yargs, y18n) {
205
194
  addUngroupedKeys(keys, options.alias, groups)
206
195
 
207
196
  // display 'Options:' table along with any custom tables:
208
- Object.keys(groups).forEach(function (groupName) {
197
+ Object.keys(groups).forEach((groupName) => {
209
198
  if (!groups[groupName].length) return
210
199
 
211
200
  ui.div(__(groupName))
212
201
 
213
202
  // if we've grouped the key 'f', but 'f' aliases 'foobar',
214
203
  // normalizedKeys should contain only 'foobar'.
215
- var normalizedKeys = groups[groupName].map(function (key) {
204
+ const normalizedKeys = groups[groupName].map((key) => {
216
205
  if (~aliasKeys.indexOf(key)) return key
217
- for (var i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
206
+ for (let i = 0, aliasKey; (aliasKey = aliasKeys[i]) !== undefined; i++) {
218
207
  if (~(options.alias[aliasKey] || []).indexOf(key)) return aliasKey
219
208
  }
220
209
  return key
221
210
  })
222
211
 
223
212
  // actually generate the switches string --foo, -f, --bar.
224
- var switches = normalizedKeys.reduce(function (acc, key) {
213
+ const switches = normalizedKeys.reduce((acc, key) => {
225
214
  acc[key] = [ key ].concat(options.alias[key] || [])
226
- .map(function (sw) {
227
- return (sw.length > 1 ? '--' : '-') + sw
228
- })
215
+ .map(sw => (sw.length > 1 ? '--' : '-') + sw)
229
216
  .join(', ')
230
217
 
231
218
  return acc
232
219
  }, {})
233
220
 
234
- normalizedKeys.forEach(function (key) {
235
- var kswitch = switches[key]
236
- var desc = descriptions[key] || ''
237
- var type = null
221
+ normalizedKeys.forEach((key) => {
222
+ const kswitch = switches[key]
223
+ let desc = descriptions[key] || ''
224
+ let type = null
238
225
 
239
226
  if (~desc.lastIndexOf(deferY18nLookupPrefix)) desc = __(desc.substring(deferY18nLookupPrefix.length))
240
227
 
241
- if (~options.boolean.indexOf(key)) type = '[' + __('boolean') + ']'
242
- if (~options.count.indexOf(key)) type = '[' + __('count') + ']'
243
- if (~options.string.indexOf(key)) type = '[' + __('string') + ']'
244
- if (~options.normalize.indexOf(key)) type = '[' + __('string') + ']'
245
- if (~options.array.indexOf(key)) type = '[' + __('array') + ']'
246
- if (~options.number.indexOf(key)) type = '[' + __('number') + ']'
228
+ if (~options.boolean.indexOf(key)) type = `[${__('boolean')}]`
229
+ if (~options.count.indexOf(key)) type = `[${__('count')}]`
230
+ if (~options.string.indexOf(key)) type = `[${__('string')}]`
231
+ if (~options.normalize.indexOf(key)) type = `[${__('string')}]`
232
+ if (~options.array.indexOf(key)) type = `[${__('array')}]`
233
+ if (~options.number.indexOf(key)) type = `[${__('number')}]`
247
234
 
248
- var extra = [
235
+ const extra = [
249
236
  type,
250
- (key in demandedOptions) ? '[' + __('required') + ']' : null,
251
- options.choices && options.choices[key] ? '[' + __('choices:') + ' ' +
252
- self.stringifiedValues(options.choices[key]) + ']' : null,
237
+ (key in demandedOptions) ? `[${__('required')}]` : null,
238
+ options.choices && options.choices[key] ? `[${__('choices:')} ${
239
+ self.stringifiedValues(options.choices[key])}]` : null,
253
240
  defaultString(options.default[key], options.defaultDescription[key])
254
241
  ].filter(Boolean).join(' ')
255
242
 
@@ -269,11 +256,11 @@ module.exports = function (yargs, y18n) {
269
256
  if (examples.length) {
270
257
  ui.div(__('Examples:'))
271
258
 
272
- examples.forEach(function (example) {
259
+ examples.forEach((example) => {
273
260
  example[0] = example[0].replace(/\$0/g, yargs.$0)
274
261
  })
275
262
 
276
- examples.forEach(function (example) {
263
+ examples.forEach((example) => {
277
264
  if (example[1] === '') {
278
265
  ui.div(
279
266
  {
@@ -299,8 +286,8 @@ module.exports = function (yargs, y18n) {
299
286
 
300
287
  // the usage string.
301
288
  if (epilog) {
302
- var e = epilog.replace(/\$0/g, yargs.$0)
303
- ui.div(e + '\n')
289
+ const e = epilog.replace(/\$0/g, yargs.$0)
290
+ ui.div(`${e}\n`)
304
291
  }
305
292
 
306
293
  return ui.toString()
@@ -309,17 +296,15 @@ module.exports = function (yargs, y18n) {
309
296
  // return the maximum width of a string
310
297
  // in the left-hand column of a table.
311
298
  function maxWidth (table, theWrap) {
312
- var width = 0
299
+ let width = 0
313
300
 
314
301
  // table might be of the form [leftColumn],
315
302
  // or {key: leftColumn}
316
303
  if (!Array.isArray(table)) {
317
- table = Object.keys(table).map(function (key) {
318
- return [table[key]]
319
- })
304
+ table = Object.keys(table).map(key => [table[key]])
320
305
  }
321
306
 
322
- table.forEach(function (v) {
307
+ table.forEach((v) => {
323
308
  width = Math.max(stringWidth(v[0]), width)
324
309
  })
325
310
 
@@ -334,11 +319,11 @@ module.exports = function (yargs, y18n) {
334
319
  // are copied to the keys being aliased.
335
320
  function normalizeAliases () {
336
321
  // handle old demanded API
337
- var demandedOptions = yargs.getDemandedOptions()
338
- var options = yargs.getOptions()
322
+ const demandedOptions = yargs.getDemandedOptions()
323
+ const options = yargs.getOptions()
339
324
 
340
- ;(Object.keys(options.alias) || []).forEach(function (key) {
341
- options.alias[key].forEach(function (alias) {
325
+ ;(Object.keys(options.alias) || []).forEach((key) => {
326
+ options.alias[key].forEach((alias) => {
342
327
  // copy descriptions.
343
328
  if (descriptions[alias]) self.describe(key, descriptions[alias])
344
329
  // copy demanded.
@@ -357,43 +342,41 @@ module.exports = function (yargs, y18n) {
357
342
  // given a set of keys, place any keys that are
358
343
  // ungrouped under the 'Options:' grouping.
359
344
  function addUngroupedKeys (keys, aliases, groups) {
360
- var groupedKeys = []
361
- var toCheck = null
362
- Object.keys(groups).forEach(function (group) {
345
+ let groupedKeys = []
346
+ let toCheck = null
347
+ Object.keys(groups).forEach((group) => {
363
348
  groupedKeys = groupedKeys.concat(groups[group])
364
349
  })
365
350
 
366
- keys.forEach(function (key) {
351
+ keys.forEach((key) => {
367
352
  toCheck = [key].concat(aliases[key])
368
- if (!toCheck.some(function (k) {
369
- return groupedKeys.indexOf(k) !== -1
370
- })) {
353
+ if (!toCheck.some(k => groupedKeys.indexOf(k) !== -1)) {
371
354
  groups[defaultGroup].push(key)
372
355
  }
373
356
  })
374
357
  return groupedKeys
375
358
  }
376
359
 
377
- self.showHelp = function (level) {
360
+ self.showHelp = (level) => {
378
361
  const logger = yargs._getLoggerInstance()
379
362
  if (!level) level = 'error'
380
- var emit = typeof level === 'function' ? level : logger[level]
363
+ const emit = typeof level === 'function' ? level : logger[level]
381
364
  emit(self.help())
382
365
  }
383
366
 
384
- self.functionDescription = function (fn) {
385
- var description = fn.name ? require('decamelize')(fn.name, '-') : __('generated-value')
367
+ self.functionDescription = (fn) => {
368
+ const description = fn.name ? require('decamelize')(fn.name, '-') : __('generated-value')
386
369
  return ['(', description, ')'].join('')
387
370
  }
388
371
 
389
- self.stringifiedValues = function (values, separator) {
390
- var string = ''
391
- var sep = separator || ', '
392
- var array = [].concat(values)
372
+ self.stringifiedValues = function stringifiedValues (values, separator) {
373
+ let string = ''
374
+ const sep = separator || ', '
375
+ const array = [].concat(values)
393
376
 
394
377
  if (!values || !array.length) return string
395
378
 
396
- array.forEach(function (value) {
379
+ array.forEach((value) => {
397
380
  if (string.length) string += sep
398
381
  string += JSON.stringify(value)
399
382
  })
@@ -404,7 +387,7 @@ module.exports = function (yargs, y18n) {
404
387
  // format the default-value-string displayed in
405
388
  // the right-hand column.
406
389
  function defaultString (value, defaultDescription) {
407
- var string = '[' + __('default:') + ' '
390
+ let string = `[${__('default:')} `
408
391
 
409
392
  if (value === undefined && !defaultDescription) return null
410
393
 
@@ -413,7 +396,7 @@ module.exports = function (yargs, y18n) {
413
396
  } else {
414
397
  switch (typeof value) {
415
398
  case 'string':
416
- string += JSON.stringify(value)
399
+ string += `"${value}"`
417
400
  break
418
401
  case 'object':
419
402
  string += JSON.stringify(value)
@@ -423,12 +406,12 @@ module.exports = function (yargs, y18n) {
423
406
  }
424
407
  }
425
408
 
426
- return string + ']'
409
+ return `${string}]`
427
410
  }
428
411
 
429
412
  // guess the width of the console window, max-width 80.
430
413
  function windowWidth () {
431
- var maxWidth = 80
414
+ const maxWidth = 80
432
415
  if (typeof process === 'object' && process.stdout && process.stdout.columns) {
433
416
  return Math.min(maxWidth, process.stdout.columns)
434
417
  } else {
@@ -437,18 +420,17 @@ module.exports = function (yargs, y18n) {
437
420
  }
438
421
 
439
422
  // logic for displaying application version.
440
- var version = null
441
- self.version = function (ver) {
423
+ let version = null
424
+ self.version = (ver) => {
442
425
  version = ver
443
426
  }
444
427
 
445
- self.showVersion = function () {
428
+ self.showVersion = () => {
446
429
  const logger = yargs._getLoggerInstance()
447
- if (typeof version === 'function') logger.log(version())
448
- else logger.log(version)
430
+ logger.log(version)
449
431
  }
450
432
 
451
- self.reset = function (localLookup) {
433
+ self.reset = function reset (localLookup) {
452
434
  // do not reset wrap here
453
435
  // do not reset fails here
454
436
  failMessage = null
@@ -457,14 +439,12 @@ module.exports = function (yargs, y18n) {
457
439
  epilog = undefined
458
440
  examples = []
459
441
  commands = []
460
- descriptions = objFilter(descriptions, function (k, v) {
461
- return !localLookup[k]
462
- })
442
+ descriptions = objFilter(descriptions, (k, v) => !localLookup[k])
463
443
  return self
464
444
  }
465
445
 
466
- var frozen
467
- self.freeze = function () {
446
+ let frozen
447
+ self.freeze = function freeze () {
468
448
  frozen = {}
469
449
  frozen.failMessage = failMessage
470
450
  frozen.failureOutput = failureOutput
@@ -474,7 +454,7 @@ module.exports = function (yargs, y18n) {
474
454
  frozen.commands = commands
475
455
  frozen.descriptions = descriptions
476
456
  }
477
- self.unfreeze = function () {
457
+ self.unfreeze = function unfreeze () {
478
458
  failMessage = frozen.failMessage
479
459
  failureOutput = frozen.failureOutput
480
460
  usage = frozen.usage