-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implicit boolean option (#109)
Implicit boolean option support A option which doesn't take a value is called as an implicit boolean option. A typical implicit boolean option is defined like below: ``` program .command('command', 'Command') .option('-f, --flag', 'Flag') ... ``` The default value of any implicit boolean option is always `true` even though a value other than `false` is explicitly specified as its default value. There is no meaning to specify arguments other than `synopsis` and `description`. Because: * `validator`: The option has no value to be validate * `defaultValue`: The default value is always `false` * `required`: Setting it `true` always makes the option value `true`
- Loading branch information
Showing
7 changed files
with
148 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"use strict"; | ||
|
||
/* global Program, logger, should, makeArgv, sinon */ | ||
|
||
describe('Issue #107 - Implicit boolean option', () => { | ||
context('having the shorthand and the longhand', () => { | ||
beforeEach(() => { | ||
this.program = new Program(); | ||
this.action = sinon.spy(); | ||
this.program | ||
.logger(logger) | ||
.version('1.0.0') | ||
.command('cmd', 'Command') | ||
.option('-b, --bool', 'Implicit boolean') | ||
.argument('[a]', 'A', this.program.INT) | ||
.action(this.action); | ||
this.fatalError = sinon.stub(this.program, "fatalError"); | ||
}); | ||
|
||
afterEach(() => { | ||
this.fatalError.restore(); | ||
this.program.reset(); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {bool: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '-b', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {bool: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {} and {bool: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '-b'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({}, {bool: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {bool: false}`, () => { | ||
this.program.parse(makeArgv(['cmd', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {bool: false}, logger)); | ||
}); | ||
}); | ||
|
||
context('only having the longhand', () => { | ||
beforeEach(() => { | ||
this.program = new Program(); | ||
this.action = sinon.spy(); | ||
this.program | ||
.logger(logger) | ||
.version('1.0.0') | ||
.command('cmd', 'Command') | ||
.option('--bool', 'Implicit boolean') | ||
.argument('[a]', 'A', this.program.INT) | ||
.action(this.action); | ||
this.fatalError = sinon.stub(this.program, "fatalError"); | ||
}); | ||
|
||
afterEach(() => { | ||
this.fatalError.restore(); | ||
this.program.reset(); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {bool: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '--bool', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {bool: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {} and {bool: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '--bool'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({}, {bool: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {bool: false}`, () => { | ||
this.program.parse(makeArgv(['cmd', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {bool: false}, logger)); | ||
}); | ||
}); | ||
|
||
context('only having the shorthand', () => { | ||
beforeEach(() => { | ||
this.program = new Program(); | ||
this.action = sinon.spy(); | ||
this.program | ||
.logger(logger) | ||
.version('1.0.0') | ||
.command('cmd', 'Command') | ||
.option('-b', 'Implicit boolean') | ||
.argument('[a]', 'A', this.program.INT) | ||
.action(this.action); | ||
this.fatalError = sinon.stub(this.program, "fatalError"); | ||
}); | ||
|
||
afterEach(() => { | ||
this.fatalError.restore(); | ||
this.program.reset(); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {b: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '-b', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {b: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {} and {b: true}`, () => { | ||
this.program.parse(makeArgv(['cmd', '-b'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({}, {b: true}, logger)); | ||
}); | ||
|
||
it(`should call the action with {a: 1} and {b: false}`, () => { | ||
this.program.parse(makeArgv(['cmd', '1'])); | ||
should(this.fatalError.callCount).eql(0); | ||
should(this.action.callCount).eql(1); | ||
should(this.action.calledWith({a: 1}, {b: false}, logger)); | ||
}); | ||
}); | ||
}); |