Pre-commit hook to generate markdown documentation from argparse-based CLI scripts
Project description
argparse_to_md: Argparse to README.md
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:
- 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()
- In your README.md file, add a section where the usage would be described. Replace
mytool
with the fully qualified name of the module andcreate_parser
with the name of the function which returns anargparse.ArgumentParser
.### Usage <!-- argparse_to_md:mytool:create_parser --> <!-- argparse_to_md_end -->
- 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. - 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.3.1
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.3.1
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
Related projects
- https://github.com/9999years/argdown/ — Generates Markdown and RestructuredText from argparse-based parsers.
- https://github.com/alex-rudakov/sphinx-argparse — Sphinx extension for documenting argparse-based parsers.
- https://github.com/docopt/docopt — Inverse of the above, constructs a parser based on documentation.
License
This tool is Copyright (c) 2024 Ivan Grokhotkov and distributed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file argparse_to_md-0.3.1.tar.gz
.
File metadata
- Download URL: argparse_to_md-0.3.1.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22e3322f29dde5cb7c36ac86c546c5b2053ff172c7f68c56df7559854abb8cde |
|
MD5 | 88e1d01abf4eefadc054fbea0f22016e |
|
BLAKE2b-256 | 9cbda3359430baf48fcb8db56990b57b7a723cd2f56a1b268871d62dd2715a61 |
Provenance
The following attestation bundles were made for argparse_to_md-0.3.1.tar.gz
:
Publisher:
main.yml
on igrr/argparse_to_md
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
argparse_to_md-0.3.1.tar.gz
- Subject digest:
22e3322f29dde5cb7c36ac86c546c5b2053ff172c7f68c56df7559854abb8cde
- Sigstore transparency entry: 150542762
- Sigstore integration time:
- Predicate type:
File details
Details for the file argparse_to_md-0.3.1-py3-none-any.whl
.
File metadata
- Download URL: argparse_to_md-0.3.1-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff294c9ad4b00edc5696a2d449218b752be27b5c5b4f166eb85489f83f84b8dc |
|
MD5 | 2c90d7aba69055df6e635b0b647ee7c1 |
|
BLAKE2b-256 | 1fd58f2c1c6cd3426f669c7285472f7be19832c9489180607fc7600804248b9f |
Provenance
The following attestation bundles were made for argparse_to_md-0.3.1-py3-none-any.whl
:
Publisher:
main.yml
on igrr/argparse_to_md
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
argparse_to_md-0.3.1-py3-none-any.whl
- Subject digest:
ff294c9ad4b00edc5696a2d449218b752be27b5c5b4f166eb85489f83f84b8dc
- Sigstore transparency entry: 150542763
- Sigstore integration time:
- Predicate type: