Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): allow overriding #5390

Merged
merged 2 commits into from Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
});
});