Skip to content

Commit

Permalink
feat: show possible values for option in help output (#2819)
Browse files Browse the repository at this point in the history
* feat: show possible values for option in help output

* fix: show all possible values

* test: updates

* test: update snaps

* test: fix

* test: fix
  • Loading branch information
snitin315 authored Jul 7, 2021
1 parent 49554a7 commit 828e5c9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -1363,10 +1363,31 @@ class WebpackCLI {
);
}

const flag = this.getBuiltInOptions().find(
(flag) => option.long === `--${flag.name}`,
);

if (flag && flag.configs) {
const possibleValues = flag.configs.reduce((accumulator, currentValue) => {
if (currentValue.values) {
return accumulator.concat(currentValue.values);
} else {
return accumulator;
}
}, []);

if (possibleValues.length > 0) {
this.logger.raw(
`${bold("Possible values:")} ${JSON.stringify(
possibleValues.join(" | "),
)}`,
);
}
}

this.logger.raw("");

// TODO implement this after refactor cli arguments
// logger.raw('Possible values: foo | bar');
// logger.raw('Documentation: https://webpack.js.org/option/name/');
} else {
outputIncorrectUsageOfHelp();
Expand Down
19 changes: 19 additions & 0 deletions test/help/__snapshots__/help.test.js.snap.devServer3.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,20 @@ CLI documentation: https://webpack.js.org/api/cli/.
Made with ♥ by the webpack team."
`;

exports[`help should show help information using the "help --cache-type" option: stderr 1`] = `""`;

exports[`help should show help information using the "help --cache-type" option: stdout 1`] = `
"Usage: webpack --cache-type <value>
Description: In memory caching. Filesystem caching.
Possible values: \\"memory | filesystem\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Webpack documentation: https://webpack.js.org/.
CLI documentation: https://webpack.js.org/api/cli/.
Made with ♥ by the webpack team."
`;

exports[`help should show help information using the "help --color" option: stderr 1`] = `""`;

exports[`help should show help information using the "help --color" option: stdout 1`] = `
Expand All @@ -2560,6 +2574,7 @@ exports[`help should show help information using the "help --mode" option: stder
exports[`help should show help information using the "help --mode" option: stdout 1`] = `
"Usage: webpack --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand All @@ -2571,6 +2586,7 @@ Made with ♥ by the webpack team."
exports[`help should show help information using the "help --mode" option: stdout 2`] = `
"Usage: webpack --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down Expand Up @@ -2610,6 +2626,7 @@ exports[`help should show help information using the "help --stats" option: stde
exports[`help should show help information using the "help --stats" option: stdout 1`] = `
"Usage: webpack --stats [value]
Description: It instructs webpack on how to treat the stats e.g. verbose.
Possible values: \\"none | summary | errors-only | errors-warnings | minimal | normal | detailed | verbose\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand All @@ -2624,6 +2641,7 @@ exports[`help should show help information using the "help --target" option: std
"Usage: webpack --target <value...>
Short: webpack -t <value...>
Description: Sets the build target e.g. node.
Possible values: \\"false\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down Expand Up @@ -2678,6 +2696,7 @@ exports[`help should show help information using the "help serve --mode" option:
exports[`help should show help information using the "help serve --mode" option: stdout 1`] = `
"Usage: webpack serve --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down
19 changes: 19 additions & 0 deletions test/help/__snapshots__/help.test.js.snap.devServer4.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -2670,6 +2670,20 @@ CLI documentation: https://webpack.js.org/api/cli/.
Made with ♥ by the webpack team."
`;

exports[`help should show help information using the "help --cache-type" option: stderr 1`] = `""`;

exports[`help should show help information using the "help --cache-type" option: stdout 1`] = `
"Usage: webpack --cache-type <value>
Description: In memory caching. Filesystem caching.
Possible values: \\"memory | filesystem\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Webpack documentation: https://webpack.js.org/.
CLI documentation: https://webpack.js.org/api/cli/.
Made with ♥ by the webpack team."
`;

exports[`help should show help information using the "help --color" option: stderr 1`] = `""`;

exports[`help should show help information using the "help --color" option: stdout 1`] = `
Expand All @@ -2690,6 +2704,7 @@ exports[`help should show help information using the "help --mode" option: stder
exports[`help should show help information using the "help --mode" option: stdout 1`] = `
"Usage: webpack --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand All @@ -2701,6 +2716,7 @@ Made with ♥ by the webpack team."
exports[`help should show help information using the "help --mode" option: stdout 2`] = `
"Usage: webpack --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down Expand Up @@ -2740,6 +2756,7 @@ exports[`help should show help information using the "help --stats" option: stde
exports[`help should show help information using the "help --stats" option: stdout 1`] = `
"Usage: webpack --stats [value]
Description: It instructs webpack on how to treat the stats e.g. verbose.
Possible values: \\"none | summary | errors-only | errors-warnings | minimal | normal | detailed | verbose\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand All @@ -2754,6 +2771,7 @@ exports[`help should show help information using the "help --target" option: std
"Usage: webpack --target <value...>
Short: webpack -t <value...>
Description: Sets the build target e.g. node.
Possible values: \\"false\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down Expand Up @@ -2808,6 +2826,7 @@ exports[`help should show help information using the "help serve --mode" option:
exports[`help should show help information using the "help serve --mode" option: stdout 1`] = `
"Usage: webpack serve --mode <value>
Description: Defines the mode to pass to webpack.
Possible values: \\"development | production | none\\"

To see list of all supported commands and options run 'webpack --help=verbose'.

Expand Down
14 changes: 13 additions & 1 deletion test/help/help.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const { run, normalizeStderr, normalizeStdout } = require("../utils/test-utils");
const { run, normalizeStderr, normalizeStdout, isWebpack5 } = require("../utils/test-utils");

describe("help", () => {
it('should show help information using the "--help" option', async () => {
Expand Down Expand Up @@ -242,6 +242,18 @@ describe("help", () => {
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
});

it('should show help information using the "help --cache-type" option', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ["help", "--cache-type"]);

if (isWebpack5) {
expect(exitCode).toBe(0);
expect(normalizeStderr(stderr)).toMatchSnapshot("stderr");
expect(normalizeStdout(stdout)).toMatchSnapshot("stdout");
} else {
expect(exitCode).toBe(2);
}
});

it('should show help information using the "help --no-stats" option', async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ["help", "--no-stats"]);

Expand Down

0 comments on commit 828e5c9

Please sign in to comment.