yargs 11.1.0 → 12.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/CHANGELOG.md +39 -1
- package/README.md +6 -0
- package/lib/command.js +3 -1
- package/lib/levenshtein.js +2 -2
- package/lib/middleware.js +10 -0
- package/lib/usage.js +6 -2
- package/lib/validation.js +1 -1
- package/package.json +11 -11
- package/yargs.js +23 -22
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
<a name="12.0.1"></a>
|
|
6
|
+
## [12.0.1](https://github.com/yargs/yargs/compare/v12.0.0...v12.0.1) (2018-06-29)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
<a name="12.0.0"></a>
|
|
11
|
+
# [12.0.0](https://github.com/yargs/yargs/compare/v11.1.0...v12.0.0) (2018-06-26)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* .argv and .parse() now invoke identical code path ([#1126](https://github.com/yargs/yargs/issues/1126)) ([f13ebf4](https://github.com/yargs/yargs/commit/f13ebf4))
|
|
17
|
+
* remove the trailing white spaces from the help output ([#1090](https://github.com/yargs/yargs/issues/1090)) ([3f0746c](https://github.com/yargs/yargs/commit/3f0746c))
|
|
18
|
+
* **completion:** Avoid default command and recommendations during completion ([#1123](https://github.com/yargs/yargs/issues/1123)) ([036e7c5](https://github.com/yargs/yargs/commit/036e7c5))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Chores
|
|
22
|
+
|
|
23
|
+
* test Node.js 6, 8 and 10 ([#1160](https://github.com/yargs/yargs/issues/1160)) ([84f9d2b](https://github.com/yargs/yargs/commit/84f9d2b))
|
|
24
|
+
* upgrade to version of yargs-parser that does not populate value for unset boolean ([#1104](https://github.com/yargs/yargs/issues/1104)) ([d4705f4](https://github.com/yargs/yargs/commit/d4705f4))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Features
|
|
28
|
+
|
|
29
|
+
* add support for global middleware, useful for shared tasks like metrics ([#1119](https://github.com/yargs/yargs/issues/1119)) ([9d71ac7](https://github.com/yargs/yargs/commit/9d71ac7))
|
|
30
|
+
* allow setting scriptName $0 ([#1143](https://github.com/yargs/yargs/issues/1143)) ([a2f2eae](https://github.com/yargs/yargs/commit/a2f2eae))
|
|
31
|
+
* remove `setPlaceholderKeys` ([#1105](https://github.com/yargs/yargs/issues/1105)) ([6ee2c82](https://github.com/yargs/yargs/commit/6ee2c82))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### BREAKING CHANGES
|
|
35
|
+
|
|
36
|
+
* Options absent from `argv` (not set via CLI argument) are now absent from the parsed result object rather than being set with `undefined`
|
|
37
|
+
* drop Node 4 from testing matrix, such that we'll gradually start drifting away from supporting Node 4.
|
|
38
|
+
* yargs-parser does not populate 'false' when boolean flag is not passed
|
|
39
|
+
* tests that assert against help output will need to be updated
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
5
43
|
<a name="11.1.0"></a>
|
|
6
44
|
# [11.1.0](https://github.com/yargs/yargs/compare/v11.0.0...v11.1.0) (2018-03-04)
|
|
7
45
|
|
|
@@ -653,7 +691,7 @@ All notable changes to this project will be documented in this file. See [standa
|
|
|
653
691
|
- [#308](https://github.com/bcoe/yargs/pull/308) Yargs now handles environment variables (@nexdrew)
|
|
654
692
|
- [#302](https://github.com/bcoe/yargs/pull/302) Add Indonesian translation (@rilut)
|
|
655
693
|
- [#300](https://github.com/bcoe/yargs/pull/300) Add Turkish translation (@feyzo)
|
|
656
|
-
- [#298](https://github.com/bcoe/yargs/pull/298) Add Norwegian
|
|
694
|
+
- [#298](https://github.com/bcoe/yargs/pull/298) Add Norwegian Bokmål translation (@sindresorhus)
|
|
657
695
|
- [#297](https://github.com/bcoe/yargs/pull/297) Fix for layout of cjk characters (@disjukr)
|
|
658
696
|
- [#296](https://github.com/bcoe/yargs/pull/296) Add Korean translation (@disjukr)
|
|
659
697
|
|
package/README.md
CHANGED
|
@@ -30,10 +30,16 @@ It gives you:
|
|
|
30
30
|
|
|
31
31
|
## Installation
|
|
32
32
|
|
|
33
|
+
Stable version:
|
|
33
34
|
```bash
|
|
34
35
|
npm i yargs --save
|
|
35
36
|
```
|
|
36
37
|
|
|
38
|
+
Bleeding edge version with the most recent features:
|
|
39
|
+
```bash
|
|
40
|
+
npm i yargs@next --save
|
|
41
|
+
```
|
|
42
|
+
|
|
37
43
|
## Usage :
|
|
38
44
|
|
|
39
45
|
### Simple Example
|
package/lib/command.js
CHANGED
|
@@ -9,15 +9,17 @@ const DEFAULT_MARKER = /(^\*)|(^\$0)/
|
|
|
9
9
|
// handles parsing positional arguments,
|
|
10
10
|
// and populating argv with said positional
|
|
11
11
|
// arguments.
|
|
12
|
-
module.exports = function command (yargs, usage, validation) {
|
|
12
|
+
module.exports = function command (yargs, usage, validation, globalMiddleware) {
|
|
13
13
|
const self = {}
|
|
14
14
|
let handlers = {}
|
|
15
15
|
let aliasMap = {}
|
|
16
16
|
let defaultCommand
|
|
17
|
+
globalMiddleware = globalMiddleware || []
|
|
17
18
|
self.addHandler = function addHandler (cmd, description, builder, handler, middlewares) {
|
|
18
19
|
let aliases = []
|
|
19
20
|
handler = handler || (() => {})
|
|
20
21
|
middlewares = middlewares || []
|
|
22
|
+
middlewares = globalMiddleware.concat(middlewares)
|
|
21
23
|
if (Array.isArray(cmd)) {
|
|
22
24
|
aliases = cmd.slice(1)
|
|
23
25
|
cmd = cmd[0]
|
package/lib/levenshtein.js
CHANGED
|
@@ -37,8 +37,8 @@ module.exports = function levenshtein (a, b) {
|
|
|
37
37
|
matrix[i][j] = matrix[i - 1][j - 1]
|
|
38
38
|
} else {
|
|
39
39
|
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
Math.min(matrix[i][j - 1] + 1, // insertion
|
|
41
|
+
matrix[i - 1][j] + 1)) // deletion
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module.exports = function (globalMiddleware, context) {
|
|
2
|
+
return function (callback) {
|
|
3
|
+
if (Array.isArray(callback)) {
|
|
4
|
+
Array.prototype.push.apply(globalMiddleware, callback)
|
|
5
|
+
} else if (typeof callback === 'object') {
|
|
6
|
+
globalMiddleware.push(callback)
|
|
7
|
+
}
|
|
8
|
+
return context
|
|
9
|
+
}
|
|
10
|
+
}
|
package/lib/usage.js
CHANGED
|
@@ -45,7 +45,10 @@ module.exports = function usage (yargs, y18n) {
|
|
|
45
45
|
// don't output failure message more than once
|
|
46
46
|
if (!failureOutput) {
|
|
47
47
|
failureOutput = true
|
|
48
|
-
if (showHelpOnFail)
|
|
48
|
+
if (showHelpOnFail) {
|
|
49
|
+
yargs.showHelp('error')
|
|
50
|
+
logger.error()
|
|
51
|
+
}
|
|
49
52
|
if (msg || err) logger.error(msg || err)
|
|
50
53
|
if (failMessage) {
|
|
51
54
|
if (msg || err) logger.error('')
|
|
@@ -346,7 +349,8 @@ module.exports = function usage (yargs, y18n) {
|
|
|
346
349
|
ui.div(`${e}\n`)
|
|
347
350
|
}
|
|
348
351
|
|
|
349
|
-
|
|
352
|
+
// Remove the trailing white spaces
|
|
353
|
+
return ui.toString().replace(/\s*$/, '')
|
|
350
354
|
}
|
|
351
355
|
|
|
352
356
|
// return the maximum width of a string
|
package/lib/validation.js
CHANGED
|
@@ -37,7 +37,7 @@ module.exports = function validation (yargs, usage, y18n) {
|
|
|
37
37
|
)
|
|
38
38
|
} else {
|
|
39
39
|
usage.fail(
|
|
40
|
-
|
|
40
|
+
__('Too many non-option arguments: got %s, maximum of %s', _s, demandedCommands._.max)
|
|
41
41
|
)
|
|
42
42
|
}
|
|
43
43
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yargs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.1",
|
|
4
4
|
"description": "yargs the modern, pirate-themed, successor to optimist.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"files": [
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"cliui": "^4.0.0",
|
|
16
|
-
"decamelize": "^
|
|
17
|
-
"find-up": "^
|
|
16
|
+
"decamelize": "^2.0.0",
|
|
17
|
+
"find-up": "^3.0.0",
|
|
18
18
|
"get-caller-file": "^1.0.1",
|
|
19
19
|
"os-locale": "^2.0.0",
|
|
20
20
|
"require-directory": "^2.1.1",
|
|
@@ -22,27 +22,27 @@
|
|
|
22
22
|
"set-blocking": "^2.0.0",
|
|
23
23
|
"string-width": "^2.0.0",
|
|
24
24
|
"which-module": "^2.0.0",
|
|
25
|
-
"y18n": "^3.2.1",
|
|
26
|
-
"yargs-parser": "^
|
|
25
|
+
"y18n": "^3.2.1 || ^4.0.0",
|
|
26
|
+
"yargs-parser": "^10.1.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"chai": "^4.1.2",
|
|
30
30
|
"chalk": "^1.1.3",
|
|
31
|
-
"coveralls": "^
|
|
31
|
+
"coveralls": "^3.0.1",
|
|
32
32
|
"cpr": "^2.0.0",
|
|
33
33
|
"cross-spawn": "^6.0.4",
|
|
34
34
|
"es6-promise": "^4.0.2",
|
|
35
35
|
"hashish": "0.0.4",
|
|
36
|
-
"mocha": "^
|
|
37
|
-
"nyc": "^11.
|
|
36
|
+
"mocha": "^5.1.1",
|
|
37
|
+
"nyc": "^11.7.3",
|
|
38
38
|
"rimraf": "^2.5.0",
|
|
39
|
-
"standard": "^
|
|
39
|
+
"standard": "^11.0.1",
|
|
40
40
|
"standard-version": "^4.2.0",
|
|
41
41
|
"which": "^1.2.9",
|
|
42
42
|
"yargs-test-extends": "^1.0.1"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
|
-
"
|
|
45
|
+
"pretest": "standard",
|
|
46
46
|
"test": "nyc --cache mocha --require ./test/before.js --timeout=8000 --check-leaks",
|
|
47
47
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
|
48
48
|
"release": "standard-version"
|
|
@@ -68,6 +68,6 @@
|
|
|
68
68
|
],
|
|
69
69
|
"license": "MIT",
|
|
70
70
|
"engine": {
|
|
71
|
-
"node": ">=
|
|
71
|
+
"node": ">=6"
|
|
72
72
|
}
|
|
73
73
|
}
|
package/yargs.js
CHANGED
|
@@ -11,6 +11,7 @@ const Y18n = require('y18n')
|
|
|
11
11
|
const objFilter = require('./lib/obj-filter')
|
|
12
12
|
const setBlocking = require('set-blocking')
|
|
13
13
|
const applyExtends = require('./lib/apply-extends')
|
|
14
|
+
const middlewareFactory = require('./lib/middleware')
|
|
14
15
|
const YError = require('./lib/yerror')
|
|
15
16
|
|
|
16
17
|
exports = module.exports = Yargs
|
|
@@ -21,6 +22,7 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
21
22
|
let command = null
|
|
22
23
|
let completion = null
|
|
23
24
|
let groups = {}
|
|
25
|
+
let globalMiddleware = []
|
|
24
26
|
let output = ''
|
|
25
27
|
let preservedGroups = {}
|
|
26
28
|
let usage = null
|
|
@@ -31,8 +33,15 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
31
33
|
updateFiles: false
|
|
32
34
|
})
|
|
33
35
|
|
|
36
|
+
self.middleware = middlewareFactory(globalMiddleware, self)
|
|
37
|
+
|
|
34
38
|
if (!cwd) cwd = process.cwd()
|
|
35
39
|
|
|
40
|
+
self.scriptName = function scriptName (scriptName) {
|
|
41
|
+
self.$0 = scriptName
|
|
42
|
+
return self
|
|
43
|
+
}
|
|
44
|
+
|
|
36
45
|
self.$0 = process.argv
|
|
37
46
|
.slice(0, 2)
|
|
38
47
|
.map((x, i) => {
|
|
@@ -117,7 +126,7 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
117
126
|
// instances of all our helpers -- otherwise just reset.
|
|
118
127
|
usage = usage ? usage.reset(localLookup) : Usage(self, y18n)
|
|
119
128
|
validation = validation ? validation.reset(localLookup) : Validation(self, usage, y18n)
|
|
120
|
-
command = command ? command.reset() : Command(self, usage, validation)
|
|
129
|
+
command = command ? command.reset() : Command(self, usage, validation, globalMiddleware)
|
|
121
130
|
if (!completion) completion = Completion(self, usage, command)
|
|
122
131
|
|
|
123
132
|
completionCommand = null
|
|
@@ -525,7 +534,9 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
525
534
|
let parseContext = null
|
|
526
535
|
self.parse = function parse (args, shortCircuit, _parseFn) {
|
|
527
536
|
argsert('[string|array] [function|boolean|object] [function]', [args, shortCircuit, _parseFn], arguments.length)
|
|
528
|
-
if (typeof args === 'undefined')
|
|
537
|
+
if (typeof args === 'undefined') {
|
|
538
|
+
return self._parseArgs(processArgs)
|
|
539
|
+
}
|
|
529
540
|
|
|
530
541
|
// a context object can optionally be provided, this allows
|
|
531
542
|
// additional information to be passed to a command handler.
|
|
@@ -1030,8 +1041,11 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1030
1041
|
argv[helpOpt] = true
|
|
1031
1042
|
}
|
|
1032
1043
|
}
|
|
1044
|
+
|
|
1033
1045
|
const handlerKeys = command.getCommands()
|
|
1034
|
-
const
|
|
1046
|
+
const requestCompletions = completion.completionKey in argv
|
|
1047
|
+
const skipRecommendation = argv[helpOpt] || requestCompletions
|
|
1048
|
+
const skipDefaultCommand = skipRecommendation && (handlerKeys.length > 1 || handlerKeys[0] !== '$0')
|
|
1035
1049
|
|
|
1036
1050
|
if (argv._.length) {
|
|
1037
1051
|
if (handlerKeys.length) {
|
|
@@ -1039,7 +1053,6 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1039
1053
|
for (let i = (commandIndex || 0), cmd; argv._[i] !== undefined; i++) {
|
|
1040
1054
|
cmd = String(argv._[i])
|
|
1041
1055
|
if (~handlerKeys.indexOf(cmd) && cmd !== completionCommand) {
|
|
1042
|
-
setPlaceholderKeys(argv)
|
|
1043
1056
|
// commands are executed using a recursive algorithm that executes
|
|
1044
1057
|
// the deepest command first; we keep track of the position in the
|
|
1045
1058
|
// argv._ array that is currently being executed.
|
|
@@ -1052,31 +1065,29 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1052
1065
|
|
|
1053
1066
|
// run the default command, if defined
|
|
1054
1067
|
if (command.hasDefaultCommand() && !skipDefaultCommand) {
|
|
1055
|
-
setPlaceholderKeys(argv)
|
|
1056
1068
|
return command.runCommand(null, self, parsed)
|
|
1057
1069
|
}
|
|
1058
1070
|
|
|
1059
1071
|
// recommend a command if recommendCommands() has
|
|
1060
1072
|
// been enabled, and no commands were found to execute
|
|
1061
|
-
if (recommendCommands && firstUnknownCommand && !
|
|
1073
|
+
if (recommendCommands && firstUnknownCommand && !skipRecommendation) {
|
|
1062
1074
|
validation.recommendCommands(firstUnknownCommand, handlerKeys)
|
|
1063
1075
|
}
|
|
1064
1076
|
}
|
|
1065
1077
|
|
|
1066
1078
|
// generate a completion script for adding to ~/.bashrc.
|
|
1067
|
-
if (completionCommand && ~argv._.indexOf(completionCommand) && !
|
|
1079
|
+
if (completionCommand && ~argv._.indexOf(completionCommand) && !requestCompletions) {
|
|
1068
1080
|
if (exitProcess) setBlocking(true)
|
|
1069
1081
|
self.showCompletionScript()
|
|
1070
1082
|
self.exit(0)
|
|
1071
1083
|
}
|
|
1072
1084
|
} else if (command.hasDefaultCommand() && !skipDefaultCommand) {
|
|
1073
|
-
setPlaceholderKeys(argv)
|
|
1074
1085
|
return command.runCommand(null, self, parsed)
|
|
1075
1086
|
}
|
|
1076
1087
|
|
|
1077
1088
|
// we must run completions first, a user might
|
|
1078
1089
|
// want to complete the --help or --version option.
|
|
1079
|
-
if (
|
|
1090
|
+
if (requestCompletions) {
|
|
1080
1091
|
if (exitProcess) setBlocking(true)
|
|
1081
1092
|
|
|
1082
1093
|
// we allow for asynchronous completions,
|
|
@@ -1089,7 +1100,7 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1089
1100
|
|
|
1090
1101
|
self.exit(0)
|
|
1091
1102
|
})
|
|
1092
|
-
return
|
|
1103
|
+
return argv
|
|
1093
1104
|
}
|
|
1094
1105
|
|
|
1095
1106
|
// Handle 'help' and 'version' options
|
|
@@ -1124,7 +1135,7 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1124
1135
|
|
|
1125
1136
|
// if we're executed via bash completion, don't
|
|
1126
1137
|
// bother with validation.
|
|
1127
|
-
if (!
|
|
1138
|
+
if (!requestCompletions) {
|
|
1128
1139
|
self._runValidation(argv, aliases, {}, parsed.error)
|
|
1129
1140
|
}
|
|
1130
1141
|
}
|
|
@@ -1133,7 +1144,7 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1133
1144
|
else throw err
|
|
1134
1145
|
}
|
|
1135
1146
|
|
|
1136
|
-
return
|
|
1147
|
+
return argv
|
|
1137
1148
|
}
|
|
1138
1149
|
|
|
1139
1150
|
self._runValidation = function runValidation (argv, aliases, positionalMap, parseErrors) {
|
|
@@ -1159,16 +1170,6 @@ function Yargs (processArgs, cwd, parentRequire) {
|
|
|
1159
1170
|
}
|
|
1160
1171
|
}
|
|
1161
1172
|
|
|
1162
|
-
function setPlaceholderKeys (argv) {
|
|
1163
|
-
Object.keys(options.key).forEach((key) => {
|
|
1164
|
-
// don't set placeholder keys for dot
|
|
1165
|
-
// notation options 'foo.bar'.
|
|
1166
|
-
if (~key.indexOf('.')) return
|
|
1167
|
-
if (typeof argv[key] === 'undefined') argv[key] = undefined
|
|
1168
|
-
})
|
|
1169
|
-
return argv
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
1173
|
// an app should almost always have --version and --help,
|
|
1173
1174
|
// if you *really* want to disable this use .help(false)/.version(false).
|
|
1174
1175
|
self.help()
|