doccmd
doccmd is a command-line tool (CLI) designed to run external commands, such as linters and formatters, against code blocks embedded within reStructuredText and Markdown documentation files. It helps ensure the code examples in documentation remain correct and up-to-date. The library follows a rapid release cadence, with frequent updates, often on a daily basis.
Common errors
-
NameError: name 'my_function' is not defined (or similar error within grouped code blocks)
cause By default, `doccmd` processes each code block independently. If you have multiple related code blocks that should be treated as a single unit (e.g., one defining a function and a later one calling it), they will error if not explicitly grouped.fixGroup related code blocks using `<!-- group doccmd[all]: start -->` and `<!-- group doccmd[all]: end -->` comments around them in Markdown, or `.. group-doccmd[all]: start` and `.. group-doccmd[all]: end` directives in reStructuredText. -
doccmd: error: changes to grouped code blocks will not be written back
cause A tool run by `doccmd` attempted to modify the content of a code block that is part of a 'group'. By design, `doccmd` prevents tools from modifying grouped code blocks and errors out to prevent silent data loss or unexpected behavior.fixIf you accept that changes to grouped blocks will not be written back to the documentation, rerun `doccmd` with the `--no-fail-on-group-write` option. Otherwise, refactor your documentation or tools to avoid modifying grouped blocks, or do not group blocks that need to be modified by tools.
Warnings
- gotcha doccmd creates temporary files for each code block (e.g., `doccmd_*.py`). These files can interfere with linters or other tools if not explicitly ignored in their configurations.
- breaking When grouping code blocks (using `group doccmd[all]: start`/`end`), tools which modify the code block content will not write changes back to the original documentation file for grouped blocks. By default, this will cause `doccmd` to error.
- gotcha The library has a very rapid release cadence with frequent updates, sometimes daily. While this means quick bug fixes and new features, it may also introduce minor behavioral changes or regressions between closely spaced versions.
Install
-
pip install doccmd
Quickstart
# Create a sample markdown file
cat << EOF > example.md
# My Project
This is an example document.
```python
import os
print("Hello from Python!")
```
```shell
echo "Hello from Shell!"
```
EOF
# Run doccmd to execute Python and shell code blocks
doccmd example.md