Skip to content

Commit b319c3a

Browse files
authored
feat: parse Number flags (#1652)
1 parent ca2d593 commit b319c3a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/webpack-cli/__tests__/arg-parser.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const basicOptions = [
3030
type: Boolean,
3131
description: 'boolean flag',
3232
},
33+
{
34+
name: 'num-flag',
35+
usage: '--num-flag <value>',
36+
type: Number,
37+
description: 'number flag',
38+
},
3339
{
3440
name: 'string-flag',
3541
usage: '--string-flag <value>',
@@ -98,16 +104,37 @@ describe('arg-parser', () => {
98104
});
99105

100106
it('parses basic flags', () => {
101-
const res = argParser(basicOptions, ['--bool-flag', '--string-flag', 'val'], true);
107+
const res = argParser(basicOptions, ['--bool-flag', '--string-flag', 'val', '--num-flag', '100'], true);
102108
expect(res.unknownArgs.length).toEqual(0);
103109
expect(res.opts).toEqual({
104110
boolFlag: true,
111+
numFlag: 100,
105112
stringFlag: 'val',
106113
stringFlagWithDefault: 'default-value',
107114
});
108115
expect(warnMock.mock.calls.length).toEqual(0);
109116
});
110117

118+
it('parses number flags', () => {
119+
const res = argParser(basicOptions, ['--num-flag', '100'], true);
120+
expect(res.unknownArgs.length).toEqual(0);
121+
expect(res.opts).toEqual({
122+
numFlag: 100,
123+
stringFlagWithDefault: 'default-value',
124+
});
125+
expect(warnMock.mock.calls.length).toEqual(0);
126+
});
127+
128+
it('parses number flags with = sign', () => {
129+
const res = argParser(basicOptions, ['--num-flag=10'], true);
130+
expect(res.unknownArgs.length).toEqual(0);
131+
expect(res.opts).toEqual({
132+
numFlag: 10,
133+
stringFlagWithDefault: 'default-value',
134+
});
135+
expect(warnMock.mock.calls.length).toEqual(0);
136+
});
137+
111138
it('should not parse negated boolean flags which are not specified', () => {
112139
const res = argParser(basicOptions, ['--no-bool-flag'], true);
113140
expect(res.unknownArgs.includes('--no-bool-flag')).toBeTruthy();

packages/webpack-cli/lib/utils/arg-parser.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
4545
const multiArg = (value, previous = []) => previous.concat([value]);
4646
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue);
4747
}
48+
} else if (option.type === Number) {
49+
parserInstance.option(flagsWithType, option.description, Number, option.defaultValue);
4850
} else {
4951
// in this case the type is a parsing function
5052
if (option.type.length > 1) {

0 commit comments

Comments
 (0)