| 1 |
"""Generate documentation for Cyclopts applications.""" |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from typing import Annotated |
| 5 |
|
| 6 |
from cyclopts.cli import app |
| 7 |
from cyclopts.docs.types import ( |
| 8 |
FORMAT_ALIASES, |
| 9 |
DocFormat, |
| 10 |
normalize_format, |
| 11 |
) |
| 12 |
from cyclopts.group import Group |
| 13 |
from cyclopts.loader import load_app_from_script |
| 14 |
from cyclopts.parameter import Parameter |
| 15 |
from cyclopts.utils import UNSET |
| 16 |
|
| 17 |
|
| 18 |
def _format_group_validator(argument_collection): |
| 19 |
format_arg = argument_collection.get("--format") |
| 20 |
output_arg = argument_collection.get("--output") |
| 21 |
|
| 22 |
if format_arg.value is UNSET: |
| 23 |
if output_arg.value is UNSET: |
| 24 |
raise ValueError('"--format" must be specified when output path is not provided.') |
| 25 |
|
| 26 |
if not output_arg.value or not hasattr(output_arg.value, "suffix"): |
| 27 |
raise ValueError('"--output" must be a valid file path when format is not specified.') |
| 28 |
|
| 29 |
suffix = output_arg.value.suffix.lower() |
| 30 |
|
| 31 |
if not suffix: |
| 32 |
raise ValueError( |
| 33 |
"Output file must have an extension to infer format (e.g., .md, .html, .rst). " |
| 34 |
'Please specify "--format" explicitly or add an extension to the output file.' |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
suffix_key = suffix.lstrip(".") |
| 39 |
|
| 40 |
if not suffix_key: |
| 41 |
raise ValueError( |
| 42 |
"Invalid file extension. Output file must have a valid extension after the period. " |
| 43 |
'Please specify "--format" explicitly.' |
| 44 |
) |
| 45 |
|
| 46 |
inferred_format = FORMAT_ALIASES.get(suffix_key) |
| 47 |
|
| 48 |
if inferred_format is None: |
| 49 |
supported_extensions = [f".{ext}" for ext in FORMAT_ALIASES.keys() if len(ext) <= 4] |
| 50 |
raise ValueError( |
| 51 |
f'Cannot infer format from output extension "{suffix}". ' |
| 52 |
f"Supported extensions: {', '.join(sorted(set(supported_extensions)))}. " |
| 53 |
f'Please specify "--format" explicitly.' |
| 54 |
) |
| 55 |
|
| 56 |
format_arg.value = inferred_format |
| 57 |
|
| 58 |
|
| 59 |
format_group = Group(validator=_format_group_validator) |
| 60 |
|
| 61 |
|
| 62 |
@app.command(default_parameter=Parameter(negative="")) |
| 63 |
def generate_docs( |
| 64 |
script: str, |
| 65 |
output: Annotated[Path | None, Parameter(alias="-o", group=format_group)] = None, |
| 66 |
*, |
| 67 |
format: Annotated[ |
| 68 |
DocFormat | None, |
| 69 |
Parameter(alias="-f", group=format_group), |
| 70 |
] = None, |
| 71 |
include_hidden: bool = False, |
| 72 |
heading_level: int = 1, |
| 73 |
usage_name: str | None = None, |
| 74 |
): |
| 75 |
"""Generate documentation for a Cyclopts application. |
| 76 |
|
| 77 |
Parameters |
| 78 |
---------- |
| 79 |
script : str |
| 80 |
Python script path, optionally with ``':app_object'`` notation to specify |
| 81 |
the App object. If not specified, will search for App objects in the |
| 82 |
script's global namespace. |
| 83 |
output : Optional[Path] |
| 84 |
Output file path. If not specified, prints to stdout. |
| 85 |
format : Optional[DocFormat] |
| 86 |
Output format for documentation. If not specified, inferred from output |
| 87 |
file extension. |
| 88 |
include_hidden : bool |
| 89 |
Include hidden commands in documentation. |
| 90 |
heading_level : int |
| 91 |
Starting heading level for markdown format. |
| 92 |
usage_name : Optional[str] |
| 93 |
Replace the app name shown in ``Usage:`` lines with this string. For |
| 94 |
example, ``"uv run cli"`` for an app whose runtime name is ``"cli"``. |
| 95 |
Headings and anchors are unaffected. Default is None. |
| 96 |
""" |
| 97 |
if format is None: |
| 98 |
raise ValueError("Must specify format.") |
| 99 |
format = normalize_format(format) |
| 100 |
app_obj, _ = load_app_from_script(script) |
| 101 |
docs_content = app_obj.generate_docs( |
| 102 |
output_format=format, |
| 103 |
include_hidden=include_hidden, |
| 104 |
heading_level=heading_level, |
| 105 |
usage_name=usage_name, |
| 106 |
) |
| 107 |
|
| 108 |
if output: |
| 109 |
output.write_text(docs_content) |
| 110 |
else: |
| 111 |
print(docs_content) |