Debian Codegraph @

docs/source/mkdocs_integration.rst

raw
1 MkDocs Integration
2 ==================
3
4 Cyclopts provides builtin `MkDocs <https://www.mkdocs.org/>`_ support.
5
6 .. warning::
7
8 The MkDocs plugin is **experimental** and may have breaking changes in future releases.
9 If you encounter any issues or have feedback, please `report them on GitHub <https://github.com/BrianPugh/cyclopts/issues>`_.
10
11 .. contents:: Table of Contents
12 :local:
13 :depth: 2
14
15 Quick Start
16 -----------
17
18 1. Install Cyclopts with MkDocs support:
19
20 .. code-block:: bash
21
22 pip install cyclopts[mkdocs]
23
24 2. Add the plugin to your MkDocs configuration (``mkdocs.yml``):
25
26 .. code-block:: yaml
27
28 plugins:
29 - cyclopts
30
31 3. Use the directive in your Markdown files:
32
33 .. code-block:: markdown
34
35 ::: cyclopts
36 module: mypackage.cli:app
37
38 Directive Usage
39 ---------------
40
41 Basic Syntax
42 ~~~~~~~~~~~~
43
44 The ``::: cyclopts`` directive uses YAML format and accepts a module path to your Cyclopts ``App`` object:
45
46 .. code-block:: markdown
47
48 ::: cyclopts
49 module: mypackage.cli:app
50
51 Module Path Formats
52 ~~~~~~~~~~~~~~~~~~~
53
54 The directive accepts two module path formats:
55
56 1. **Explicit format** (``module.path:app_name``):
57
58 .. code-block:: markdown
59
60 ::: cyclopts
61 module: mypackage.cli:app
62
63 ::: cyclopts
64 module: myapp.commands:main_app
65
66 ::: cyclopts
67 module: src.cli:cli
68
69 This explicitly specifies which ``App`` object to document.
70
71 2. **Automatic discovery** (``module.path``):
72
73 .. code-block:: markdown
74
75 ::: cyclopts
76 module: mypackage.cli
77
78 ::: cyclopts
79 module: myapp.main
80
81 The plugin will search the module for an ``App`` instance, looking for common names like ``app``, ``cli``, or ``main``.
82
83 Directive Options
84 -----------------
85
86 The directive supports several options to customize the generated documentation. All options use standard YAML syntax:
87
88 ``module`` - Module Path (Required)
89 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90
91 The module path to your Cyclopts App instance:
92
93 .. code-block:: markdown
94
95 ::: cyclopts
96 module: mypackage.cli:app
97
98 This is the only required option.
99
100 ``heading_level`` - Heading Level
101 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102
103 Set the starting heading level for the generated documentation (1-6, default: 2):
104
105 .. code-block:: markdown
106
107 ::: cyclopts
108 module: mypackage.cli:app
109 heading_level: 3
110
111 This is useful when you need to adjust the heading hierarchy. The default of 2 works well for most cases where the directive is placed under a page title.
112
113 ``max_heading_level`` - Maximum Heading Level
114 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115
116 Set the maximum heading level to use (1-6, default: 6):
117
118 .. code-block:: markdown
119
120 ::: cyclopts
121 module: mypackage.cli:app
122 max_heading_level: 4
123
124 Headings deeper than this level will be capped at this value. This is useful for deeply nested command hierarchies where you want to prevent headings from becoming too small.
125
126 ``recursive`` - Include Subcommands
127 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
129 Control whether to document subcommands recursively (default: true):
130
131 .. code-block:: markdown
132
133 ::: cyclopts
134 module: mypackage.cli:app
135 recursive: false
136
137 Set to ``false`` to only document the top-level commands.
138
139 ``include_hidden`` - Show Hidden Commands
140 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141
142 Include commands marked with ``show=False`` in the documentation:
143
144 .. code-block:: markdown
145
146 ::: cyclopts
147 module: mypackage.cli:app
148 include_hidden: true
149
150 By default, hidden commands are not included in the generated documentation.
151
152 ``flatten_commands`` - Generate Flat Command Hierarchy
153 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154
155 Generate all commands at the same heading level instead of nested hierarchy:
156
157 .. code-block:: markdown
158
159 ::: cyclopts
160 module: mypackage.cli:app
161 flatten_commands: true
162
163 This creates distinct, equally-weighted headings for each command and subcommand, making them easier to reference and navigate in the documentation. Without this option, subcommands are nested with incrementing heading levels.
164
165 ``generate_toc`` - Generate Table of Contents
166 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167
168 Control whether to generate a table of contents for multi-command apps (default: true):
169
170 .. code-block:: markdown
171
172 ::: cyclopts
173 module: mypackage.cli:app
174 generate_toc: false
175
176 This is useful when you want to suppress the automatic table of contents, especially when using multiple directives on the same page or when you have your own navigation structure.
177
178 ``code_block_title`` - Render Titles as Inline Code
179 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180
181 Render command titles with inline code formatting (backticks) instead of plain text:
182
183 .. code-block:: markdown
184
185 ::: cyclopts
186 module: mypackage.cli:app
187 code_block_title: true
188
189 When enabled, command titles are rendered as ``#### `command-name``` instead of ``#### command-name``. This makes command names appear with monospace formatting, which can be useful for certain documentation themes or to make command names stand out visually.
190
191 ``commands`` - Filter Specific Commands
192 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193
194 Document only specific commands from your CLI application:
195
196 .. code-block:: markdown
197
198 ::: cyclopts
199 module: mypackage.cli:app
200 commands:
201 - init
202 - build
203 - deploy
204
205 This will only document the specified commands. You can also use nested command paths with dot notation:
206
207 .. code-block:: markdown
208
209 ::: cyclopts
210 module: mypackage.cli:app
211 commands:
212 - db.migrate
213 - db.backup
214 - api
215
216 Or use inline YAML list syntax:
217
218 .. code-block:: markdown
219
220 ::: cyclopts
221 module: mypackage.cli:app
222 commands: [db.migrate, db.backup, api]
223
224 - ``db.migrate`` - Documents only the ``migrate`` subcommand under ``db``
225 - ``db.backup`` - Documents only the ``backup`` subcommand under ``db``
226 - ``api`` - Documents the ``api`` command and all its subcommands
227
228 You can use either underscore or dash notation in command names - they will be normalized automatically.
229
230 ``exclude_commands`` - Exclude Specific Commands
231 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232
233 Exclude specific commands from the documentation:
234
235 .. code-block:: markdown
236
237 ::: cyclopts
238 module: mypackage.cli:app
239 exclude_commands:
240 - debug
241 - internal-test
242
243 This is useful for hiding internal or debug commands from user-facing documentation. Like ``commands``, this also supports nested command paths with dot notation and inline YAML list syntax.
244
245 ``skip_preamble`` - Skip Description and Usage
246 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247
248 Skip the description and usage sections for the target command when filtering to a single command:
249
250 .. code-block:: markdown
251
252 ::: cyclopts
253 module: mypackage.cli:app
254 commands: [deploy]
255 skip_preamble: true
256
257 When you filter to a single command using ``commands`` and provide your own section heading in the Markdown, you may not want the plugin to generate the command's description and usage block. Setting ``skip_preamble: true`` suppresses these sections while still generating the command's parameters and subcommands.
258
259 This is useful when you want to write your own introduction for a command section:
260
261 .. code-block:: markdown
262
263 ## Deployment
264
265 Deploy your application to production with these commands.
266
267 ::: cyclopts
268 module: mypackage.cli:app
269 commands: [deploy]
270 skip_preamble: true
271
272 Without ``skip_preamble``, the output would include both your introduction and the command's docstring description, which can be redundant.
273
274 Complete Example
275 ----------------
276
277 Here's a complete example showing a CLI application and its MkDocs documentation:
278
279 CLI Application (``myapp/cli.py``):
280 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
281
282 .. code-block:: python
283
284 from pathlib import Path
285 from typing import Optional
286 from cyclopts import App
287
288 app = App(
289 name="myapp",
290 help="My awesome CLI application",
291 version="1.0.0"
292 )
293
294 @app.command
295 def init(path: Path = Path("."), template: str = "default"):
296 """Initialize a new project.
297
298 Parameters
299 ----------
300 path : Path
301 Directory where the project will be created
302 template : str
303 Project template to use
304 """
305 print(f"Initializing project at {path}")
306
307 @app.command
308 def build(source: Path, output: Optional[Path] = None, *, minify: bool = False):
309 """Build the project.
310
311 Parameters
312 ----------
313 source : Path
314 Source directory
315 output : Path, optional
316 Output directory (defaults to source/dist)
317 minify : bool
318 Minify the output files
319 """
320 output = output or source / "dist"
321 print(f"Building from {source} to {output}")
322
323 if __name__ == "__main__":
324 app()
325
326 MkDocs Configuration (``mkdocs.yml``):
327 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
328
329 .. code-block:: yaml
330
331 site_name: MyApp Documentation
332 site_description: Documentation for MyApp CLI
333
334 theme:
335 name: readthedocs
336
337 plugins:
338 - search
339 - cyclopts
340
341 nav:
342 - Home: index.md
343 - CLI Reference: cli-reference.md
344 - User Guide: guide.md
345
346 markdown_extensions:
347 - admonition
348 - codehilite
349 - toc:
350 permalink: true
351
352 Documentation File (``docs/cli-reference.md``):
353 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354
355 .. code-block:: markdown
356
357 # CLI Reference
358
359 This section documents all available CLI commands.
360
361 ::: cyclopts
362 module: myapp.cli:app
363 heading_level: 2
364 recursive: true
365
366 The above directive will automatically generate documentation for all
367 commands, including their parameters, types, defaults, and help text.
368
369 Advanced Usage
370 --------------
371
372 Using Flat Command Structure
373 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
374
375 When you want each command to have its own distinct heading for better navigation:
376
377 .. code-block:: markdown
378
379 # CLI Command Reference
380
381 ::: cyclopts
382 module: myapp.cli:app
383 flatten_commands: true
384
385 This generates all commands at the same heading level (not nested),
386 making it easier to navigate and reference specific commands.
387
388 Selective Command Documentation
389 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
390
391 Split your CLI documentation across multiple sections or pages:
392
393 .. code-block:: markdown
394
395 ## Database Commands
396
397 The following commands manage database operations:
398
399 ::: cyclopts
400 module: myapp.cli:app
401 commands: [db]
402 recursive: true
403
404 ## API Management
405
406 Commands for controlling the API server:
407
408 ::: cyclopts
409 module: myapp.cli:app
410 commands: [api]
411 recursive: true
412
413 ## Development Tools
414
415 Utilities for development (excluding internal debug commands):
416
417 ::: cyclopts
418 module: myapp.cli:app
419 commands: [dev]
420 exclude_commands: [dev.debug, dev.internal]
421 recursive: true
422
423 This approach allows you to:
424
425 - Organize large CLI applications into logical sections
426 - Document different command groups on separate pages
427 - Exclude internal or debug commands from user documentation
428 - Create targeted documentation for different audiences
429
430 See Also
431 --------
432
433 * :doc:`/sphinx_integration` - Sphinx integration (similar functionality)
434 * :doc:`/help` - Customizing help output
435 * :doc:`/commands` - Creating commands and subcommands
436 * :doc:`/parameters` - Parameter types and validation
437 * `MkDocs Documentation <https://www.mkdocs.org/>`_ - Official MkDocs documentation