doccmd


Namedoccmd JSON
Version 2025.10.18 PyPI version JSON
download
home_pageNone
SummaryRun commands against code blocks in reStructuredText and Markdown files.
upload_time2025-10-18 06:25:52
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords markdown rst sphinx testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            |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.

.. contents::
   :local:

Installation
------------

With ``pip``
^^^^^^^^^^^^

Requires Python |minimum-python-version|\+.

.. code-block:: shell

   $ pip install doccmd

With Homebrew (macOS, Linux, WSL)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Requires `Homebrew`_.

.. code-block:: shell

   $ brew tap adamtheturtle/doccmd
   $ brew install doccmd

.. _Homebrew: https://docs.brew.sh/Installation

Pre-built Linux (x86) binaries
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: console

   $ curl --fail -L https://github.com/adamtheturtle/doccmd/releases/download/2025.09.19/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``:

.. code-block:: yaml

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

.. _pre-commit: https://pre-commit.com

Usage example
-------------

.. code-block:: shell

   # 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

What does it work on?
---------------------

* reStructuredText (``.rst``)

.. code-block:: 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``.

.. code-block:: markdown

   ```shell
   echo "Hello, world!"
   ```

* MyST (``.md`` with MyST syntax)

.. code-block:: markdown

   ```{code-block} shell
   echo "Hello, world!"
   ```

   ```{code} shell
   echo "Or this 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`` flag.
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 ``--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``:

.. code-block:: toml

   [tool.ruff]

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

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 example will be skipped:

.. code-block:: markdown

   <-- 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``)

.. code-block:: rst

   .. skip doccmd[echo]: next

   .. code-block:: shell

      echo "This will be skipped!"

   .. code-block:: shell

      echo "This will run"

* Markdown (``.md``)

.. code-block:: markdown

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

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

   ```shell
   echo "This will run"
   ```

* MyST (``.md`` with MyST syntax)

.. code-block:: markdown

   % skip doccmd[echo]: next

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

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

Grouping code blocks
--------------------

You might have two code blocks like this:

.. group doccmd[all]: start

.. code-block:: python

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


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


.. code-block:: python

   my_function()

.. group doccmd[all]: end

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``.

.. _using_groups_with_formatters:

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``)

.. code-block:: rst

   .. invisible-code-block: java

* Markdown (``.md``)

.. code-block:: markdown

   <!-- 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 <https://adamtheturtle.github.io/doccmd/>`__.

.. |Build Status| image:: https://github.com/adamtheturtle/doccmd/actions/workflows/ci.yml/badge.svg?branch=main
   :target: https://github.com/adamtheturtle/doccmd/actions
.. |PyPI| image:: https://badge.fury.io/py/doccmd.svg
   :target: https://badge.fury.io/py/doccmd
.. |minimum-python-version| replace:: 3.10

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "doccmd",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "markdown, rst, sphinx, testing",
    "author": null,
    "author_email": "Adam Dangoor <adamdangoor@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/b3/5d/453896d187264d946a6334fe081dd7c5d4943f54efcd2ac21710796e742a/doccmd-2025.10.18.tar.gz",
    "platform": null,
    "description": "|Build Status| |PyPI|\n\ndoccmd\n======\n\nA command line tool for running commands against code blocks in documentation files.\nThis allows you to run linters, formatters, and other tools against the code blocks in your documentation files.\n\n.. contents::\n   :local:\n\nInstallation\n------------\n\nWith ``pip``\n^^^^^^^^^^^^\n\nRequires Python |minimum-python-version|\\+.\n\n.. code-block:: shell\n\n   $ pip install doccmd\n\nWith Homebrew (macOS, Linux, WSL)\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nRequires `Homebrew`_.\n\n.. code-block:: shell\n\n   $ brew tap adamtheturtle/doccmd\n   $ brew install doccmd\n\n.. _Homebrew: https://docs.brew.sh/Installation\n\nPre-built Linux (x86) binaries\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code-block:: console\n\n   $ curl --fail -L https://github.com/adamtheturtle/doccmd/releases/download/2025.09.19/doccmd-linux -o /usr/local/bin/doccmd &&\n       chmod +x /usr/local/bin/doccmd\n\nUsing ``doccmd`` as a pre-commit hook\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nTo run ``doccmd`` with `pre-commit`_, add hooks like the following to your ``.pre-commit-config.yaml``:\n\n.. code-block:: yaml\n\n   -   repo: https://github.com/adamtheturtle/doccmd-pre-commit\n       rev: v2025.4.8\n       hooks:\n       -   id: doccmd\n           args: [\"--language\", \"shell\", \"--command\", \"shellcheck --shell=bash\"]\n           additional_dependencies: [\"shellcheck-py\"]\n\n.. _pre-commit: https://pre-commit.com\n\nUsage example\n-------------\n\n.. code-block:: shell\n\n   # Run mypy against the Python code blocks in README.md and CHANGELOG.rst\n   $ doccmd --language=python --command=\"mypy\" README.md CHANGELOG.rst\n\n   # Run gofmt against the Go code blocks in README.md\n   # This will modify the README.md file in place\n   $ doccmd --language=go --command=\"gofmt -w\" README.md\n\n   # or type less... and search for files in the docs directory\n   $ doccmd -l python -c mypy README.md docs/\n\n   # Run ruff format against the code blocks in a Markdown file\n   # Don't \"pad\" the code blocks with newlines - the formatter wouldn't like that.\n   # See the documentation about groups for more information.\n   $ doccmd --language=python --no-pad-file --no-pad-groups --command=\"ruff format\" README.md\n\n   # Run j2lint against the sphinx-jinja2 code blocks in a MyST file\n   $ doccmd --sphinx-jinja2 --no-pad-file --command=\"j2lint\" README.md\n\nWhat does it work on?\n---------------------\n\n* reStructuredText (``.rst``)\n\n.. code-block:: rst\n\n   .. code-block:: shell\n\n      echo \"Hello, world!\"\n\n   .. code:: shell\n\n      echo \"Or this Hello, world!\"\n\n* Markdown (``.md``)\n\nBy default, ``.md`` files are treated as MyST files.\nTo treat them as Markdown, use ``--myst-extension=. --markdown-extension=.md``.\n\n.. code-block:: markdown\n\n   ```shell\n   echo \"Hello, world!\"\n   ```\n\n* MyST (``.md`` with MyST syntax)\n\n.. code-block:: markdown\n\n   ```{code-block} shell\n   echo \"Hello, world!\"\n   ```\n\n   ```{code} shell\n   echo \"Or this Hello, world!\"\n   ```\n\n* Want more? Open an issue!\n\nFormatters and padding\n----------------------\n\nRunning linters with ``doccmd`` gives you errors and warnings with line numbers that match the documentation file.\nIt does this by adding padding to the code blocks before running the command.\n\nSome 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.\nSee using_groups_with_formatters_ for more information.\n\nFile names and linter ignores\n-----------------------------\n\n``doccmd`` creates temporary files for each code block in the documentation file.\nThese 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.\nFiles are created with a prefix set to the given ``--temporary-file-name-prefix`` argument (default ``doccmd``).\n\nYou can use this information to ignore files in your linter configuration.\n\nFor example, to ignore a rule in all files created by ``doccmd`` in a ``ruff`` configuration in ``pyproject.toml``:\n\n.. code-block:: toml\n\n   [tool.ruff]\n\n   lint.per-file-ignores.\"doccmd_*.py\" = [\n      # Allow hardcoded secrets in documentation.\n      \"S105\",\n   ]\n\nSkipping code blocks\n--------------------\n\nCode blocks which come just after a comment matching ``skip doccmd[all]: next`` are skipped.\n\nTo skip multiple code blocks in a row, use ``skip doccmd[all]: start`` and ``skip doccmd[all]: end`` comments surrounding the code blocks to skip.\n\nUse the ``--skip-marker`` option to set a marker for this particular command which will work as well as ``all``.\nFor example, use ``--skip-marker=\"type-check\"`` to skip code blocks which come just after a comment matching ``skip doccmd[type-check]: next``.\n\nTo 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.\n\nThe skip comment will skip the next code block which would otherwise be run.\nThis means that if you run ``doccmd`` with ``--language=python``, the Python code block in the following example will be skipped:\n\n.. code-block:: markdown\n\n   <-- skip doccmd[all]: next -->\n\n   ```{code-block} shell\n   echo \"This will not run because the shell language was not selected\"\n   ```\n\n   ```{code-block} python\n   print(\"This will be skipped!\")\n   ```\n\nTherefore it is not recommended to use ``skip doccmd[all]`` and to instead use a more specific marker.\nFor example, if we used ``doccmd`` with ``--language=shell`` and ``--skip-marker=echo`` the following examples show how to skip code blocks in different formats:\n\n* reStructuredText (``.rst``)\n\n.. code-block:: rst\n\n   .. skip doccmd[echo]: next\n\n   .. code-block:: shell\n\n      echo \"This will be skipped!\"\n\n   .. code-block:: shell\n\n      echo \"This will run\"\n\n* Markdown (``.md``)\n\n.. code-block:: markdown\n\n   <-- skip doccmd[echo]: next -->\n\n   ```shell\n   echo \"This will be skipped!\"\n   ```\n\n   ```shell\n   echo \"This will run\"\n   ```\n\n* MyST (``.md`` with MyST syntax)\n\n.. code-block:: markdown\n\n   % skip doccmd[echo]: next\n\n   ```{code-block} shell\n   echo \"This will be skipped!\"\n   ```\n\n   ```{code-block} shell\n   echo \"This will run\"\n   ```\n\nGrouping code blocks\n--------------------\n\nYou might have two code blocks like this:\n\n.. group doccmd[all]: start\n\n.. code-block:: python\n\n   \"\"\"Example function which is used in a future code block.\"\"\"\n\n\n   def my_function() -> None:\n       \"\"\"Do nothing.\"\"\"\n\n\n.. code-block:: python\n\n   my_function()\n\n.. group doccmd[all]: end\n\nand wish to type check the two code blocks as if they were one.\nBy default, this will error as in the second code block, ``my_function`` is not defined.\n\nTo treat code blocks as one, use ``group doccmd[all]: start`` and ``group doccmd[all]: end`` comments surrounding the code blocks to group.\nGrouped code blocks will not have their contents updated in the documentation file.\nError messages for grouped code blocks may include lines which do not match the document, so code formatters will not work on them.\n\nUse the ``--group-marker`` option to set a marker for this particular command which will work as well as ``all``.\nFor 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``.\n\n.. _using_groups_with_formatters:\n\nUsing groups with formatters\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nBy default, code blocks in groups will be separated by newlines in the temporary file created.\nThis means that line numbers from the original document match the line numbers in the temporary file, and error messages will have correct line numbers.\nSome tools, such as formatters, may not work well with this separation.\nTo have just one newline between code blocks in a group, use the ``--no-pad-groups`` option.\nIf you then want to add extra padding to the code blocks in a group, add invisible code blocks to the document.\nMake sure that the language of the invisible code block is the same as the ``--language`` option given to ``doccmd``.\n\nFor example:\n\n* reStructuredText (``.rst``)\n\n.. code-block:: rst\n\n   .. invisible-code-block: java\n\n* Markdown (``.md``)\n\n.. code-block:: markdown\n\n   <!-- invisible-code-block: java\n\n   -->\n\nTools which change the code block content cannot change the content of code blocks inside groups.\nBy default this will error.\nUse the ``--no-fail-on-group-write`` option to emit a warning but not error in this case.\n\nFull documentation\n------------------\n\nSee the `full documentation <https://adamtheturtle.github.io/doccmd/>`__.\n\n.. |Build Status| image:: https://github.com/adamtheturtle/doccmd/actions/workflows/ci.yml/badge.svg?branch=main\n   :target: https://github.com/adamtheturtle/doccmd/actions\n.. |PyPI| image:: https://badge.fury.io/py/doccmd.svg\n   :target: https://badge.fury.io/py/doccmd\n.. |minimum-python-version| replace:: 3.10\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Run commands against code blocks in reStructuredText and Markdown files.",
    "version": "2025.10.18",
    "project_urls": {
        "Documentation": "https://adamtheturtle.github.io/doccmd/",
        "Source": "https://github.com/adamtheturtle/doccmd"
    },
    "split_keywords": [
        "markdown",
        " rst",
        " sphinx",
        " testing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c585b70ecc0327083ea246d36315ec0b3af68f4e42308b2fb8b09dda4b98c8b7",
                "md5": "976a3ed219506c22f27de195214b1d3c",
                "sha256": "4acaab4eb74ecdf3b571ddc4af41ee1fbd7dc1e417635ada81572c5c31d39f08"
            },
            "downloads": -1,
            "filename": "doccmd-2025.10.18-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "976a3ed219506c22f27de195214b1d3c",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.10",
            "size": 14659,
            "upload_time": "2025-10-18T06:25:51",
            "upload_time_iso_8601": "2025-10-18T06:25:51.009057Z",
            "url": "https://files.pythonhosted.org/packages/c5/85/b70ecc0327083ea246d36315ec0b3af68f4e42308b2fb8b09dda4b98c8b7/doccmd-2025.10.18-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b35d453896d187264d946a6334fe081dd7c5d4943f54efcd2ac21710796e742a",
                "md5": "ad1d967be20e8ee17a22fa6eb1779cfc",
                "sha256": "8878f17b02679f49da7ab66f1ad2c3df729d0c0e5a85558e180cd1121180e769"
            },
            "downloads": -1,
            "filename": "doccmd-2025.10.18.tar.gz",
            "has_sig": false,
            "md5_digest": "ad1d967be20e8ee17a22fa6eb1779cfc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 45061,
            "upload_time": "2025-10-18T06:25:52",
            "upload_time_iso_8601": "2025-10-18T06:25:52.796915Z",
            "url": "https://files.pythonhosted.org/packages/b3/5d/453896d187264d946a6334fe081dd7c5d4943f54efcd2ac21710796e742a/doccmd-2025.10.18.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-18 06:25:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamtheturtle",
    "github_project": "doccmd",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "doccmd"
}
        
Elapsed time: 2.16334s