Skip to content

Commit 7f50948

Browse files
authored
fix: show default value in help output if available (#2814)
* fix: show default value in help output if available * test: add
1 parent 0d8d832 commit 7f50948

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ class WebpackCLI {
13571357
this.logger.raw(`${bold("Description:")} ${option.description}`);
13581358
}
13591359

1360-
if (!option.negate && options.defaultValue) {
1360+
if (!option.negate && option.defaultValue) {
13611361
this.logger.raw(
13621362
`${bold("Default value:")} ${JSON.stringify(option.defaultValue)}`,
13631363
);

test/api/CLI.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -1699,4 +1699,34 @@ describe("CLI API", () => {
16991699
expect(command.helpInformation()).toContain("--no-boolean Negated description");
17001700
});
17011701
});
1702+
1703+
describe("custom help output", () => {
1704+
let consoleSpy;
1705+
let exitSpy;
1706+
1707+
beforeEach(async () => {
1708+
consoleSpy = jest.spyOn(global.console, "log");
1709+
exitSpy = jest.spyOn(process, "exit").mockImplementation(() => {});
1710+
1711+
cli.program.option("--color [value]", "any color", "blue");
1712+
await new Promise((resolve, reject) => {
1713+
try {
1714+
cli.run(["help", "--color"], { from: "user" });
1715+
resolve();
1716+
} catch (error) {
1717+
reject(error);
1718+
}
1719+
});
1720+
});
1721+
1722+
afterEach(async () => {
1723+
consoleSpy.mockRestore();
1724+
exitSpy.mockRestore();
1725+
});
1726+
1727+
it("should display help information", () => {
1728+
expect(exitSpy).toHaveBeenCalledWith(0);
1729+
expect(consoleSpy.mock.calls).toMatchSnapshot();
1730+
});
1731+
});
17021732
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CLI API custom help output should display help information 1`] = `
4+
Array [
5+
Array [
6+
"Usage: webpack --color [value]",
7+
],
8+
Array [
9+
"Description: any color",
10+
],
11+
Array [
12+
"Default value: \\"blue\\"",
13+
],
14+
Array [
15+
"",
16+
],
17+
Array [
18+
"To see list of all supported commands and options run 'webpack --help=verbose'.
19+
",
20+
],
21+
Array [
22+
"Webpack documentation: https://webpack.js.org/.",
23+
],
24+
Array [
25+
"CLI documentation: https://webpack.js.org/api/cli/.",
26+
],
27+
Array [
28+
"Made with ♥ by the webpack team.",
29+
],
30+
]
31+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CLI API custom help output should display help information 1`] = `
4+
Array [
5+
Array [
6+
"Usage: webpack --color [value]",
7+
],
8+
Array [
9+
"Description: any color",
10+
],
11+
Array [
12+
"Default value: \\"blue\\"",
13+
],
14+
Array [
15+
"",
16+
],
17+
Array [
18+
"To see list of all supported commands and options run 'webpack --help=verbose'.
19+
",
20+
],
21+
Array [
22+
"Webpack documentation: https://webpack.js.org/.",
23+
],
24+
Array [
25+
"CLI documentation: https://webpack.js.org/api/cli/.",
26+
],
27+
Array [
28+
"Made with ♥ by the webpack team.",
29+
],
30+
]
31+
`;

0 commit comments

Comments
 (0)