From 6f78c05a3a719f3c40e3b8197dd1ea2e85afd627 Mon Sep 17 00:00:00 2001 From: zodern Date: Sat, 15 Apr 2017 16:45:02 -0500 Subject: [PATCH] feat: allow strict mode to be disabled (#840) BREAKING CHANGE: the first argument to strict() is now used to enable/disable its functionality, rather than controlling whether or not it is global. --- README.md | 2 +- test/command.js | 26 +++++++++++++------------- test/yargs.js | 2 -- yargs.js | 9 +++------ 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 488791555..065476091 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/test/command.js b/test/command.js index 4d50af7d0..196b7832c 100644 --- a/test/command.js +++ b/test/command.js @@ -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() @@ -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() diff --git a/test/yargs.js b/test/yargs.js index ff3087112..5e46d8b58 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -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) @@ -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({}) diff --git a/yargs.js b/yargs.js index 92eb38f9e..559f17011 100644 --- a/yargs.js +++ b/yargs.js @@ -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 @@ -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 () {