doccmd 2026.3.26.2


pip install doccmd

  Latest version

Released: Mar 26, 2026


Meta
Author: Adam Dangoor
Requires Python: >=3.11

Classifiers

Development Status
  • 5 - Production/Stable

Environment
  • Web Environment

Operating System
  • Microsoft :: Windows
  • POSIX

Programming Language
  • Python :: 3 :: Only
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14

Build Status PyPI

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.10+.

$ pip install doccmd

With Homebrew (macOS, Linux, WSL)

Requires Homebrew.

$ brew tap adamtheturtle/doccmd
$ brew install doccmd

With winget (Windows)

Requires winget.

$ winget install --id adamtheturtle.doccmd --source winget --exact

The winget package may not be the latest version.

Pre-built Linux (x86) binaries

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

Pre-built macOS (ARM) binaries

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

Pre-built macOS (ARM) binaries

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

You may need to remove the quarantine attribute to run the binary:

$ xattr -d com.apple.quarantine /usr/local/bin/doccmd

Pre-built Windows binaries

Download the Windows executable from the latest release and place it in a directory on your PATH.

With Docker

$ docker run --rm -v "$(pwd):/workdir" -w /workdir "ghcr.io/adamtheturtle/doccmd" --help

With Nix

Requires Nix.

$ nix --extra-experimental-features 'nix-command flakes' run "github:adamtheturtle/doccmd/2026.03.26.1" -- --help

To avoid passing --extra-experimental-features every time, enable flakes permanently.

Or add to your flake inputs:

{
  inputs.doccmd.url = "github:adamtheturtle/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: v2026.3.26.1
    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/

# Run ruff format against the code blocks in a Markdown file
# Don't "pad" the code blocks with newlines - the formatter wouldn't like that.
# See the documentation about groups for more information.
$ doccmd --language=python --no-pad-file --no-pad-groups --command="ruff format" README.md

# Run j2lint against the sphinx-jinja2 code blocks in a MyST file
$ doccmd --sphinx-jinja2 --no-pad-file --command="j2lint" README.md

# Run ruff format against the pycon (Python interactive console) code blocks in README.rst
# doccmd strips the >>> prompts before running the formatter and restores them afterward
$ doccmd --language=pycon --no-pad-file --command="ruff format" README.rst

# Run ruff format against python blocks, auto-detecting which ones are pycon
# (use this if your existing docs label interactive sessions as "python" rather than "pycon")
$ doccmd --language=python --no-pad-file --command="ruff format" README.rst

What does it work on?

  • reStructuredText (.rst)

.. code-block:: shell

   echo "Hello, world!"

.. code:: shell

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

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!"
```

```{code} shell
echo "Or this Hello, world!"
```

```{code-cell} shell
echo "Or this code-cell!"
```
  • MDX (.mdx)

.mdx files are supported out of the box. Use --mdx-extension if you need additional suffixes.

```javascript
console.log("Hello, MDX!")
```
  • Djot (.dj)

.dj files are supported out of the box. Use --djot-extension if you need additional suffixes.

```shell
echo "Hello, Djot!"
```
  • Norg (.norg)

.norg files are supported out of the box. Use --norg-extension if you need additional suffixes.

@code shell
echo "Hello, Norg!"
@end
  • 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 flag. See using_groups_with_formatters for more information.

pycon code blocks

When --language=pycon is used, doccmd strips >>> and ... prompts before passing the code to the tool, and restores them afterward, preserving output lines. This allows linters and formatters that expect plain Python source to work on pycon blocks.

The recommended approach is to label interactive Python sessions in your documentation as pycon, keeping them clearly distinct from plain python blocks.

If your existing documentation already uses python as the language for both plain code and interactive sessions, use --detect-pycon-language=python (the default) together with --language=python. doccmd will automatically detect which python blocks are interactive (by checking whether the first non-empty line starts with >>>), and apply pycon stripping only to those. Plain python blocks are processed without stripping.

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 --temporary-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",
]

Running commands in parallel

When doccmd is not writing formatter output back into your documentation files (i.e. you are using --no-write-to-file), you can speed things up by parallelizing both within a document and across documents.

  • --example-workers evaluates multiple code blocks from the same document at once.

  • --document-workers runs different documents concurrently.

Set either option to 0 to auto-detect a worker count based on the number of CPUs on your machine.

For example, doccmd --no-write-to-file --example-workers 4 --document-workers 2 spreads work across two documents, with up to four blocks active per document. This is handy for CPU-bound linters that only emit diagnostics.

Parallel execution is intentionally disabled whenever --write-to-file is in effect, since doccmd cannot safely merge formatter changes into the original documents out of order. Command output might interleave between example workers and document workers, so stick to the default sequential mode when deterministic stdout/stderr ordering is important.

Skipping code blocks

Code blocks which come just after a comment matching skip doccmd[all]: next are skipped.

To skip multiple code blocks in a row, use skip doccmd[all]: start and skip doccmd[all]: end comments surrounding the code blocks to skip.

Use the --skip-marker option to set a marker for this particular command which will work as well as all. For example, use --skip-marker="type-check" to skip code blocks which come just after a comment matching skip doccmd[type-check]: next.

To skip a code block for each of multiple markers, for example to skip a code block for the type-check and lint markers but not all markers, add multiple skip doccmd comments above the code block.

The skip comment will skip the next code block which would otherwise be run. This means that if you run doccmd with --language=python, the Python code block in the following Markdown or MDX example will be skipped:

<-- skip doccmd[all]: next -->

```{code-block} shell
echo "This will not run because the shell language was not selected"
```

```{code-block} python
print("This will be skipped!")
```

Therefore it is not recommended to use skip doccmd[all] and to instead use a more specific marker. For example, if we used doccmd with --language=shell and --skip-marker=echo the following examples show how to skip code blocks in different formats:

  • reStructuredText (.rst)

.. skip doccmd[echo]: next

.. code-block:: shell

   echo "This will be skipped!"

.. code-block:: shell

   echo "This will run"
  • Markdown (.md) and MDX (.mdx)

<-- skip doccmd[echo]: next -->

```shell
echo "This will be skipped!"
```

```shell
echo "This will run"
```
  • MyST (.md with MyST syntax)

% skip doccmd[echo]: next

```{code-block} shell
echo "This will be skipped!"
```

```{code-block} shell
echo "This will run"
```

Grouping code blocks

Automatic file-level grouping

The --group-file option automatically groups all code blocks of the same language within each file, treating them as a single unit for execution. This is useful when code blocks are designed to be executed sequentially, such as in MyST notebooks or tutorial documents where later blocks depend on definitions from earlier ones.

When this option is enabled, you don’t need to add explicit group directives - all code blocks in a file are automatically combined.

For example, with --group-file, these blocks work together without any special markup:

"""Example function which is used in a future code block."""


def my_function() -> None:
    """Do nothing."""
my_function()

Manual grouping with directives

You might have two code blocks like this:

"""Example function which is used in a future code block."""


def my_function() -> None:
    """Do nothing."""
my_function()

and wish to type check the two code blocks as if they were one. By default, this will error as in the second code block, my_function is not defined.

To treat code blocks as one, use group doccmd[all]: start and group doccmd[all]: end comments surrounding the code blocks to group. Grouped code blocks will not have their contents updated in the documentation file. Error messages for grouped code blocks may include lines which do not match the document, so code formatters will not work on them.

Use the --group-marker option to set a marker for this particular command which will work as well as all. For example, use --group-marker="type-check" to group code blocks which come between comments matching group doccmd[type-check]: start and group doccmd[type-check]: end.

Grouping by MDX attributes

The --group-mdx-by-attribute option groups MDX code blocks by the value of a specified attribute. Code blocks with the same attribute value are grouped together and executed as a single unit. This is useful for working with MDX files that follow conventions like Docusaurus, where code blocks are grouped using custom attributes.

For example, with --group-mdx-by-attribute=group, these blocks are grouped by their group attribute value:

```python group="example1"
def my_function():
    return "Hello"
```

```python group="example2"
def other_function():
    return "World"
```

```python group="example1"
result = my_function()
```

In this example, the first and third code blocks (both with group="example1") are grouped together and executed as one unit, while the second block (with group="example2") is processed separately.

Code blocks without the specified attribute are processed individually as normal.

This option only applies to MDX files.

Using groups with formatters

By default, code blocks in groups will be separated by newlines in the temporary file created. This means that line numbers from the original document match the line numbers in the temporary file, and error messages will have correct line numbers. Some tools, such as formatters, may not work well with this separation. To have just one newline between code blocks in a group, use the --no-pad-groups option. If you then want to add extra padding to the code blocks in a group, add invisible code blocks to the document. Make sure that the language of the invisible code block is the same as the --language option given to doccmd.

For example:

  • reStructuredText (.rst)

.. invisible-code-block: java
  • Markdown (.md) and MDX (.mdx)

<!-- invisible-code-block: java

-->

Tools which change the code block content cannot change the content of code blocks inside groups. By default this will error. Use the --no-fail-on-group-write option to emit a warning but not error in this case.

Full documentation

See the full documentation.

2026.3.26.2 Mar 26, 2026
2026.3.26.1 Mar 26, 2026
2026.3.26 Mar 26, 2026
2026.3.2 Mar 02, 2026
2026.3.1 Mar 01, 2026
2026.2.27.1 Feb 27, 2026
2026.2.27 Feb 27, 2026
2026.2.26 Feb 26, 2026
2026.2.15 Feb 15, 2026
2026.1.31.3 Jan 31, 2026
2026.1.31.2 Jan 31, 2026
2026.1.31.1 Jan 31, 2026
2026.1.31 Jan 31, 2026
2026.1.28 Jan 28, 2026
2026.1.27.4 Jan 27, 2026
2026.1.27.3 Jan 27, 2026
2026.1.27.2 Jan 27, 2026
2026.1.27.1 Jan 27, 2026
2026.1.27 Jan 27, 2026
2026.1.25 Jan 25, 2026
2026.1.23.4 Jan 23, 2026
2026.1.23.3 Jan 23, 2026
2026.1.23.2 Jan 23, 2026
2026.1.23.1 Jan 23, 2026
2026.1.23 Jan 23, 2026
2026.1.22.1 Jan 22, 2026
2026.1.22 Jan 22, 2026
2026.1.21.2 Jan 21, 2026
2026.1.21.1 Jan 21, 2026
2026.1.21 Jan 21, 2026
2026.1.18 Jan 18, 2026
2026.1.12 Jan 12, 2026
2026.1.3.2 Jan 03, 2026
2026.1.3.1 Jan 03, 2026
2026.1.3 Jan 03, 2026
2025.12.13 Dec 13, 2025
2025.12.10 Dec 10, 2025
2025.12.8.5 Dec 08, 2025
2025.12.8.4 Dec 08, 2025
2025.12.8.3 Dec 08, 2025
2025.12.8.2 Dec 08, 2025
2025.12.8.1 Dec 08, 2025
2025.12.8 Dec 08, 2025
2025.12.7 Dec 07, 2025
2025.12.5.2 Dec 05, 2025
2025.12.5.1 Dec 05, 2025
2025.12.5 Dec 05, 2025
2025.12.3 Dec 03, 2025
2025.11.20 Nov 20, 2025
2025.11.8.1 Nov 08, 2025
2025.11.8 Nov 08, 2025
2025.10.27 Oct 27, 2025
2025.10.25 Oct 25, 2025
2025.10.18 Oct 18, 2025
2025.9.19 Sep 19, 2025
2025.4.8 Apr 08, 2025
2025.4.7 Apr 07, 2025
2025.4.4 Apr 04, 2025
2025.4.3 Apr 03, 2025
2025.3.27 Mar 27, 2025
2025.3.18 Mar 18, 2025
2025.3.6 Mar 06, 2025
2025.2.20.7 Feb 20, 2025
2025.2.20.5 Feb 20, 2025
2025.2.20.4 Feb 20, 2025
2025.2.20.3 Feb 20, 2025
2025.2.20.2 Feb 20, 2025
2025.2.20.1 Feb 20, 2025
2025.2.20 Feb 20, 2025
2025.2.19 Feb 19, 2025
2025.2.18 Feb 18, 2025
2025.2.17 Feb 17, 2025
2025.1.11 Jan 11, 2025
2024.12.26 Dec 26, 2024
2024.11.14 Nov 14, 2024
2024.11.7 Nov 07, 2024
2024.11.6.1 Nov 06, 2024
2024.11.6 Nov 06, 2024
2024.11.5 Nov 05, 2024
2024.11.4 Nov 04, 2024
2024.10.18 Oct 18, 2024
2024.10.14.2 Oct 14, 2024
2024.10.14.1 Oct 14, 2024
2024.10.14 Oct 14, 2024
2024.10.13.1 Oct 13, 2024
2024.10.13 Oct 13, 2024
2024.10.12.1 Oct 12, 2024
2024.10.12 Oct 12, 2024
2024.10.11 Oct 11, 2024
2024.10.8.12 Oct 08, 2024
2024.10.8.11 Oct 08, 2024
2024.10.8.10 Oct 08, 2024
2024.10.8.9 Oct 08, 2024
2024.10.8.8 Oct 08, 2024
2024.10.8.7 Oct 08, 2024
2024.10.8.6 Oct 08, 2024
2024.10.8.5 Oct 08, 2024
2024.10.8.4 Oct 08, 2024
2024.10.8.3 Oct 08, 2024
2024.10.8.2 Oct 08, 2024
2024.10.8.1 Oct 08, 2024
2024.10.8 Oct 08, 2024
2024.10.7 Oct 07, 2024
2024.10.6 Oct 06, 2024
2024.9.27 Sep 27, 2024
2024.9.26 Sep 26, 2024
2024.9.24 Sep 24, 2024
2024.9.23 Sep 23, 2024
2024.9.21 Sep 21, 2024
2024.9.20 Sep 20, 2024
2024.9.19.3 Sep 19, 2024
2024.9.19.1 Sep 19, 2024
2024.9.19 Sep 19, 2024
2024.9.18 Sep 18, 2024
2024.9.16 Sep 16, 2024
2024.9.15.2 Sep 15, 2024
2024.9.15.1 Sep 15, 2024
2024.9.15 Sep 15, 2024
2024.9.14.5 Sep 14, 2024
2024.9.14.4 Sep 14, 2024
2024.9.14.3 Sep 14, 2024
2024.9.14.2 Sep 14, 2024
2024.9.11.5 Sep 11, 2024
2024.9.11.4 Sep 11, 2024
2024.9.11.3 Sep 11, 2024
2024.9.11.2 Sep 11, 2024

Wheel compatibility matrix

Platform Python 2 Python 3
any

Files in release

Extras:
Dependencies:
beartype (>=0.22.9)
charset-normalizer (>=3.4.1)
click (<8.4.0,>=8.3.0)
click-compose (>=2025.10.27.3)
cloup (>=3.0.0)
dulwich (>=0.22.1)
pygments (>=2.18.0)
sybil (<11.0.0,>=9.3.0)
sybil-extras (==2026.3.1.6)