diff --git a/src/cli-constant.js b/src/cli-constant.js index 39c2530d..f39ee3ba 100644 --- a/src/cli-constant.js +++ b/src/cli-constant.js @@ -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 }; diff --git a/src/cli-util.js b/src/cli-util.js index aed736e2..eb63fbfb 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -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) { diff --git a/tests_integration/__tests__/__snapshots__/early-exit.js.snap b/tests_integration/__tests__/__snapshots__/early-exit.js.snap index de995069..2d4292f3 100644 --- a/tests_integration/__tests__/__snapshots__/early-exit.js.snap +++ b/tests_integration/__tests__/__snapshots__/early-exit.js.snap @@ -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 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 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 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 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 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 to the file to pretend that stdin comes from. --version or -v Print Prettier version. - --write Edit files in-place. (Beware!) "