Fix CLI option parsing (#2684)

Fixes #2683
master
Simon Lydell 2017-08-29 08:16:04 +02:00 committed by GitHub
parent a44ef1fe8b
commit 7c9e589323
4 changed files with 87 additions and 10 deletions

View File

@ -49,7 +49,7 @@ const argv = minimist(args, {
"debug-print-doc",
"debug-check",
"with-node-modules"
],
].concat(booleanOptionNames),
string: [
"cursor-offset",
"range-start",
@ -58,7 +58,7 @@ const argv = minimist(args, {
"config",
"find-config-path",
"ignore-path"
],
].concat(stringOptionNames),
default: {
color: true,
"ignore-path": ".prettierignore"
@ -70,14 +70,8 @@ const argv = minimist(args, {
},
unknown: param => {
if (param.startsWith("-")) {
const paramName = param.replace(/--(no-)?/, "");
if (
booleanOptionNames.indexOf(paramName) === -1 &&
stringOptionNames.indexOf(paramName) === -1
) {
console.warn("Ignored unknown option: " + param + "\n");
return false;
}
console.warn("Ignored unknown option: " + param + "\n");
return false;
}
}
});

View File

@ -0,0 +1,45 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`boolean flags do not swallow the next argument 1`] = `
"/* eslint-disable */
console.log('could be single quote and without semi');
"
`;
exports[`boolean flags do not swallow the next argument 2`] = `""`;
exports[`negated options work 1`] = `
"/* eslint-disable */
console.log(\\"could be single quote and without semi\\")
"
`;
exports[`negated options work 2`] = `""`;
exports[`unknown negated options are warned 1`] = `
"/* eslint-disable */
console.log(\\"could be single quote and without semi\\");
"
`;
exports[`unknown negated options are warned 2`] = `
"Ignored unknown option: --no-unknown
"
`;
exports[`unknown options are warned 1`] = `
"/* eslint-disable */
console.log(\\"could be single quote and without semi\\");
"
`;
exports[`unknown options are warned 2`] = `
"Ignored unknown option: --unknown
"
`;

View File

@ -0,0 +1,35 @@
"use strict";
const runPrettier = require("../runPrettier");
test("boolean flags do not swallow the next argument", () => {
const result = runPrettier("cli/arg-parsing", ["--single-quote", "file.js"]);
expect(result.stdout).toMatchSnapshot();
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(0);
});
test("negated options work", () => {
const result = runPrettier("cli/arg-parsing", ["--no-semi", "file.js"]);
expect(result.stdout).toMatchSnapshot();
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(0);
});
test("unknown options are warned", () => {
const result = runPrettier("cli/arg-parsing", ["file.js", "--unknown"]);
expect(result.stdout).toMatchSnapshot();
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(0);
});
test("unknown negated options are warned", () => {
const result = runPrettier("cli/arg-parsing", ["file.js", "--no-unknown"]);
expect(result.stdout).toMatchSnapshot();
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(0);
});

View File

@ -0,0 +1,3 @@
/* eslint-disable */
console.log("could be single quote and without semi")