Skip to content

Commit f3f074b

Browse files
committed
fix: positional arguments of sub-commands threw strict() exception (#805)
1 parent a607061 commit f3f074b

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

.editorconfig

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/command.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ module.exports = function (yargs, usage, validation) {
224224

225225
// we apply validation post-hoc, so that custom
226226
// checks get passed populated positional arguments.
227-
yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error)
227+
if (!yargs._hasOutput()) yargs._runValidation(innerArgv, aliases, positionalMap, yargs.parsed.error)
228228

229229
if (commandHandler.handler && !yargs._hasOutput()) {
230+
yargs._setHasOutput()
230231
commandHandler.handler(innerArgv)
231232
}
232233

test/command.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,19 @@ describe('Command', function () {
10241024
})
10251025
})
10261026

1027+
// address https://github.com/yargs/yargs/issues/795
1028+
it('does not fail strict check due to postional command arguments in nested commands', function (done) {
1029+
yargs()
1030+
.strict()
1031+
.command('hi', 'The hi command', function (yargs) {
1032+
yargs.command('ben <age>', 'ben command', function () {}, function () {})
1033+
})
1034+
.parse('hi ben 99', function (err, argv, output) {
1035+
expect(err).to.equal(null)
1036+
return done()
1037+
})
1038+
})
1039+
10271040
it('does not fire command if validation fails', function (done) {
10281041
var commandRun = false
10291042
yargs()

yargs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ function Yargs (processArgs, cwd, parentRequire) {
897897
return hasOutput
898898
}
899899

900+
self._setHasOutput = function () {
901+
hasOutput = true
902+
}
903+
900904
var recommendCommands
901905
self.recommendCommands = function (recommend) {
902906
argsert('[boolean]', [recommend], arguments.length)

0 commit comments

Comments
 (0)