diff --git a/src/main/options-normalizer.js b/src/main/options-normalizer.js index c0e3484e34e7..ca9dd2bff70c 100644 --- a/src/main/options-normalizer.js +++ b/src/main/options-normalizer.js @@ -158,6 +158,18 @@ function optionInfoToSchema(optionInfo, { isCLI, optionInfos }) { handlers.deprecated = true; } + // allow CLI overriding, e.g., prettier package.json --tab-width 1 --tab-width 2 + if (isCLI && !optionInfo.array) { + const originalPreprocess = parameters.preprocess || (x => x); + parameters.preprocess = (value, schema, utils) => + schema.preprocess( + originalPreprocess( + Array.isArray(value) ? value[value.length - 1] : value + ), + utils + ); + } + return optionInfo.array ? vnopts.ArraySchema.create( Object.assign( diff --git a/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap b/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap index 2ecb96e99471..aed33500032b 100644 --- a/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap +++ b/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap @@ -1,5 +1,9 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`allow overriding flags (stderr) 1`] = `""`; + +exports[`allow overriding flags (write) 1`] = `Array []`; + exports[`boolean flags do not swallow the next argument (stderr) 1`] = `""`; exports[`boolean flags do not swallow the next argument (stdout) 1`] = ` diff --git a/tests_integration/__tests__/arg-parsing.js b/tests_integration/__tests__/arg-parsing.js index 36420e2d1779..31291aa44179 100644 --- a/tests_integration/__tests__/arg-parsing.js +++ b/tests_integration/__tests__/arg-parsing.js @@ -37,3 +37,14 @@ describe("deprecated option values are warned", () => { status: 0 }); }); + +describe("allow overriding flags", () => { + runPrettier( + "cli/arg-parsing", + ["--tab-width=1", "--tab-width=3", "--parser=babylon"], + { input: "function a() { b }" } + ).test({ + stdout: "function a() {\n b;\n}\n", + status: 0 + }); +});