Generate CLI reference documentation from argparse-based applications using Jinja2 templates
Project description
gencodo-py
Generate CLI reference documentation from argparse-based applications using Jinja2 templates.
Installation
pip install gencodo-py
Quick Start
Define your CLI commands as plain Python classes (no base class required):
import argparse
class GreetCommand:
name = "greet"
help_msg = "Greet a specific person"
overview = "Personalize your greeting by specifying a name."
hidden = False
examples = [("Greet Alice", "myapp greet Alice")]
related_commands = None
def fill_parser(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument("name", help="Name to greet")
parser.add_argument("--formal", action="store_true", default=False,
help="Use formal style")
Generate documentation:
from gencodo import CommandGroup, gen_docs_tree, get_bundled_templates
groups = [CommandGroup(name="Greetings", commands=[GreetCommand])]
templates = get_bundled_templates("md") # or "rst"
gen_docs_tree(
appname="myapp",
command_groups=groups,
output_dir="docs/cli-ref",
templates=templates,
)
API Reference
Types
Command-- Protocol that any CLI command class must satisfy (structural subtyping).CommandGroup-- NamedTuple grouping commands under a name.ExampleInfo-- Dataclass for a usage example (info, usage).FlagInfo-- Dataclass for a CLI flag (name, usage, default_value).TemplateInfo-- Dataclass for Jinja2 template configuration.
Functions
gen_docs(command_class, writer, template, appname, command_groups)-- Render docs for a single command to a TextIO writer.gen_docs_tree(appname, command_groups, output_dir, templates, ...)-- Generate a full documentation tree (one file per command + index).get_bundled_templates(format="rst", index_file_name=None)-- Load bundled reST or Markdown templates.
Command Protocol
Your command classes need these attributes/methods:
| Attribute | Type | Description |
|---|---|---|
name |
str |
Command name |
help_msg |
str |
Short help string |
overview |
str |
Longer description |
hidden |
bool |
Exclude from docs if True |
examples |
list[tuple[str, str]] |
(description, command) pairs |
related_commands |
list[str] | None |
Explicit related commands, or None to auto-infer |
fill_parser(parser) |
method | Add arguments to an ArgumentParser |
Template Customization
Bundled Templates
Use get_bundled_templates("rst") or get_bundled_templates("md") for the built-in templates.
Custom Templates
Pass your own Jinja2 template strings via TemplateInfo:
from gencodo import TemplateInfo
templates = TemplateInfo(
index_file_name="index.md",
index_template="# Commands\n{% for f in files %}...\n{% endfor %}",
command_template="# {{ command_name }}\n{{ short }}\n...",
)
Available template variables for command templates:
| Variable | Type | Description |
|---|---|---|
ref |
str |
Anchor reference (underscored name) |
command_name |
str |
Command name |
short |
str |
Short help message |
long |
str |
Overview text |
synopsis |
str |
Usage synopsis |
examples |
list[ExampleInfo] |
Usage examples |
flags |
list[FlagInfo] |
Optional flags |
related_commands |
list[str] |
Related command names |
heading_len |
int |
Length of command name |
appname |
str |
Application name |
Custom Jinja2 filters available: indent(width), repeat(n).
License
LGPL-3.0-only
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gencodo_py-0.2.0.tar.gz.
File metadata
- Download URL: gencodo_py-0.2.0.tar.gz
- Upload date:
- Size: 45.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f41d06d1c11a070c20ab0b60f9745840229680d248c88e376fb3c22a7784c892
|
|
| MD5 |
ea8435b0e89aadf742d2dae99fc66d5c
|
|
| BLAKE2b-256 |
dba88c41f1266f8b21a947fa7f8c8a86e12d8fde884fb63d3941e840f2eb92ab
|
Provenance
The following attestation bundles were made for gencodo_py-0.2.0.tar.gz:
Publisher:
publish.yml on canonical/gencodo-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gencodo_py-0.2.0.tar.gz -
Subject digest:
f41d06d1c11a070c20ab0b60f9745840229680d248c88e376fb3c22a7784c892 - Sigstore transparency entry: 992687693
- Sigstore integration time:
-
Permalink:
canonical/gencodo-py@ee7b05e1a0c760a29f6be3a1b482c71b53ddbc00 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ee7b05e1a0c760a29f6be3a1b482c71b53ddbc00 -
Trigger Event:
release
-
Statement type:
File details
Details for the file gencodo_py-0.2.0-py3-none-any.whl.
File metadata
- Download URL: gencodo_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2275933dadbda1c9d648be6472077a77562da664ed2d712efc7e133f46771e58
|
|
| MD5 |
0124b8db723c69d16b4916bdcb06a610
|
|
| BLAKE2b-256 |
6c14ef5600a975cff92663441bfe46a8073cb5e07404539c7c3fbec524b968b8
|
Provenance
The following attestation bundles were made for gencodo_py-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on canonical/gencodo-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gencodo_py-0.2.0-py3-none-any.whl -
Subject digest:
2275933dadbda1c9d648be6472077a77562da664ed2d712efc7e133f46771e58 - Sigstore transparency entry: 992687697
- Sigstore integration time:
-
Permalink:
canonical/gencodo-py@ee7b05e1a0c760a29f6be3a1b482c71b53ddbc00 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/canonical
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ee7b05e1a0c760a29f6be3a1b482c71b53ddbc00 -
Trigger Event:
release
-
Statement type: