Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/argument.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ class Argument {
* @package
*/

_concatValue(value, previous) {
_collectValue(value, previous) {
if (previous === this.defaultValue || !Array.isArray(previous)) {
return [value];
}

return previous.concat(value);
previous.push(value);
return previous;
}

/**
Expand Down Expand Up @@ -103,7 +104,7 @@ class Argument {
);
}
if (this.variadic) {
return this._concatValue(arg, previous);
return this._collectValue(arg, previous);
}
return arg;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
if (val !== null && option.parseArg) {
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
} else if (val !== null && option.variadic) {
val = option._concatValue(val, oldValue);
val = option._collectValue(val, oldValue);
}

// Fill-in appropriate missing values. Long winded but easy to follow.
Expand Down
7 changes: 4 additions & 3 deletions lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ class Option {
* @package
*/

_concatValue(value, previous) {
_collectValue(value, previous) {
if (previous === this.defaultValue || !Array.isArray(previous)) {
return [value];
}

return previous.concat(value);
previous.push(value);
return previous;
}

/**
Expand All @@ -186,7 +187,7 @@ class Option {
);
}
if (this.variadic) {
return this._concatValue(arg, previous);
return this._collectValue(arg, previous);
}
return arg;
};
Expand Down
13 changes: 13 additions & 0 deletions tests/argument.variadic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,17 @@ describe('variadic argument', () => {
program.parse(['one', 'two'], { from: 'user' });
expect(passedArg).toEqual(['one', 'two']);
});

test('when variadic has default array then specified value is used instead of default (not appended)', () => {
const program = new commander.Command();
let passedArg;
program
.addArgument(new commander.Argument('[value...]').default(['DEFAULT']))
.action((value) => {
passedArg = value;
});

program.parse(['one', 'two'], { from: 'user' });
expect(passedArg).toEqual(['one', 'two']);
});
});
8 changes: 8 additions & 0 deletions tests/options.variadic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ describe('variadic special cases', () => {

expect(program.options[0].variadic).toBeFalsy();
});

test('when option has default array then specified value is used instead of default (not appended)', () => {
const program = new commander.Command();
program.option('-c,--comma [value...]', 'values', ['default']);
program.parse(['--comma', 'CCC'], { from: 'user' });

expect(program.opts().comma).toEqual(['CCC']);
});
});
Loading