Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow command to override global strict #840

Merged
merged 3 commits into from Apr 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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