File: help.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (291 lines) | stat: -rw-r--r-- 10,357 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
====
Help
====

A help screen is standard for every CLI application.
Cyclopts by-default adds ``--help`` and ``-h`` flags to the application:

.. code-block:: console

   $ my-application --help
   Usage: my-application COMMAND

   My application short description.

   ╭─ Commands ─────────────────────────────────────────────────────────╮
   │ foo        Foo help string.                                        │
   │ bar        Bar help string.                                        │
   │ --help -h  Display this message and exit.                          │
   │ --version  Display application version.                            │
   ╰────────────────────────────────────────────────────────────────────╯

Cyclopts derives the components of the help string from a variety of sources.
The source resolution order is as follows (as applicable):

1. The ``help`` field in the :meth:`@app.command <cyclopts.App.command>` decorator.

   .. code-block:: python

      app = cyclopts.App()


      @app.command(help="This is the highest precedence help-string for 'bar'.")
      def bar():
          pass

   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).

2. Via :attr:`.App.help`.

   .. code-block:: python

      app = cyclopts.App(help="This help string has highest precedence at the app-level.")

      sub_app = cyclopts.App(help="This is the help string for the 'foo' subcommand.")
      app.command(sub_app, name="foo")
      app.command(sub_app, name="foo", help="This is illegal and raises a ValueError.")


3. The ``__doc__`` docstring of the registered :meth:`@app.default <cyclopts.App.default>` command.
   Cyclopts parses the docstring to populate short-descriptions and long-descriptions
   at the command-level, as well as at the parameter-level.

   .. code-block:: python

      app = cyclopts.App()
      app.command(cyclopts.App(), name="foo")


      @app.default
      def bar(val1: str):
          """This is the primary application docstring.

          Parameters
          ----------
          val1: str
              This will be parsed for val1 help-string.
          """


      @app["foo"].default  # You can access sub-apps like a dictionary.
      def foo_handler():
          """This will be shown for the "foo" command."""


4. This resolution order, but of the :ref:`Meta App`.

   .. code-block:: python

      app = cyclopts.App()


      @app.meta.default
      def bar():
          """This is the primary application docstring."""

-------------
Markup Format
-------------
The standard markup language for docstrings in python is reStructuredText (see `PEP-0287`_).
By default, Cyclopts parses docstring descriptions as restructuredtext and renders it appropriately.
To change the markup format, set the :attr:`.App.help_format` field accordingly.

Subapps inherit their parent's :attr:`.App.help_format` unless explicitly overridden. I.e. you only need
to set :attr:`.App.help_format` in your main root application for all docstrings to be parsed appropriately.

^^^^^^^^^
PlainText
^^^^^^^^^
Do not perform any additional parsing, display supplied text as-is.

.. code-block:: python

   from cyclopts import App

   app = App(help_format="plaintext")

   @app.default
   def default():
       """My application summary.

       This is a pretty standard docstring; if there's a really long sentence
       I should probably wrap it because people don't like code that is more
       than 80 columns long.

       In this new paragraph, I would like to discuss the benefits of relaxing 80 cols to 120 cols.
       More text in this paragraph.

       Some new paragraph.
       """

   app()

.. code-block:: text

   Usage: default COMMAND

   My application summary.

   This is a pretty standard docstring; if there's a really long
   sentence
   I should probably wrap it because people don't like code that is
   more
   than 80 columns long.

   In this new paragraph, I would like to discuss the benefits of
   relaxing 80 cols to 120 cols.
   More text in this paragraph.

   Some new paragraph.

   ╭─ Commands ─────────────────────────────────────────────────────╮
   │ --help,-h  Display this message and exit.                      │
   │ --version  Display application version.                        │
   ╰────────────────────────────────────────────────────────────────╯

Most noteworthy, is **no additional text reflow is performed**; newlines are presented as-is.

^^^^
Rich
^^^^
Displays text as `Rich Markup`_.

.. note::

      Newlines are interpreted literally.

.. code-block:: python

   from cyclopts import App

   app = App(help_format="rich")

   @app.default
   def default():
      """Rich can display colors like [red]red[/red] easily.

      However, I cannot be bothered to figure out how to show that in documentation.
      """

   app()

.. raw:: html

   <div class="highlight-default notranslate">
         <pre style="font-family: 'JetBrains Mono', 'Fira Code', Monaco, Consolas, monospace;">Usage: default COMMAND

   Rich can display colors like <span style="color: #ff6666">red</span> easily.

   ╭─ Commands ───────────────────────────────────────────────────────╮
   │ <span style="color: #66b3ff">--help -h  </span>Display this message and exit.                        │
   │ <span style="color: #66b3ff">--version  </span>Display application version.                          │
   ╰──────────────────────────────────────────────────────────────────╯</pre>
   </div>

^^^^^^^^^^^^^^^^
ReStructuredText
^^^^^^^^^^^^^^^^
ReStructuredText is the default parsing behavior of Cyclopts, so `help_format` won't need to be explicitly set.

.. code-block:: python

   app = App(help_format="restructuredtext")  # or "rst"
   # or don't supply help_format at all; rst is default.

   @app.default
   def default():
       """My application summary.

       We can do RST things like have **bold text**.
       More words in this paragraph.

       This is a new paragraph with some bulletpoints below:

       * bullet point 1.
       * bullet point 2.
       """

   app()


Resulting help:

.. raw:: html

   <div class="highlight-default notranslate">
         <pre style="font-family: 'JetBrains Mono', 'Fira Code', Monaco, Consolas, monospace;">Usage: default COMMAND

   My application summary.

   We can do RST things like have <span style="font-weight: bold">bold text</span>. More words in this
   paragraph.

   This is a new paragraph with some bulletpoints below:

   1. bullet point 1.
   2. bullet point 2.

   ╭─ Commands ──────────────────────────────────────────────────────────╮
   │ <span style="color: #66b3ff">--help -h  </span>Display this message and exit.                           │
   │ <span style="color: #66b3ff">--version  </span>Display application version.                             │
   ╰─────────────────────────────────────────────────────────────────────╯
   </pre></div>

Under most circumstances, plaintext (without any additional markup) looks prettier and reflows better when interpreted as restructuredtext (or markdown, for that matter).

^^^^^^^^^
Markdown
^^^^^^^^^
Markdown is another popular markup language that Cyclopts can render.

.. code-block:: python

   app = App(help_format="markdown")  # or "md"


   @app.default
   def default():
       """My application summary.

       We can do markdown things like have **bold text**.
       [Hyperlinks work as well.](https://cyclopts.readthedocs.io)
       """

Resulting help:

.. raw:: html

   <div class="highlight-default notranslate">
         <pre style="font-family: 'JetBrains Mono', 'Fira Code', Monaco, Consolas, monospace;">Usage: default COMMAND

   My application summary.

   We can do markdown things like have <span style="font-weight: bold">bold text</span>. <a href="https://cyclopts.readthedocs.io" style="color: #66b3ff">Hyperlinks work as well</a>.

   ╭─ Commands ──────────────────────────────────────────────────────────╮
   │ <span style="color: #66b3ff">--help -h  </span>Display this message and exit.                           │
   │ <span style="color: #66b3ff">--version  </span>Display application version.                             │
   ╰─────────────────────────────────────────────────────────────────────╯
   </pre></div>

----------
Help Flags
----------
The default ``--help`` flags can be changed to different name(s) via the ``help_flags`` parameter.

.. code-block:: python

   app = cyclopts.App(help_flags="--show-help")
   app = cyclopts.App(help_flags=["--send-help", "--send-help-plz", "-h"])

To disable the help-page entirely, set ``help_flags`` to an empty string or iterable.

.. code-block:: python

   app = cyclopts.App(help_flags="")
   app = cyclopts.App(help_flags=[])


.. _PEP-0257: https://peps.python.org/pep-0257/
.. _PEP-0287: https://peps.python.org/pep-0287/
.. _Rich Markup: https://rich.readthedocs.io/en/stable/markup.html