Skip to content

Commit c635fad

Browse files
authored
Collect variadic with push, add tests (#2410)
1 parent 201d932 commit c635fad

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

lib/argument.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ class Argument {
5353
* @package
5454
*/
5555

56-
_concatValue(value, previous) {
56+
_collectValue(value, previous) {
5757
if (previous === this.defaultValue || !Array.isArray(previous)) {
5858
return [value];
5959
}
6060

61-
return previous.concat(value);
61+
previous.push(value);
62+
return previous;
6263
}
6364

6465
/**
@@ -103,7 +104,7 @@ class Argument {
103104
);
104105
}
105106
if (this.variadic) {
106-
return this._concatValue(arg, previous);
107+
return this._collectValue(arg, previous);
107108
}
108109
return arg;
109110
};

lib/command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
701701
if (val !== null && option.parseArg) {
702702
val = this._callParseArg(option, val, oldValue, invalidValueMessage);
703703
} else if (val !== null && option.variadic) {
704-
val = option._concatValue(val, oldValue);
704+
val = option._collectValue(val, oldValue);
705705
}
706706

707707
// Fill-in appropriate missing values. Long winded but easy to follow.

lib/option.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ class Option {
162162
* @package
163163
*/
164164

165-
_concatValue(value, previous) {
165+
_collectValue(value, previous) {
166166
if (previous === this.defaultValue || !Array.isArray(previous)) {
167167
return [value];
168168
}
169169

170-
return previous.concat(value);
170+
previous.push(value);
171+
return previous;
171172
}
172173

173174
/**
@@ -186,7 +187,7 @@ class Option {
186187
);
187188
}
188189
if (this.variadic) {
189-
return this._concatValue(arg, previous);
190+
return this._collectValue(arg, previous);
190191
}
191192
return arg;
192193
};

tests/argument.variadic.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,17 @@ describe('variadic argument', () => {
101101
program.parse(['one', 'two'], { from: 'user' });
102102
expect(passedArg).toEqual(['one', 'two']);
103103
});
104+
105+
test('when variadic has default array then specified value is used instead of default (not appended)', () => {
106+
const program = new commander.Command();
107+
let passedArg;
108+
program
109+
.addArgument(new commander.Argument('[value...]').default(['DEFAULT']))
110+
.action((value) => {
111+
passedArg = value;
112+
});
113+
114+
program.parse(['one', 'two'], { from: 'user' });
115+
expect(passedArg).toEqual(['one', 'two']);
116+
});
104117
});

tests/options.variadic.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,12 @@ describe('variadic special cases', () => {
162162

163163
expect(program.options[0].variadic).toBeFalsy();
164164
});
165+
166+
test('when option has default array then specified value is used instead of default (not appended)', () => {
167+
const program = new commander.Command();
168+
program.option('-c,--comma [value...]', 'values', ['default']);
169+
program.parse(['--comma', 'CCC'], { from: 'user' });
170+
171+
expect(program.opts().comma).toEqual(['CCC']);
172+
});
165173
});

0 commit comments

Comments
 (0)