Improve option categorization and usage summary

- Move the very important options --write and --list-different to the
  top, in the new Output category. It could be argued that --help and
  --version also belong there, but I don't think they deserve such a
  prominent position, so they are left in the Other category.
  --find-config-path could also be considered an Output option, but I
  think it fits better in the Config category.
- Now that there's an Output category, should there also be an Input
  category? I thought about that, but the only candidates for such a
  category are --stdin and --stdin-filepath and I don't think they are
  important enough to have their own category, so they stay in Other.
- Move --cursor-offset, --range-end and --range-start to a new Editor
  category, since they are mainly for editor integrations.
- Document how Prettier deals with input and output by default.
master
Simon Lydell 2017-09-13 08:09:01 +02:00 committed by Ika
parent 32dfb80c86
commit 45d200a06d
3 changed files with 58 additions and 20 deletions

View File

@ -1,8 +1,10 @@
"use strict";
const CATEGORY_CONFIG = "Config";
const CATEGORY_EDITOR = "Editor";
const CATEGORY_FORMAT = "Format";
const CATEGORY_OTHER = "Other";
const CATEGORY_OUTPUT = "Output";
/**
* {
@ -99,6 +101,7 @@ const detailedOptions = normalizeDetailedOptions({
},
"cursor-offset": {
type: "int",
category: CATEGORY_EDITOR,
exception: -1,
forwardToApi: true,
description: dedent(`
@ -146,6 +149,7 @@ const detailedOptions = normalizeDetailedOptions({
},
"list-different": {
type: "boolean",
category: CATEGORY_OUTPUT,
alias: "l",
description:
"Print the names of files that are different from Prettier's formatting."
@ -167,6 +171,7 @@ const detailedOptions = normalizeDetailedOptions({
},
"range-end": {
type: "int",
category: CATEGORY_EDITOR,
forwardToApi: true,
exception: Infinity,
description: dedent(`
@ -178,6 +183,7 @@ const detailedOptions = normalizeDetailedOptions({
},
"range-start": {
type: "int",
category: CATEGORY_EDITOR,
forwardToApi: true,
description: dedent(`
Format code starting at a given character offset.
@ -201,7 +207,7 @@ const detailedOptions = normalizeDetailedOptions({
},
stdin: {
type: "boolean",
description: "Read input from stdin."
description: "Force reading input from stdin."
},
"stdin-filepath": {
type: "path",
@ -245,6 +251,7 @@ const detailedOptions = normalizeDetailedOptions({
},
write: {
type: "boolean",
category: CATEGORY_OUTPUT,
description: "Edit files in-place. (Beware!)"
}
});
@ -278,6 +285,13 @@ const minimistOptions = {
}
};
const usageSummary = `
Usage: prettier [optionss] [path ...]
By default, output is written to stdout.
Stdin is read if it is piped to Prettier and no paths are given.
`.trim();
function dedent(str) {
const spaces = str.match(/\n^( +)/m)[1].length;
return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim();
@ -319,9 +333,12 @@ const detailedOptionMap = detailedOptions.reduce(
module.exports = {
CATEGORY_CONFIG,
CATEGORY_EDITOR,
CATEGORY_FORMAT,
CATEGORY_OTHER,
CATEGORY_OUTPUT,
minimistOptions,
detailedOptions,
detailedOptionMap
detailedOptionMap,
usageSummary
};

View File

@ -345,9 +345,12 @@ function createUsage() {
const groupedOptions = groupBy(flattenedOptions, option => option.category);
const usageSummary = "Usage: prettier [opts] [filename ...]";
const firstCategories = [constant.CATEGORY_FORMAT, constant.CATEGORY_CONFIG];
const firstCategories = [
constant.CATEGORY_OUTPUT,
constant.CATEGORY_FORMAT,
constant.CATEGORY_CONFIG,
constant.CATEGORY_EDITOR
];
const lastCategories = [constant.CATEGORY_OTHER];
const restCategories = Object.keys(groupedOptions).filter(
category =>
@ -363,7 +366,7 @@ function createUsage() {
return `${category} options:\n\n${indent(categoryOptions, 2)}`;
});
return [usageSummary].concat(optionsUsage, [""]).join("\n\n");
return [constant.usageSummary].concat(optionsUsage, [""]).join("\n\n");
}
function createOptionUsage(option, threshold) {

View File

@ -1,7 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`show usage with --help 1`] = `
"Usage: prettier [opts] [filename ...]
"Usage: prettier [optionss] [path ...]
By default, output is written to stdout.
Stdin is read if it is piped to Prettier and no paths are given.
Output options:
--list-different or -l Print the names of files that are different from Prettier's formatting.
--write Edit files in-place. (Beware!)
Format options:
@ -34,13 +42,10 @@ Config options:
Defaults to ./.prettierignore.
--with-node-modules Process files inside 'node_modules' directory.
Other options:
Editor options:
--no-color Do not colorize error messages.
--cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.
This option cannot be used with --range-start and --range-end.
--help or -h Show help.
--list-different or -l Print the names of files that are different from Prettier's formatting.
--range-end <int> Format code ending at a given character offset (exclusive).
The range will extend forwards to the end of the selected statement.
This option cannot be used with --cursor-offset.
@ -49,17 +54,29 @@ Other options:
The range will extend backwards to the start of the first line containing the selected statement.
This option cannot be used with --cursor-offset.
Defaults to 0.
--stdin Read input from stdin.
Other options:
--no-color Do not colorize error messages.
--help or -h Show help.
--stdin Force reading input from stdin.
--stdin-filepath <path> Path to the file to pretend that stdin comes from.
--version or -v Print Prettier version.
--write Edit files in-place. (Beware!)
"
`;
exports[`throw error and show usage with something unexpected 1`] = `
"Usage: prettier [opts] [filename ...]
"Usage: prettier [optionss] [path ...]
By default, output is written to stdout.
Stdin is read if it is piped to Prettier and no paths are given.
Output options:
--list-different or -l Print the names of files that are different from Prettier's formatting.
--write Edit files in-place. (Beware!)
Format options:
@ -92,13 +109,10 @@ Config options:
Defaults to ./.prettierignore.
--with-node-modules Process files inside 'node_modules' directory.
Other options:
Editor options:
--no-color Do not colorize error messages.
--cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.
This option cannot be used with --range-start and --range-end.
--help or -h Show help.
--list-different or -l Print the names of files that are different from Prettier's formatting.
--range-end <int> Format code ending at a given character offset (exclusive).
The range will extend forwards to the end of the selected statement.
This option cannot be used with --cursor-offset.
@ -107,10 +121,14 @@ Other options:
The range will extend backwards to the start of the first line containing the selected statement.
This option cannot be used with --cursor-offset.
Defaults to 0.
--stdin Read input from stdin.
Other options:
--no-color Do not colorize error messages.
--help or -h Show help.
--stdin Force reading input from stdin.
--stdin-filepath <path> Path to the file to pretend that stdin comes from.
--version or -v Print Prettier version.
--write Edit files in-place. (Beware!)
"