feat(cli): allow overriding (#5390)

master
Ika 2018-11-10 00:59:59 +08:00 committed by GitHub
parent de11f69889
commit b0b5460e8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -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(

View File

@ -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`] = `

View File

@ -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
});
});