doccmd

A command line tool for running commands against code blocks in documentation files. This allows you to run linters, formatters, and other tools against the code blocks in your documentation files.

Installation

With pip

Requires Python 3.11+.

$ pip install doccmd

With Homebrew (macOS, Linux, WSL)

Requires Homebrew.

$ brew tap adamtheturtle/doccmd
$ brew install doccmd

Pre-built Linux (x86) binaries

$ curl --fail -L "https://github.com/adamtheturtle/doccmd/releases/download/2025.02.20.7/doccmd-linux" -o /usr/local/bin/doccmd &&
    chmod +x /usr/local/bin/doccmd

Using doccmd as a pre-commit hook

To run doccmd with pre-commit, add hooks like the following to your .pre-commit-config.yaml:

-   repo: https://github.com/adamtheturtle/doccmd-pre-commit
    rev: v2025.02.20.7
    hooks:
    -   id: doccmd
        args: ["--language", "shell", "--command", "shellcheck --shell=bash"]
        additional_dependencies: ["shellcheck-py"]

Usage example

# Run mypy against the Python code blocks in README.md and CHANGELOG.rst
$ doccmd --language=python --command="mypy" README.md CHANGELOG.rst

# Run gofmt against the Go code blocks in README.md
# This will modify the README.md file in place
$ doccmd --language=go --command="gofmt -w" README.md

# or type less... and search for files in the docs directory
$ doccmd -l python -c mypy README.md docs/

What does it work on?

  • reStructuredText (.rst)

.. code-block:: shell

   echo "Hello, world!"
  • Markdown (.md)

Note

By default, .md files are treated as MyST files. To treat them as Markdown, use --myst-extension=. --markdown-extension=.md.

```shell
echo "Hello, world!"
```
  • MyST (.md with MyST syntax)

```{code-block} shell
echo "Hello, world!"
```
  • Want more? Open an issue!

Formatters and padding

Running linters with doccmd gives you errors and warnings with line numbers that match the documentation file. It does this by adding padding to the code blocks before running the command.

Some tools do not work well with this padding, and you can choose to obscure the line numbers in order to give the tool the original code block’s content without padding, by using the --no-pad-file and --no-pad-groups flags. See Using groups with formatters for more information.

File names and linter ignores

doccmd creates temporary files for each code block in the documentation file. These files are created in the same directory as the documentation file, and are named with the documentation file name and the line number of the code block. Files are created with a prefix set to the given --file-name-prefix argument (default doccmd).

You can use this information to ignore files in your linter configuration.

For example, to ignore a rule in all files created by doccmd in a ruff configuration in pyproject.toml:

[tool.ruff]

lint.per-file-ignores."*doccmd_*.py" = [
   # Allow hardcoded secrets in documentation.
   "S105",
]

To ignore a rule in files created by doccmd when using pylint, use pylint-per-file-ignores, and a configuration like the following (if using pyproject.toml):

[tool.pylint.'MESSAGES CONTROL']

per-file-ignores = [
   "*doccmd_*.py:invalid-name",
]

Reference