Skip to content

Command Line Interface

v1.x vs v2.x

  • v1.x was written in pure python
  • v2.x was written in rust (with python and node.js bindings)

Version 2.x is intended to be backwards compatible, but a complete rewrite in rust prompted a major version bump.

The minimum versions (badges) specified here hyperlink to different repositories. Anything established in v2.x will correspond to the rust project. Anything established in v1.4.6 or later (before v2.0.0) will correspond to the pure python project. Anything established before v1.4.6 will correspond to pure python sources in the cpp-linter-action project.

Usage
cpp-linter [OPTIONS] [FILE]... [COMMAND]

Commands

version

Display the version of cpp-linter and exit

help

Print this message or the help of the given subcommand(s)

Arguments

files

An explicit path to a file.

This can be specified zero or more times, resulting in a list of files. The list of files is appended to the internal list of 'not ignored' files. Further filtering can still be applied (see Source options).

General options

-V, --version

1.4.6 Default""

The desired version of the clang tools to use.

Accepted options are:

  • A semantic version specifier, eg. >=10, <13, =12.0.1, or simply 16. A version without an operator is a minimum within that major version: 18.1.8 accepts 18.1.8 or a newer 18.x release, and 16 accepts any 16.x release. Use =18.1.8 to require exactly that version. System package managers are not used for such a version because they only install by major version.
  • latest for the newest major version that cpp-linter can install from its static binaries (any minor or patch release of it). This version is fixed when cpp-linter is built, so it only changes with a cpp-linter update.
  • A blank string ('') to use the platform's default installed version.
  • A path to where the clang tools are installed (if using a custom install location). All paths specified here are converted to absolute.
  • If this option is specified without a value, then the cpp-linter version is printed and the program exits.

-v, --verbosity

1.3.0 Default"info"

This controls the log messages' verbosity.

This option does not affect the verbosity of resulting thread comments or file annotations.

Possible values:

  • info | 20: Enables the [log::Level::Info] log level and above
  • debug | 10: Enables the [log::Level::Debug] log level and above

-S, --strict

Default"false"

Exit with code 1 when any check fails.

By default, cpp-linter exits with code 0 even when checks fail, and a CI workflow reads the checks-failed output variable instead. Use this option when only the exit code is read, as in a shell script, a git hook or a coding agent.

This can also be enabled with the CPP_LINTER_STRICT_EXIT environment variable (PRE_COMMIT=1 has the same effect).

Exit codes:

  • 0: no check failed, or checks failed without this option. - 1: checks failed (with this option), or the run stopped on an error. The cpp-linter command prints the error on a line starting with Error:. The main() function of the Python and Node.js bindings raises it as an exception instead. - 2: the command line arguments are invalid.

Possible values:

  • true | on | 1
  • false | off | 0

--mod-sys

2.0.0 Default"false"

Whether to use the system's available package managers.

By default, this matches the value of a CI environment variable. For non-CI contexts, this allows users to opt-in to using system package managers.

When allowed, system package managers are tried first. If they are not allowed or fail, then the clang tools are downloaded from PyPI, and then from the static binaries built by cpp-linter (for compatible platforms).

--no-mod-sys

2.0.0 Default"false"

Strictly disallow using the system's package managers.

This can be used to override the default behavior of --mod-sys, useful in sensitive CI environments like self-hosted runners.

Source options

-e, --extensions

1.4.6 Default"c,h,C,H,cpp,hpp,cc,hh,c++,h++,cxx,hxx"

A comma-separated list of file extensions to analyze.

Surrounding whitespace and a leading . are ignored, so .cpp, .h is the same as cpp,h.

-r, --repo-root

1.4.6 Default"."

The path to the repository root directory.

This path should be relative to the current working directory. It can also be an absolute path.

-l, --lines-changed-only

1.5.0 contents: read Default"true"

This controls what part of the files are analyzed.

Possible values:

  • false | off | 0: All lines in a file are analyzed.
  • diff: All lines in the diff are analyzed (including unchanged lines but not subtractions).
  • true | on | 1: Only lines in the diff that contain additions are analyzed.

-f, --files-changed-only

1.3.0 contents: read Default"false"

Set this option to false to analyze any source files in the repo.

This is automatically enabled if --lines-changed-only is enabled.

Note

The GITHUB_TOKEN should be supplied when running on a private repository with this option enabled, otherwise the runner does not not have the privilege to list the changed files for an event.

See Authenticating with the GITHUB_TOKEN.

Possible values:

  • true | on | 1
  • false | off | 0

-i, --ignore

1.3.0 Default".github|target"

Set this option with path(s) to ignore (or not ignore).

  • In the case of multiple paths, you can use | to separate each path.
  • There is no need to use ./ for each entry; a blank entry represents the repo-root path (eg. '|!src' ignores everything except src). A blank string ('') on its own means nothing is ignored.
  • This can also have files, but the file's path (relative to the --repo-root) has to be specified with the filename.
  • Submodules are automatically ignored. Hidden directories (beginning with a .) are also ignored automatically.
  • Prefix a path with ! to explicitly not ignore it. This can be applied to a submodule's path (if desired) but not hidden directories.
  • Glob patterns are supported here. Path separators in glob patterns should use / because \ represents an escaped literal.

-b, --diff-base

1.12.0

The git reference to use as the base for diffing changed files.

This can be any valid git ref, such as a branch name, tag name, or commit SHA. If it is an integer, then it is treated as the number of parent commits from HEAD.

This option only applies to non-CI contexts (eg. local CLI use).

--ignore-index

1.12.0 Default"false"

Assert this switch to ignore any staged changes when generating a diff of changed files. Useful when used with --diff-base

Clang-format options

-s, --style

1.4.6 Default"llvm"

The style rules to use.

  • Set this to file to have clang-format use the closest relative .clang-format file. Same as passing no value to this option.
  • Set this to a blank string ('') to disable using clang-format entirely.

Note

If this is not a blank string, then fixes suggested by clang-tidy (if --tidy-checks is not -*) are also formatted with this style. This is done to ensure suggestions from both clang-tidy and clang-format are consistent.

-M, --ignore-format

1.9.0

Similar to --ignore but applied exclusively to files analyzed by clang-format

Clang-tidy options

-D, --ignore-tidy

1.9.0

Similar to --ignore but applied exclusively to files analyzed by clang-tidy

-c, --tidy-checks

1.4.6 Default"boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,cppcoreguidelines-*"

A comma-separated list of globs with optional - prefix.

Globs are processed in order of appearance in the list. Globs without - prefix add checks with matching names to the set, globs with the - prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in a .clang-tidy file (if any).

  • It is possible to disable clang-tidy entirely by setting this option to '-*'.
  • It is also possible to rely solely on a .clang-tidy config file by specifying this option as a blank string ('').
  • The default does not add the Clang Static Analyzer checks (clang-analyzer-*) because they can make clang-tidy many times slower. clang-tidy 22 and newer also leave them out of their own defaults. clang-tidy 21 and older still enable them unless -clang-analyzer-* is added. Add clang-analyzer-* to this option or to a .clang-tidy file to enable them.

See also clang-tidy docs for more info.

-p, --database

1.4.0

The path that is used to read a compile command database.

For example, it can be a CMake build directory in which a file named compile_commands.json exists (set CMAKE_EXPORT_COMPILE_COMMANDS to ON). When no build path is specified, a search for compile_commands.json will be attempted through all parent paths of the first input file, and cpp-linter warns that clang-tidy may report false "file not found" errors. See LLVM docs about setup tooling for an example of setting up Clang Tooling on a source tree.

-x, --extra-arg

1.4.7

A string of extra arguments passed to clang-tidy for use as compiler arguments.

This can be specified more than once for each additional argument. Recommend using quotes around the value and avoid using spaces between name and value (use = instead):

cpp-linter --extra-arg="-std=c++17" --extra-arg="-Wall"

Feedback options

-t, --thread-comments

1.6.1 contents: write Default"false"

Set this option to true to enable the use of thread comments as feedback.

Note

To use thread comments, the GITHUB_TOKEN (provided by Github to each repository) must be declared as an environment variable.

See Authenticating with the GITHUB_TOKEN.

Possible values:

  • true | on | 1: Always post a new comment and delete any outdated ones.
  • false | off | 0: Do not post thread comments.
  • update: Only update existing thread comments. If none exist, then post a new one.

-g, --no-lgtm

1.6.1 Default"true"

Set this option to true or false to enable or disable the use of a thread comment that basically says 'Looks Good To Me' (when all checks pass).

Important

The --thread-comments option also notes further implications.

Possible values:

  • true | on | 1
  • false | off | 0

-w, --step-summary

1.6.0 Default"false"

Set this option to true or false to enable or disable the use of a workflow step summary when the run has concluded

Possible values:

  • true | on | 1
  • false | off | 0

-o, --summary-output-file

1.13.0

Provide this option with a path to which a summary comment is written.

A relative path is relative to the current working directory.

--sarif-file

2.0.0

Provide this option with a path to which clang-tidy diagnostics are written in the SARIF 2.1.0 format.

A relative path is relative to the current working directory. Use - to write the SARIF log to stdout instead of a file. Outside CI, log messages (including file annotations) and the generated patch then go to stderr, so stdout holds only the SARIF log.

The SARIF file only includes clang-tidy diagnostics that are also reported in other forms of feedback. So, source filters like --lines-changed-only and --ignore-tidy are respected. The SARIF file is written even if no diagnostics were found.

> In GitHub Actions, the SARIF file can be uploaded to > GitHub code scanning > with the github/codeql-action/upload-sarif action. > Code scanning treats each upload for a branch as the full state of that branch, > so scan whole files there. See GitHub Code Scanning.

--code-quality-file

2.0.0

Provide this option with a path to which clang-format and clang-tidy findings are written as a GitLab Code Quality report.

A relative path is relative to the current working directory. Use - to write the report to stdout instead of a file. As with --sarif-file -, log messages and the generated patch then go to stderr outside CI. Only one of --sarif-file and --code-quality-file can be -.

Like the SARIF file, the report only includes findings that are also reported in other forms of feedback. So, source filters like --lines-changed-only and --ignore-tidy are respected. Unlike the SARIF file, the report also includes clang-format findings. The report is written even if no findings were found (an empty array).

> In GitLab CI/CD, declare the file as a > codequality report artifact > to show the findings in merge requests.

-a, --file-annotations

1.4.3 Default"true"

Set this option to false to disable the use of file annotations as feedback.

> GitHub shows only the first 10 annotations of each level > (error, warning, notice) in a step, and 50 in a job. > cpp-linter logs a warning when some annotations will not be shown. > The step summary and the thread comment list every finding.

Possible values:

  • true | on | 1
  • false | off | 0

-P, --pr-review

2.0.0 pull-requests: write

Set to true to enable Pull Request reviews.

v1's --tidy-review and --format-review are aliases of this option. It can be given more than once; reviews are enabled if any value is true.

Possible values:

  • true | on | 1
  • false | off | 0

-R, --passive-reviews

1.10.0 pull-requests: write Default"false"

Set to true to prevent Pull Request reviews from approving or requesting changes

Possible values:

  • true | on | 1
  • false | off | 0

-F, --gen-patch

Default"false"

Set to true to generate a patch file of applied clang fixes.

The path to the generated patch file is printed in the logs and set as the output variable fix-patch-path in CI workflows.

Possible values:

  • true | on | 1
  • false | off | 0

--gen-patch-path

Write the patch file of applied clang fixes to this path.

Setting this implies --gen-patch. A relative path is relative to the current working directory. If not set, the patch is written to .cpp-linter-cache/auto-fix.patch in the --repo-root folder.

This can also be set with the GEN_PATCH_PATH environment variable, so the same variable can be used to apply the patch afterward:

export GEN_PATCH_PATH=fixes.patch
cpp-linter
git apply "$GEN_PATCH_PATH"