Skip to main content

Pre-commit hook to generate markdown documentation from argparse-based CLI scripts

Project description

argparse_to_md: Argparse to README.md

PyPI - Version CI Python

argparse_to_md tool helps developers of command-line tools written in Python keep the usage instructions in their README.md files up to date. It can automatically update usage instructions in README.md file based on argparse parsers defined in the code. It can be invoked as a pre-commit hook or as a standalone script.

How to use argparse_to_md:

  1. In your CLI tool, move creation of argparse.ArgumentParser into a separate function:
    import argparse
    
    def create_parser() -> argparse.ArgumentParser:
        parser = argparse.ArgumentParser(prog='mytool')
        parser.add_argument(...)
        return parser
    
    def main():
        parser = create_parser()
        parser.parse_args()
    
  2. In your README.md file, add a section where the usage would be described. Replace mytool with the fully qualified name of the module and create_parser with the name of the function which returns an argparse.ArgumentParser.
    ### Usage
    
    <!-- argparse_to_md:mytool:create_parser -->
    <!-- argparse_to_md_end -->
    
  3. Run argparse_to_md, either manually or as a pre-commit hook. The README.md file will be updated, the usage instructions will appear inside this section.
  4. Whenever you modify the parser in your code, re-run argparse_to_md, or let the pre-commit hook run. README.md will be updated with the new usage instructions.

Usage as a pre-commit hook

Add to your .pre-commit-config.yaml. This pre-commit hook will be triggered by changes to all Python or Markdown files, and it will edit README.md:

repos:
-   repo: https://github.com/igrr/argparse_to_md.git
    rev: v0.4.0
    hooks:
    -   id: argparse_to_md

If you need to adjust the list of files to be updated, specify them in args: as follows:

repos:
-   repo: https://github.com/igrr/argparse_to_md.git
    rev: v0.4.0
    hooks:
    -   id: argparse_to_md
        args: [--input=README.md, --input=README_CN.md]

Command-line usage

You can also use argparse_to_md from the command line:

Usage:

argparse_to_md [-h] [-i INPUT [INPUT ...]]
                          [--extra-sys-path EXTRA_SYS_PATH [EXTRA_SYS_PATH ...]] [--check]
                          [--version]

Optional arguments:

  • -i INPUT [INPUT ...], --input INPUT [INPUT ...]: Markdown file to update (can be specified multiple times).
  • --extra-sys-path EXTRA_SYS_PATH [EXTRA_SYS_PATH ...]: Extra paths to add to PYTHONPATH before loading the module
  • --check: Check if the files need to be updated, but don't modify them. Non-zero exit code is returned if any file needs to be updated.
  • --version: show program's version number and exit

Customizing output

Output can be customized by passing additional options in the comment:

    <!-- argparse_to_md:module_name:function_name:opt1=value1:opt2=value2 -->
    <!-- argparse_to_md_end -->

The following options are supported:

  • subheading_level (default 0): if set to a non-zero value, the Usage line and all the Usage lines related to subparsers are prefixed with a markdown heading of respective level. For example, when specifying subheading_level=2, the final output will contain ## Usage: instead of Usage:.

Related projects

License

This tool is Copyright (c) 2024 Ivan Grokhotkov and distributed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

argparse_to_md-0.4.0.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparse_to_md-0.4.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file argparse_to_md-0.4.0.tar.gz.

File metadata

  • Download URL: argparse_to_md-0.4.0.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for argparse_to_md-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0a657af3292aeadaa314dbe1b52ce958f331c7893e9d196b1cd0a43f58f57536
MD5 b73a4a367e960e295c74b720f65f7171
BLAKE2b-256 e6c92a497308b6ef0e838b2442f9c07b0964952f9604d9506b52c20b6e0b125f

See more details on using hashes here.

Provenance

The following attestation bundles were made for argparse_to_md-0.4.0.tar.gz:

Publisher: main.yml on igrr/argparse_to_md

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file argparse_to_md-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: argparse_to_md-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for argparse_to_md-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1556a0ada39b21d35268c63b8a281bec9522bb92df3b98e42daf2c09fea0047e
MD5 23f7619d79e803a22a16c13091cf6e3c
BLAKE2b-256 059d65e4ec111a85b961b296a30b58746fdef657081fbac2401e69c3667835b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for argparse_to_md-0.4.0-py3-none-any.whl:

Publisher: main.yml on igrr/argparse_to_md

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page