Skip to content

Commit

Permalink
feat(cli): allow overriding (#5390)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 9, 2018
1 parent de11f69 commit b0b5460
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/options-normalizer.js
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions 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`] = `
Expand Down
11 changes: 11 additions & 0 deletions tests_integration/__tests__/arg-parsing.js
Expand Up @@ -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
});
});

0 comments on commit b0b5460

Please sign in to comment.