Skip to content

Commit

Permalink
feat: parse Number flags (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jun 26, 2020
1 parent ca2d593 commit b319c3a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/webpack-cli/__tests__/arg-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const basicOptions = [
type: Boolean,
description: 'boolean flag',
},
{
name: 'num-flag',
usage: '--num-flag <value>',
type: Number,
description: 'number flag',
},
{
name: 'string-flag',
usage: '--string-flag <value>',
Expand Down Expand Up @@ -98,16 +104,37 @@ describe('arg-parser', () => {
});

it('parses basic flags', () => {
const res = argParser(basicOptions, ['--bool-flag', '--string-flag', 'val'], true);
const res = argParser(basicOptions, ['--bool-flag', '--string-flag', 'val', '--num-flag', '100'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
numFlag: 100,
stringFlag: 'val',
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses number flags', () => {
const res = argParser(basicOptions, ['--num-flag', '100'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
numFlag: 100,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses number flags with = sign', () => {
const res = argParser(basicOptions, ['--num-flag=10'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
numFlag: 10,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('should not parse negated boolean flags which are not specified', () => {
const res = argParser(basicOptions, ['--no-bool-flag'], true);
expect(res.unknownArgs.includes('--no-bool-flag')).toBeTruthy();
Expand Down
2 changes: 2 additions & 0 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
const multiArg = (value, previous = []) => previous.concat([value]);
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue);
}
} else if (option.type === Number) {
parserInstance.option(flagsWithType, option.description, Number, option.defaultValue);
} else {
// in this case the type is a parsing function
if (option.type.length > 1) {
Expand Down

0 comments on commit b319c3a

Please sign in to comment.