Skip to content

Commit aa34a14

Browse files
refactor: code around only negative flags
1 parent 602bed4 commit aa34a14

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -874,10 +874,16 @@ class WebpackCLI {
874874
}
875875

876876
command.addOption(optionForCommand);
877-
}
877+
} else if (mainOption.type.size === 0 && negativeOption) {
878+
const optionForCommand = new Option(mainOption.flags, mainOption.description);
879+
880+
// Hide stub option
881+
optionForCommand.flags = "";
882+
optionForCommand.hidden = option.hidden || true;
883+
(optionForCommand as Option & { internal?: boolean }).internal = true;
878884

879-
// A situation arises when `mainOption.type.size === 0 && negativeOption`
880-
// In this case, we don't create the option, since this means the option only has a negative value
885+
command.addOption(optionForCommand);
886+
}
881887

882888
if (negativeOption) {
883889
const optionForCommand = new Option(negativeOption.flags, negativeOption.description).default(
@@ -1043,6 +1049,10 @@ class WebpackCLI {
10431049
},
10441050
visibleOptions: function visibleOptions(command) {
10451051
return command.options.filter((option) => {
1052+
if ((option as Option & { internal?: boolean }).internal) {
1053+
return false;
1054+
}
1055+
10461056
// Hide `--watch` option when developer use `webpack watch --help`
10471057
if (
10481058
(options[0] === "w" || options[0] === "watch") &&
@@ -1977,7 +1987,10 @@ class WebpackCLI {
19771987
}
19781988

19791989
for (const option of command.options) {
1980-
if (distance(name, option.long?.slice(2) as string) < 3) {
1990+
if (
1991+
!(option as Option & { internal?: boolean }).internal &&
1992+
distance(name, option.long?.slice(2) as string) < 3
1993+
) {
19811994
this.logger.error(`Did you mean '--${option.name()}'?`);
19821995
}
19831996
}

test/build/core-flags/core-flags.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ describe("core flags", () => {
108108
const { exitCode, stderr, stdout } = await run(__dirname, ["--amd"]);
109109

110110
expect(exitCode).toBe(2);
111-
expect(stderr).toContain("Invalid value 'true' for the '--amd' option");
112-
expect(stderr).toContain("Expected: 'false'");
111+
expect(stderr).toContain("Unknown option '--amd");
113112
expect(stdout).toBeFalsy();
114113
});
115114

0 commit comments

Comments
 (0)