Skip to content

Commit c3ebc11

Browse files
committed
feat(webpack-cli): allow negative property for cli-flags
1 parent 62d77da commit c3ebc11

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ const basicOptions = [
3636
type: Number,
3737
description: 'number flag',
3838
},
39+
{
40+
name: 'neg-flag',
41+
alias: 'b',
42+
usage: '--neg-flag',
43+
type: Boolean,
44+
negative: true,
45+
description: 'boolean flag',
46+
},
3947
{
4048
name: 'string-flag',
4149
usage: '--string-flag <value>',
@@ -160,6 +168,26 @@ describe('arg-parser', () => {
160168
expect(warnMock.mock.calls.length).toEqual(0);
161169
});
162170

171+
it('parses --neg-flag', () => {
172+
const res = argParser(basicOptions, ['--neg-flag'], true);
173+
expect(res.unknownArgs.length).toEqual(0);
174+
expect(res.opts).toEqual({
175+
negFlag: true,
176+
stringFlagWithDefault: 'default-value',
177+
});
178+
expect(warnMock.mock.calls.length).toEqual(0);
179+
});
180+
181+
it('parses --no-neg-flag', () => {
182+
const res = argParser(basicOptions, ['--no-neg-flag'], true);
183+
expect(res.unknownArgs.length).toEqual(0);
184+
expect(res.opts).toEqual({
185+
negFlag: false,
186+
stringFlagWithDefault: 'default-value',
187+
});
188+
expect(warnMock.mock.calls.length).toEqual(0);
189+
});
190+
163191
it('parses multi type flag as Boolean', () => {
164192
const res = argParser(basicOptions, ['--multi-type'], true);
165193
expect(res.unknownArgs.length).toEqual(0);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
6363
if (!option.multiple) {
6464
// Prevent default behavior for standalone options
6565
parserInstance.option(flagsWithType, option.description, option.defaultValue).action(() => {});
66+
if (option.negative) {
67+
// commander requires explicitly adding the negated version of boolean flags
68+
const negatedFlag = `--no-${option.name}`;
69+
parserInstance.option(negatedFlag, `negates ${option.name}`);
70+
}
6671
} else {
6772
const multiArg = (value, previous = []) => previous.concat([value]);
6873
parserInstance.option(flagsWithType, option.description, multiArg, option.defaultValue).action(() => {});

packages/webpack-cli/lib/utils/cli-flags.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ module.exports = {
155155
usage: '--hot',
156156
alias: 'h',
157157
type: Boolean,
158+
negative: true,
158159
group: ADVANCED_GROUP,
159160
description: 'Enables Hot Module Replacement',
160161
link: 'https://webpack.js.org/concepts/hot-module-replacement/',
@@ -242,6 +243,7 @@ module.exports = {
242243
usage: '--stats <value>',
243244
type: [String, Boolean],
244245
group: DISPLAY_GROUP,
246+
negative: true,
245247
description: 'It instructs webpack on how to treat the stats e.g. verbose',
246248
link: 'https://webpack.js.org/configuration/stats/#stats',
247249
},

0 commit comments

Comments
 (0)