Skip to main content

命令行接口 (CLI)

¥Command Line Interface (CLI)

你可以在命令行上使用 Stylelint。例如:

¥You can use Stylelint on the command line. For example:

npx stylelint "**/*.css"

你应该在文件 glob 周围添加引号。

¥You should include quotation marks around file globs.

如果你使用 npm 脚本,则需要转义引号:

¥If you are using npm scripts, you'll need to escape the quotes:

{
"scripts": {
"lint": "stylelint \"**/*.css\""
}
}

使用 npx stylelint --help 打印 CLI 文档。

¥Use npx stylelint --help to print the CLI documentation.

选项

¥Options

CLI 接受:

¥The CLI accepts:

--allow-empty-input, --aei

当 glob 模式不匹配任何文件时,进程退出而不抛出错误。更多信息

¥The process exits without throwing an error when glob pattern matches no files. More info.

--cache-location

缓存位置的文件或目录的路径。更多信息

¥Path to a file or directory for the cache location. More info.

--cache-strategy

用于检测已更改文件的缓存策略。可以是 "metadata" 或 "content"。更多信息

¥Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". More info.

--cache

存储处理文件的结果,以便 Stylelint 只对更改的文件进行操作。默认情况下,缓存存储在 process.cwd() 中的 ./.stylelintcache 中。更多信息

¥Store the results of processed files so that Stylelint only operates on the changed ones. By default, the cache is stored in ./.stylelintcache in process.cwd(). More info.

--color, --no-color

强制启用/禁用颜色。

¥Force enabling/disabling of color.

--config-basedir

定义 "extends"、"插件" 和 "customSyntax" 的相对路径相对于的目录的绝对路径。仅当这些值是相对路径时才需要。更多信息

¥Absolute path to the directory that relative paths defining "extends", "plugins", and "customSyntax" are relative to. Only necessary if these values are relative paths. More info.

--config, -c

包含 配置对象.json、YAML 或 JS 文件的路径。更多信息

¥Path to a JSON, YAML, or JS file that contains your configuration object. More info.

--custom-syntax

指定要在代码上使用的自定义语法。更多信息

¥Specify a custom syntax to use on your code. More info.

--disable-default-ignores, --di

禁用默认忽略。Stylelint 不会自动忽略 node_modules 的内容。更多信息

¥Disable the default ignores. Stylelint will not automatically ignore the contents of node_modules. More info.

--fix

如果可能,自动修复规则报告的问题。更多信息

¥Automatically fix, where possible, problems reported by rules. More info.

--formatter, -f | --custom-formatter

指定格式化程序来格式化结果。更多信息

¥Specify the formatter to format your results. More info.

--globbyOptions, --go

JSON 格式的选项传递给 globby更多信息

¥Options in JSON format passed to globby. More info.

--ignore-disables, --id

忽略 stylelint-disable(例如 /* stylelint-disable block-no-empty */)注释。更多信息

¥Ignore stylelint-disable (e.g. /* stylelint-disable block-no-empty */) comments. More info.

--ignore-path, -i

包含描述要忽略的文件的模式的文件的路径。该路径可以是绝对路径,也可以是相对于 process.cwd() 的路径。你可以重复该选项以提供多个路径。默认情况下,Stylelint 在 process.cwd() 中查找 .stylelintignore更多信息

¥Path to a file containing patterns that describe files to ignore. The path can be absolute or relative to process.cwd(). You can repeat the option to provide multiple paths. By default, Stylelint looks for .stylelintignore in process.cwd(). More info.

--ignore-pattern, --ip

要忽略的文件模式(除了 .stylelintignore 中的文件之外)。

¥Pattern of files to ignore (in addition to those in .stylelintignore).

--max-warnings, --mw

设置接受警告的数量限制。更多信息

¥Set a limit to the number of warnings accepted. More info.

--output-file, -o

写入报告的文件路径。Stylelint 除了标准输出之外,还将报告输出到指定的 filename

¥Path of file to write a report. Stylelint outputs the report to the specified filename in addition to the standard output.

--print-config

打印给定输入路径的配置。不支持 Glob。

¥Print the configuration for the given input path. Globs are unsupported.

--quiet, -q

仅注册严重性为 "error" 级的规则的问题(忽略 "warning" 级)。更多信息

¥Only register problems for rules with an "error"-level severity (ignore "warning"-level). More info.

--quiet-deprecation-warnings

忽略弃用警告。更多信息

¥Ignore deprecation warnings. More info.

--report-descriptionless-disables, --rdd

生成一份不带描述的 stylelint-disable 注释报告。更多信息

¥Produce a report of the stylelint-disable comments without a description. More info.

--report-invalid-scope-disables, --risd

生成用于配置对象中不存在的规则的 stylelint-disable 注释的报告。更多信息

¥Produce a report of the stylelint-disable comments that used for rules that don't exist within the configuration object. More info.

--report-needless-disables, --rd

生成一份报告来清理你的代码库,仅保留有用的 stylelint-disable 注释。更多信息

¥Produce a report to clean up your codebase, keeping only the stylelint-disable comments that serve a purpose. More info.

--stdin-filename

用于分配输入的文件名。更多信息

¥A filename to assign the input. More info.

--stdin

接受标准输入,即使它是空的。

¥Accept stdin input even if it is empty.

--version, -v

显示当前安装的 Stylelint 版本。

¥Show the currently installed version of Stylelint.

使用示例

¥Usage examples

CLI 期望输入为 文件全局process.stdin。它将格式化结果输出到 process.stdout 中。

¥The CLI expects input as either a file glob or process.stdin. It outputs formatted results into process.stdout.

你应该在文件 glob 周围添加引号。

¥You should include quotation marks around file globs.

示例 A - recursive

¥Example A - recursive

递归检查 foo 目录中的所有 .css 文件:

¥Recursively linting all .css files in the foo directory:

stylelint "foo/**/*.css"

示例 B - 多个文件扩展名

¥Example B - multiple file extensions

对所有 .css.scss.sass 文件进行 Linting:

¥Linting all .css, .scss, and .sass files:

stylelint "**/*.{css,scss,sass}"

示例 C - stdin

¥Example C - stdin

棉绒 stdin

¥Linting stdin:

echo "a { color: pink; }" | stylelint

示例 D - negation

¥Example D - negation

使用输入 glob 中的否定对除 docker 子文件夹内的文件之外的所有 .css 文件进行 Linting:

¥Linting all .css files except those within docker subfolders, using negation in the input glob:

stylelint "**/*.css" "!**/docker/**"

示例 E - caching

¥Example E - caching

缓存已处理的 .scss 文件 foo 目录:

¥Caching processed .scss files foo directory:

stylelint "foo/**/*.scss" --cache --cache-location "/Users/user/.stylelintcache/"

示例 F - 写报告

¥Example F - writing a report

foo 目录中的所有 .css 文件进行 Linting,然后将输出写入 myTestReport.txt

¥Linting all .css files in the foo directory, then writing the output to myTestReport.txt:

stylelint "foo/*.css" --output-file myTestReport.txt

示例 G - 指定配置

¥Example G - specifying a config

使用 bar/mySpecialConfig.json 作为配置来检查 foo 目录及其任何子目录中的所有 .css 文件:

¥Using bar/mySpecialConfig.json as config to lint all .css files in the foo directory and any of its subdirectories:

stylelint "foo/**/*.css" --config bar/mySpecialConfig.json

示例 H - 使用自定义语法

¥Example H - using a custom syntax

使用自定义语法递归检查 foo 目录中的所有 .css 文件:

¥Recursively linting all .css files in the foo directory using a custom syntax:

stylelint "foo/**/*.css" --customSyntax path/to/my-custom-syntax.js

示例 I - 打印成功

¥Example I - print on success

确保成功运行时的输出:

¥Ensure output on successful runs:

stylelint -f verbose "foo/**/*.css"

示例 J - 打印配置

¥Example J - print a config

打印用于给定输入文件的配置:

¥Print a configuration used for the given input file:

stylelint test.css --print-config

退出代码

¥Exit codes

CLI 可以使用以下退出代码退出进程:

¥The CLI can exit the process with the following exit codes:

  • 1 - 致命错误

    ¥1 - fatal error

  • 2 - 棉绒问题

    ¥2 - lint problem

  • 64 - CLI 使用无效

    ¥64 - invalid CLI usage

  • 78 - 无效的配置文件

    ¥78 - invalid configuration file