Debian Codegraph @

docs/source/help.rst

raw
1 ====
2 Help
3 ====
4
5 A help screen is standard for every CLI application.
6 Cyclopts by-default adds ``--help`` and ``-h`` flags to the application:
7
8 .. code-block:: console
9
10 $ my-application --help
11 Usage: my-application COMMAND
12
13 My application short description.
14
15 ╭─ Commands ─────────────────────────────────────────────────────────╮
16 │ foo Foo help string. │
17 │ bar Bar help string. │
18 │ --help -h Display this message and exit. │
19 │ --version Display application version. │
20 ╰────────────────────────────────────────────────────────────────────╯
21
22 Cyclopts derives the components of the help string from a variety of sources.
23 The source resolution order is as follows (as applicable):
24
25 1. The ``help`` field in the :meth:`@app.command <cyclopts.App.command>` decorator.
26
27 .. code-block:: python
28
29 app = cyclopts.App()
30
31
32 @app.command(help="This is the highest precedence help-string for 'bar'.")
33 def bar():
34 pass
35
36 When registering an :class:`.App` object, supplying ``help`` via the :meth:`@app.command <cyclopts.App.command>` decorator is forbidden to reduce ambiguity and will raise a :exc:`ValueError`. See (2).
37
38 2. Via :attr:`.App.help`.
39
40 .. code-block:: python
41
42 app = cyclopts.App(help="This help string has highest precedence at the app-level.")
43
44 sub_app = cyclopts.App(help="This is the help string for the 'foo' subcommand.")
45 app.command(sub_app, name="foo")
46 app.command(sub_app, name="foo", help="This is illegal and raises a ValueError.")
47
48
49 3. The ``__doc__`` docstring of the registered :meth:`@app.default <cyclopts.App.default>` command.
50 Cyclopts parses the docstring to populate short-descriptions and long-descriptions
51 at the command-level, as well as at the parameter-level.
52
53 .. code-block:: python
54
55 app = cyclopts.App()
56 app.command(cyclopts.App(), name="foo")
57
58
59 @app.default
60 def bar(val1: str):
61 """This is the primary application docstring.
62
63 Parameters
64 ----------
65 val1: str
66 This will be parsed for val1 help-string.
67 """
68
69
70 @app["foo"].default # You can access sub-apps like a dictionary.
71 def foo_handler():
72 """This will be shown for the "foo" command."""
73
74 .. note::
75 Docstrings should always use the **Python variable name** from the function signature.
76
77 .. code-block:: python
78
79 @app.default
80 def main(internal_name: Annotated[str, Parameter(name="external-name")]):
81 """Command description.
82
83 Parameters
84 ----------
85 internal_name: # Use the Python variable name
86 Help text here.
87 """
88
89 This follows standard Python documentation conventions; the parameter will still appear as ``--external-name`` on the CLI.
90
91 4. This resolution order, but of the :ref:`Meta App`.
92
93 .. code-block:: python
94
95 app = cyclopts.App()
96
97
98 @app.meta.default
99 def bar():
100 """This is the primary application docstring."""
101
102 -------------
103 Markup Format
104 -------------
105 While the standard markup language for docstrings in Python is reStructuredText (see `PEP-0287`_), Cyclopts defaults to Markdown for better readability and simplicity.
106 Cyclopts mostly respects `PEP-0257`_, but has some slight differences for developer ergonomics:
107
108 1. The "summary line" (AKA short-description) may actually be multiple lines. Cyclopts will unwrap the first block of text and interpret it as the short description. The first block of text ends at the first double-newline (i.e. a single blank line) is reached.
109
110 .. code-block:: python
111
112 def my_command():
113 """
114 This entire sentence
115 is part of the short description and will
116 have all the newlines removed.
117
118 This is the beginning of the long description.
119 """
120
121 2. If a docstring is provided with a long description, it **must** also have a short description.
122
123 By default, Cyclopts parses docstring descriptions as markdown and renders it appropriately.
124 To change the markup format, set the :attr:`.App.help_format` field accordingly. The different options are described below.
125
126 Subapps inherit their parent's :attr:`.App.help_format` unless explicitly overridden. I.e. you only need
127 to set :attr:`.App.help_format` in your main root application for all docstrings to be parsed appropriately.
128
129 ^^^^^^^^^
130 PlainText
131 ^^^^^^^^^
132 Do not perform any additional parsing, display supplied text as-is.
133
134 .. code-block:: python
135
136 from cyclopts import App
137
138 app = App(help_format="plaintext")
139
140 @app.default
141 def default():
142 """My application summary.
143
144 This is a pretty standard docstring; if there's a really long sentence
145 I should probably wrap it because people don't like code that is more
146 than 80 columns long.
147
148 In this new paragraph, I would like to discuss the benefits of relaxing 80 cols to 120 cols.
149 More text in this paragraph.
150
151 Some new paragraph.
152 """
153
154 app()
155
156 .. code-block:: text
157
158 Usage: default COMMAND
159
160 My application summary.
161
162 This is a pretty standard docstring; if there's a really long
163 sentence
164 I should probably wrap it because people don't like code that is
165 more
166 than 80 columns long.
167
168 In this new paragraph, I would like to discuss the benefits of
169 relaxing 80 cols to 120 cols.
170 More text in this paragraph.
171
172 Some new paragraph.
173
174 ╭─ Commands ─────────────────────────────────────────────────────╮
175 │ --help,-h Display this message and exit. │
176 │ --version Display application version. │
177 ╰────────────────────────────────────────────────────────────────╯
178
179 Most noteworthy, is **no additional text reflow is performed**; newlines are presented as-is.
180
181 ^^^^
182 Rich
183 ^^^^
184 Displays text as `Rich Markup`_.
185
186 .. note::
187
188 Newlines are interpreted literally.
189
190 .. code-block:: python
191
192 from cyclopts import App
193
194 app = App(help_format="rich")
195
196 @app.default
197 def default():
198 """Rich can display colors like [red]red[/red] easily.
199
200 However, I cannot be bothered to figure out how to show that in documentation.
201 """
202
203 app()
204
205 .. raw:: html
206
207 <div class="highlight-default notranslate">
208 <pre style="font-family: monospace;">Usage: default COMMAND
209
210 Rich can display colors like <span style="color: #cc3333">red</span> easily.
211
212 ╭─ Commands ───────────────────────────────────────────────────────╮
213 │ <span style="color: #0088cc">--help -h </span>Display this message and exit. │
214 │ <span style="color: #0088cc">--version </span>Display application version. │
215 ╰──────────────────────────────────────────────────────────────────╯</pre>
216 </div>
217
218 ^^^^^^^^^^^^^^^^
219 ReStructuredText
220 ^^^^^^^^^^^^^^^^
221 ReStructuredText can be enabled by setting `help_format` to "restructuredtext" or "rst".
222
223 .. code-block:: python
224
225 app = App(help_format="restructuredtext") # or "rst"
226
227 @app.default
228 def default():
229 """My application summary.
230
231 We can do RST things like have **bold text**.
232 More words in this paragraph.
233
234 This is a new paragraph with some bulletpoints below:
235
236 * bullet point 1.
237 * bullet point 2.
238 """
239
240 app()
241
242
243 Resulting help:
244
245 .. raw:: html
246
247 <div class="highlight-default notranslate">
248 <pre style="font-family: monospace;">Usage: default COMMAND
249
250 My application summary.
251
252 We can do RST things like have <span style="font-weight: bold">bold text</span>. More words in this
253 paragraph.
254
255 This is a new paragraph with some bulletpoints below:
256
257 1. bullet point 1.
258 2. bullet point 2.
259
260 ╭─ Commands ──────────────────────────────────────────────────────────╮
261 │ <span style="color: #0088cc">--help -h </span>Display this message and exit. │
262 │ <span style="color: #0088cc">--version </span>Display application version. │
263 ╰─────────────────────────────────────────────────────────────────────╯
264 </pre></div>
265
266 Under most circumstances, plaintext (without any additional markup) looks prettier and reflows better when interpreted as restructuredtext (or markdown, for that matter).
267
268 ^^^^^^^^^
269 Markdown
270 ^^^^^^^^^
271 Markdown is the default parsing behavior of Cyclopts, so `help_format` won't need to be explicitly set. It's another popular markup language that Cyclopts can render.
272
273 .. code-block:: python
274
275 app = App(help_format="markdown") # or "md"
276 # or don't supply help_format at all; markdown is default.
277
278
279 @app.default
280 def default():
281 """My application summary.
282
283 We can do markdown things like have **bold text**.
284 [Hyperlinks work as well.](https://cyclopts.readthedocs.io)
285 """
286
287 Resulting help:
288
289 .. raw:: html
290
291 <div class="highlight-default notranslate">
292 <pre style="font-family: monospace;">Usage: default COMMAND
293
294 My application summary.
295
296 We can do markdown things like have <span style="font-weight: bold">bold text</span>. <a href="https://cyclopts.readthedocs.io" style="color: #0088cc">Hyperlinks work as well</a>.
297
298 ╭─ Commands ──────────────────────────────────────────────────────────╮
299 │ <span style="color: #0088cc">--help -h </span>Display this message and exit. │
300 │ <span style="color: #0088cc">--version </span>Display application version. │
301 ╰─────────────────────────────────────────────────────────────────────╯
302 </pre></div>
303
304 ----------
305 Help Flags
306 ----------
307 The default ``--help`` flags can be changed to different name(s) via the ``help_flags`` parameter.
308
309 .. code-block:: python
310
311 app = cyclopts.App(help_flags="--show-help")
312 app = cyclopts.App(help_flags=["--send-help", "--send-help-plz", "-h"])
313
314 To disable the help-page entirely, set ``help_flags`` to an empty string or iterable.
315
316 .. code-block:: python
317
318 app = cyclopts.App(help_flags="")
319 app = cyclopts.App(help_flags=[])
320
321 --------------------------
322 Help Prologue and Epilogue
323 --------------------------
324 An epilogue is text displayed at the end of the help screen, after all command and parameter panels.
325 A prologue is text display at the beginning of the help screen, before the usage section.
326 This is commonly used for version information, support contact details, or additional notes.
327
328 The epilogue is set via the :attr:`.App.help_epilogue` attribute:
329
330 .. code-block:: python
331
332 from cyclopts import App
333
334 app = App(
335 name="myapp",
336 help="My application description.",
337 help_epilogue="Support: [email protected]"
338 )
339
340 @app.default
341 def main():
342 """Main command."""
343 pass
344
345 app()
346
347 .. code-block:: console
348
349 $ myapp --help
350 Usage: myapp [ARGS]
351
352 My application description.
353
354 ╭─ Commands ────────────────────────────────────────────────────────────╮
355 │ --help -h Display this message and exit. │
356 │ --version Display application version. │
357 ╰───────────────────────────────────────────────────────────────────────╯
358
359 Support: [email protected]
360
361 The prologue is set via the :attr:`.App.help_prologue` attribute:
362
363 .. code-block:: python
364
365 from cyclopts import App
366
367 app = App(
368 name="myapp",
369 help="My application help.",
370 help_prologue=f"myapp, v1.0.0 (http://example.myapp.com)"
371 )
372 app()
373
374 .. code-block:: console
375
376 $ my-script --help
377 myapp, v1.0.0 (http://example.myapp.com)
378
379 Usage: myapp COMMAND
380
381 My application help.
382
383 ╭─ Commands ────────────────────────────────────────────────────────────╮
384 │ --help -h Display this message and exit. │
385 │ --version Display application version. │
386 ╰───────────────────────────────────────────────────────────────────────╯
387
388 Like :attr:`.App.help_format`, epilogues and prologues inherit from parent to child apps.
389 This allows you to set a single epilogue or prologue that applies across your entire application:
390
391 .. code-block:: python
392
393 parent = App(
394 name="myapp",
395 help_epilogue="Version 1.0.0 | [email protected]"
396 )
397
398 # Child inherits parent's epilogue
399 child = App(name="process", help="Process data files.")
400 parent.command(child)
401
402 # Another child overrides with its own epilogue
403 admin = App(
404 name="admin",
405 help="Admin commands.",
406 help_epilogue="Admin Tools v2.0 | USE WITH CAUTION"
407 )
408 parent.command(admin)
409
410 parent()
411
412 .. code-block:: console
413
414 $ myapp process --help
415 Usage: myapp process
416
417 Process data files.
418
419 Version 1.0.0 | [email protected] # Inherited from parent
420
421 $ myapp admin --help
422 Usage: myapp admin
423
424 Admin commands.
425
426 Admin Tools v2.0 | USE WITH CAUTION # Overridden by child
427
428 To disable the epilogue or prologue for a specific subcommand, set it to an empty string:
429
430 .. code-block:: python
431
432 no_epilogue = App(name="internal", help_epilogue="")
433 parent.command(no_epilogue)
434
435 .. code-block:: python
436
437 no_prologue = App(name="internal", help_prologue="")
438 parent.command(no_prologue)
439
440 --------------------
441 Help Customization
442 --------------------
443 For advanced customization of help screen appearance, including custom formatters,
444 styled panels, and dynamic column layouts, see :ref:`Help Customization`.
445
446
447 .. _PEP-0257: https://peps.python.org/pep-0257/
448 .. _PEP-0287: https://peps.python.org/pep-0287/
449 .. _Rich Markup: https://rich.readthedocs.io/en/stable/markup.html