Skip to content

Commit

Permalink
feat: allow strict mode to be disabled (#840)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the first argument to strict() is now used to enable/disable its functionality, rather than controlling whether or not it is global.
  • Loading branch information
zodern authored and bcoe committed Apr 15, 2017
1 parent e7359d6 commit 6f78c05
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -1809,7 +1809,7 @@ Specify --help for available options
Specifies either a single option key (string), or an array of options.
If any of the options is present, yargs validation is skipped.

.strict([global=true])
.strict([enabled=true])
---------

Any command-line argument given that is not demanded, or does not have a
Expand Down
26 changes: 13 additions & 13 deletions test/command.js
Expand Up @@ -1000,19 +1000,6 @@ describe('Command', function () {
commandCalled.should.be.true
})

it('does not apply strict globally when passed value of `false`', function () {
var commandCalled = false
yargs('hi')
.strict(false)
.command('hi', 'The hi command', function (innerYargs) {
commandCalled = true
innerYargs.getStrict().should.be.false
})
yargs.getStrict().should.be.true
yargs.argv // parse and run command
commandCalled.should.be.true
})

// address regression introduced in #766, thanks @nexdrew!
it('does not fail strict check due to postional command arguments', function (done) {
yargs()
Expand All @@ -1037,6 +1024,19 @@ describe('Command', function () {
})
})

it('allows a command to override global`', function () {
var commandCalled = false
yargs('hi')
.strict()
.command('hi', 'The hi command', function (innerYargs) {
commandCalled = true
innerYargs.strict(false).getStrict().should.be.false
})
yargs.getStrict().should.be.true
yargs.argv // parse and run command
commandCalled.should.be.true
})

it('does not fire command if validation fails', function (done) {
var commandRun = false
yargs()
Expand Down
2 changes: 0 additions & 2 deletions test/yargs.js
Expand Up @@ -225,7 +225,6 @@ describe('yargs dsl tests', function () {
.implies('foo', 'snuh')
.conflicts('qux', 'xyzzy')
.group('foo', 'Group:')
.strict(false)
.exitProcess(false) // defaults to true.
.global('foo', false)
.global('qux', false)
Expand Down Expand Up @@ -266,7 +265,6 @@ describe('yargs dsl tests', function () {
expect(y.getValidationInstance().getConflicting()).to.deep.equal({})
expect(y.getCommandInstance().getCommandHandlers()).to.deep.equal({})
expect(y.getExitProcess()).to.equal(false)
expect(y.getStrict()).to.equal(false)
expect(y.getDemandedOptions()).to.deep.equal({})
expect(y.getDemandedCommands()).to.deep.equal({})
expect(y.getGroups()).to.deep.equal({})
Expand Down
9 changes: 3 additions & 6 deletions yargs.js
Expand Up @@ -126,7 +126,6 @@ function Yargs (processArgs, cwd, parentRequire) {
command = command ? command.reset() : Command(self, usage, validation)
if (!completion) completion = Completion(self, usage, command)

if (!strictGlobal) strict = false
completionCommand = null
output = ''
exitError = null
Expand Down Expand Up @@ -695,11 +694,9 @@ function Yargs (processArgs, cwd, parentRequire) {
}

var strict = false
var strictGlobal = false
self.strict = function (global) {
argsert('[boolean]', [global], arguments.length)
strict = true
strictGlobal = global !== false
self.strict = function (enabled) {
argsert('[boolean]', [enabled], arguments.length)
strict = enabled !== false
return self
}
self.getStrict = function () {
Expand Down

0 comments on commit 6f78c05

Please sign in to comment.