Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,10 @@ export class YargsParser {
if (!isUndefined(argAfterEqualSign)) {
error = Error(__('Argument unexpected for: %s', key));
}
setArg(key, defaultValue(key));
// An option configured to take no arguments is a switch, so its value
// is the one implied by its type. The configured default applies when
// the option is absent, not when it is given without a value.
setArg(key, defaultForType(guessType(key)));
return i;
}

Expand Down
21 changes: 21 additions & 0 deletions test/yargs-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ describe('yargs-parser', function () {
parse.should.have.property('_').and.deep.equal(['false', 7, 'foo']);
});

it('should not use the configured default for an option given with zero narg', function () {
const parse = parser(['-a', 'hello', '-b', 'world'], {
narg: {a: 1, b: 0},
default: {b: false},
alias: {blah: ['b', 'bl']},
});
parse.should.have.property('a', 'hello');
parse.should.have.property('b', true).and.be.a('boolean');
parse.should.have.property('blah', true).and.be.a('boolean');
parse.should.have.property('bl', true).and.be.a('boolean');
parse.should.have.property('_').and.deep.equal(['world']);
});

it('should use the configured default for an option with zero narg that is not given', function () {
const parse = parser([], {
narg: {b: 0},
default: {b: false},
});
parse.should.have.property('b', false).and.be.a('boolean');
});

it('should allow defining options as boolean in groups', function () {
const parse = parser(['-x', '-z', 'one', 'two', 'three'], {
boolean: ['x', 'y', 'z'],
Expand Down