Skip to content

Commit

Permalink
feat: allow only specified negated flags
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jun 5, 2020
1 parent 3e02d12 commit b5ce268
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
49 changes: 33 additions & 16 deletions packages/webpack-cli/__tests__/arg-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const basicOptions = [
type: Boolean,
description: 'boolean flag',
},
{
name: 'specific-bool',
alias: 's',
usage: '--specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'no-specific-bool',
usage: '--no-specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'string-flag',
usage: '--string-flag <value>',
Expand Down Expand Up @@ -82,57 +95,62 @@ describe('arg-parser', () => {
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses negated boolean flags', () => {
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();
});

it('parses boolean flag alias', () => {
const res = argParser(basicOptions, ['-b'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: false,
boolFlag: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses boolean flag alias', () => {
const res = argParser(basicOptions, ['-b'], true);
it('parses specified negated boolean flag', () => {
const res = argParser(basicOptions, ['--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('warns on usage of both flag and same negated flag, setting it to false', () => {
const res = argParser(basicOptions, ['--bool-flag', '--no-bool-flag'], true);
const res = argParser(basicOptions, ['--specific-bool', '--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: false,
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-bool-flag', '--bool-flag'], true);
const res = argParser(basicOptions, ['--no-specific-bool', '--specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag alias and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-bool-flag', '-b'], true);
const res = argParser(basicOptions, ['--no-specific-bool', '-s'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both -b and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both -s and --no-specific-bool');
});

it('parses string flag using equals sign', () => {
Expand Down Expand Up @@ -190,12 +208,11 @@ describe('arg-parser', () => {
});

it('parses webpack args', () => {
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/', '--no-watch'], true);
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts.entry).toEqual('test.js');
expect(res.opts.hot).toBeTruthy();
expect(res.opts.output).toEqual('./dist/');
expect(res.opts.watch).toBeFalsy();
expect(warnMock.mock.calls.length).toEqual(0);
});
});
5 changes: 0 additions & 5 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
const flagsWithType = option.type !== Boolean ? flags + ' <value>' : flags;
if (option.type === Boolean || option.type === String) {
parserInstance.option(flagsWithType, option.description, option.defaultValue);
if (option.type === Boolean) {
// commander requires explicitly adding the negated version of boolean flags
const negatedFlag = `--no-${option.name}`;
parserInstance.option(negatedFlag, `negates ${option.name}`);
}
} else {
// in this case the type is a parsing function
parserInstance.option(flagsWithType, option.description, option.type, option.defaultValue);
Expand Down

0 comments on commit b5ce268

Please sign in to comment.