| 1 |
.. _Help Customization: |
| 2 |
|
| 3 |
================== |
| 4 |
Help Customization |
| 5 |
================== |
| 6 |
|
| 7 |
Cyclopts provides extensive customization options for help screen appearance and formatting through the ``help_formatter`` parameter available on both :attr:`App <cyclopts.App.help_formatter>` and :attr:`Group <cyclopts.Group.help_formatter>`. These parameters accept formatters that follow the :class:`~cyclopts.help.protocols.HelpFormatter` protocol. |
| 8 |
|
| 9 |
-------------------------- |
| 10 |
Setting Help Formatters |
| 11 |
-------------------------- |
| 12 |
|
| 13 |
App-Level Formatting |
| 14 |
^^^^^^^^^^^^^^^^^^^^ |
| 15 |
|
| 16 |
The :class:`~cyclopts.App` class accepts a ``help_formatter`` parameter that controls the default formatting for all help output: |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
from cyclopts import App |
| 21 |
from cyclopts.help import DefaultFormatter, PlainFormatter |
| 22 |
|
| 23 |
# Use a built-in formatter by name: {"default", "plain"} |
| 24 |
app = App(help_formatter="plain") |
| 25 |
|
| 26 |
# Or pass a formatter instance with custom configuration |
| 27 |
app = App( |
| 28 |
help_formatter=DefaultFormatter( |
| 29 |
# Custom configuration options |
| 30 |
) |
| 31 |
) |
| 32 |
|
| 33 |
# Or use a completely custom formatter; see HelpFormatter protocol. |
| 34 |
app = App(help_formatter=MyCustomFormatter()) |
| 35 |
|
| 36 |
Group-Level Formatting |
| 37 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 38 |
|
| 39 |
Individual :class:`~cyclopts.Group` instances can have their own ``help_formatter`` that overrides the app-level default: |
| 40 |
|
| 41 |
.. code-block:: python |
| 42 |
|
| 43 |
from cyclopts import App, Group |
| 44 |
from cyclopts.help import DefaultFormatter, PanelSpec |
| 45 |
from rich.box import DOUBLE |
| 46 |
|
| 47 |
# Create a group with custom formatting |
| 48 |
advanced_group = Group( |
| 49 |
"Advanced Options", |
| 50 |
help_formatter=DefaultFormatter( |
| 51 |
panel_spec=PanelSpec( |
| 52 |
border_style="red", |
| 53 |
box=DOUBLE, |
| 54 |
) |
| 55 |
) |
| 56 |
) |
| 57 |
|
| 58 |
# The app can have a different default formatter |
| 59 |
app = App(help_formatter="plain") |
| 60 |
|
| 61 |
# Parameters in advanced_group will use the group's formatter, |
| 62 |
# while other parameters use the app's formatter |
| 63 |
|
| 64 |
------------------- |
| 65 |
Built-in Formatters |
| 66 |
------------------- |
| 67 |
|
| 68 |
DefaultFormatter |
| 69 |
^^^^^^^^^^^^^^^^ |
| 70 |
|
| 71 |
The :class:`~cyclopts.help.DefaultFormatter` is the default help formatter that uses |
| 72 |
`Rich <https://github.com/Textualize/rich>`_ for beautiful terminal output with colors, |
| 73 |
borders, and structured layouts. |
| 74 |
|
| 75 |
.. code-block:: python |
| 76 |
|
| 77 |
from cyclopts import App |
| 78 |
|
| 79 |
# Explicitly use the default formatter (same as not specifying) |
| 80 |
app = App(help_formatter="default") |
| 81 |
|
| 82 |
@app.default |
| 83 |
def main(name: str, count: int = 1): |
| 84 |
"""A simple greeting application. |
| 85 |
|
| 86 |
Parameters |
| 87 |
---------- |
| 88 |
name : str |
| 89 |
Person to greet. |
| 90 |
count : int |
| 91 |
Number of times to greet. |
| 92 |
""" |
| 93 |
for _ in range(count): |
| 94 |
print(f"Hello, {name}!") |
| 95 |
|
| 96 |
if __name__ == "__main__": |
| 97 |
app() |
| 98 |
|
| 99 |
Output: |
| 100 |
|
| 101 |
.. raw:: html |
| 102 |
|
| 103 |
<div class="highlight-default notranslate"> |
| 104 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: my-app [ARGS] [OPTIONS]</span> |
| 105 |
|
| 106 |
A simple greeting application. |
| 107 |
|
| 108 |
╭─ Commands ───────────────────────────────────────────────────────────────────╮ |
| 109 |
│ <span style="color: #0088cc">--help -h</span> Display this message and exit. │ |
| 110 |
│ <span style="color: #0088cc">--version</span> Display application version. │ |
| 111 |
╰──────────────────────────────────────────────────────────────────────────────╯ |
| 112 |
╭─ Parameters ─────────────────────────────────────────────────────────────────╮ |
| 113 |
│ <span style="color: #cc3333; font-weight: bold">*</span> <span style="color: #0088cc">NAME --name</span> Person to greet. <span style="color: #cc3333; opacity: 0.7">[required]</span> │ |
| 114 |
│ <span style="color: #0088cc">COUNT --count</span> Number of times to greet. <span style="opacity: 0.7">[default: 1]</span> │ |
| 115 |
╰──────────────────────────────────────────────────────────────────────────────╯</pre> |
| 116 |
</div> |
| 117 |
|
| 118 |
PlainFormatter |
| 119 |
^^^^^^^^^^^^^^ |
| 120 |
|
| 121 |
The :class:`~cyclopts.help.PlainFormatter` provides accessibility-focused plain text output |
| 122 |
without colors or special characters, ideal for screen readers and simpler terminals. |
| 123 |
|
| 124 |
.. code-block:: python |
| 125 |
|
| 126 |
from cyclopts import App |
| 127 |
|
| 128 |
# Use plain text formatter for accessibility |
| 129 |
app = App(help_formatter="plain") |
| 130 |
|
| 131 |
@app.default |
| 132 |
def main(name: str, count: int = 1): |
| 133 |
"""A simple greeting application. |
| 134 |
|
| 135 |
Parameters |
| 136 |
---------- |
| 137 |
name : str |
| 138 |
Person to greet. |
| 139 |
count : int |
| 140 |
Number of times to greet. |
| 141 |
""" |
| 142 |
for _ in range(count): |
| 143 |
print(f"Hello, {name}!") |
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
app() |
| 147 |
|
| 148 |
Output: |
| 149 |
|
| 150 |
.. code-block:: text |
| 151 |
|
| 152 |
Usage: demo.py [ARGS] [OPTIONS] |
| 153 |
|
| 154 |
A simple greeting application. |
| 155 |
|
| 156 |
Commands: |
| 157 |
--help, -h: Display this message and exit. |
| 158 |
--version: Display application version. |
| 159 |
|
| 160 |
Parameters: |
| 161 |
NAME, --name: Person to greet. |
| 162 |
COUNT, --count: Number of times to greet. |
| 163 |
|
| 164 |
--------------------- |
| 165 |
Basic Customization |
| 166 |
--------------------- |
| 167 |
|
| 168 |
The :class:`~cyclopts.help.DefaultFormatter` accepts several customization options |
| 169 |
through its initialization parameters. |
| 170 |
|
| 171 |
Panel Customization |
| 172 |
^^^^^^^^^^^^^^^^^^^ |
| 173 |
|
| 174 |
The :class:`~cyclopts.help.PanelSpec` controls the outer panel appearance: |
| 175 |
|
| 176 |
.. code-block:: python |
| 177 |
|
| 178 |
from cyclopts import App |
| 179 |
from cyclopts.help import DefaultFormatter, PanelSpec |
| 180 |
from rich.box import DOUBLE |
| 181 |
|
| 182 |
app = App( |
| 183 |
help_formatter=DefaultFormatter( |
| 184 |
panel_spec=PanelSpec( |
| 185 |
box=DOUBLE, # Use double-line borders |
| 186 |
border_style="blue", # Blue border color |
| 187 |
padding=(1, 2), # (vertical, horizontal) padding |
| 188 |
expand=True, # Expand to full terminal width |
| 189 |
) |
| 190 |
) |
| 191 |
) |
| 192 |
|
| 193 |
@app.default |
| 194 |
def main(path: str, verbose: bool = False): |
| 195 |
"""Process a file with custom panel styling.""" |
| 196 |
print(f"Processing {path}") |
| 197 |
|
| 198 |
if __name__ == "__main__": |
| 199 |
app() |
| 200 |
|
| 201 |
Output: |
| 202 |
|
| 203 |
.. raw:: html |
| 204 |
|
| 205 |
<div class="highlight-default notranslate"> |
| 206 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: demo.py [ARGS] [OPTIONS]</span> |
| 207 |
|
| 208 |
Process a file with custom panel styling. |
| 209 |
|
| 210 |
<span style="color: #0088cc">╔═ Commands ═══════════════════════════════════════════════════════════╗</span> |
| 211 |
<span style="color: #0088cc">║ ║</span> |
| 212 |
<span style="color: #0088cc">║ </span><span style="color: #0088cc">--help -h </span>Display this message and exit. <span style="color: #0088cc">║</span> |
| 213 |
<span style="color: #0088cc">║ </span><span style="color: #0088cc">--version </span>Display application version. <span style="color: #0088cc">║</span> |
| 214 |
<span style="color: #0088cc">║ ║</span> |
| 215 |
<span style="color: #0088cc">╚══════════════════════════════════════════════════════════════════════╝</span> |
| 216 |
<span style="color: #0088cc">╔═ Parameters ═════════════════════════════════════════════════════════╗</span> |
| 217 |
<span style="color: #0088cc">║ ║</span> |
| 218 |
<span style="color: #0088cc">║ </span><span style="color: #cc3333; font-weight: bold">* </span><span style="color: #0088cc">PATH --path </span> <span style="color: #cc3333; opacity: 0.7">[required]</span> <span style="color: #0088cc">║</span> |
| 219 |
<span style="color: #0088cc">║ </span><span style="color: #0088cc">VERBOSE --verbose</span> <span style="opacity: 0.7">[default: False]</span> <span style="color: #0088cc">║</span> |
| 220 |
<span style="color: #0088cc">║ </span><span style="color: #0088cc">--no-verbose </span> <span style="color: #0088cc">║</span> |
| 221 |
<span style="color: #0088cc">║ ║</span> |
| 222 |
<span style="color: #0088cc">╚══════════════════════════════════════════════════════════════════════╝</span></pre> |
| 223 |
</div> |
| 224 |
|
| 225 |
|
| 226 |
Table Customization |
| 227 |
^^^^^^^^^^^^^^^^^^^ |
| 228 |
|
| 229 |
The :class:`~cyclopts.help.TableSpec` controls the table styling within panels: |
| 230 |
|
| 231 |
.. code-block:: python |
| 232 |
|
| 233 |
from cyclopts import App |
| 234 |
from cyclopts.help import DefaultFormatter, TableSpec |
| 235 |
|
| 236 |
app = App( |
| 237 |
help_formatter=DefaultFormatter( |
| 238 |
table_spec=TableSpec( |
| 239 |
show_header=True, # Show column headers |
| 240 |
show_lines=True, # Show lines between rows |
| 241 |
show_edge=False, # Remove outer table border |
| 242 |
border_style="green", # Green table elements |
| 243 |
padding=(0, 2, 0, 0), # Extra right padding |
| 244 |
box=SQUARE, # otherwise we won't see the lines |
| 245 |
) |
| 246 |
) |
| 247 |
) |
| 248 |
|
| 249 |
@app.default |
| 250 |
def main(path: str, verbose: bool = False): |
| 251 |
"""Process a file with custom table styling.""" |
| 252 |
print(f"Processing {path}") |
| 253 |
|
| 254 |
if __name__ == "__main__": |
| 255 |
app() |
| 256 |
|
| 257 |
Output: |
| 258 |
|
| 259 |
.. raw:: html |
| 260 |
|
| 261 |
<div class="highlight-default notranslate"> |
| 262 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: test_table_custom.py [ARGS] [OPTIONS]</span> |
| 263 |
|
| 264 |
Process a file with custom table styling. |
| 265 |
|
| 266 |
╭─ Commands ───────────────────────────────────────────────────────────────────╮ |
| 267 |
│ Command <span style="color: #00aa00">│</span>Description │ |
| 268 |
│ <span style="color: #00aa00">───────────┼────────────────────────────────────────────────────────────────</span> │ |
| 269 |
│ <span style="color: #0088cc">--help -h</span> <span style="color: #00aa00">│</span>Display this message and exit. │ |
| 270 |
│ <span style="color: #00aa00">───────────┼────────────────────────────────────────────────────────────────</span> │ |
| 271 |
│ <span style="color: #0088cc">--version</span> <span style="color: #00aa00">│</span>Display application version. │ |
| 272 |
╰──────────────────────────────────────────────────────────────────────────────╯ |
| 273 |
╭─ Parameters ─────────────────────────────────────────────────────────────────╮ |
| 274 |
│ <span style="color: #00aa00">│</span>Option <span style="color: #00aa00">│</span>Description │ |
| 275 |
│ <span style="color: #00aa00">───┼───────────────────┼────────────────────────────────────────────────────</span> │ |
| 276 |
│ <span style="color: #cc3333; font-weight: bold">*</span> <span style="color: #00aa00">│</span><span style="color: #0088cc">PATH --path</span> <span style="color: #00aa00">│</span><span style="color: #cc3333; opacity: 0.7">[required]</span> │ |
| 277 |
│ <span style="color: #00aa00">───┼───────────────────┼────────────────────────────────────────────────────</span> │ |
| 278 |
│ <span style="color: #00aa00">│</span><span style="color: #0088cc">VERBOSE --verbose</span> <span style="color: #00aa00">│</span><span style="opacity: 0.7">[default: False]</span> │ |
| 279 |
│ <span style="color: #00aa00">│</span><span style="color: #0088cc"> --no-verbose</span> <span style="color: #00aa00">│</span> │ |
| 280 |
╰──────────────────────────────────────────────────────────────────────────────╯</pre> |
| 281 |
</div> |
| 282 |
|
| 283 |
Combining Customizations |
| 284 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 285 |
|
| 286 |
You can combine both panel and table specifications: |
| 287 |
|
| 288 |
.. code-block:: python |
| 289 |
|
| 290 |
from cyclopts import App |
| 291 |
from cyclopts.help import DefaultFormatter, PanelSpec, TableSpec |
| 292 |
from rich.box import ROUNDED |
| 293 |
|
| 294 |
app = App( |
| 295 |
help_formatter=DefaultFormatter( |
| 296 |
panel_spec=PanelSpec( |
| 297 |
box=ROUNDED, |
| 298 |
border_style="cyan", |
| 299 |
padding=(0, 1), |
| 300 |
), |
| 301 |
table_spec=TableSpec( |
| 302 |
show_header=False, |
| 303 |
show_lines=False, |
| 304 |
padding=(0, 1), |
| 305 |
) |
| 306 |
) |
| 307 |
) |
| 308 |
|
| 309 |
@app.default |
| 310 |
def main(path: str, verbose: bool = False): |
| 311 |
"""Process a file with combined customizations.""" |
| 312 |
print(f"Processing {path}") |
| 313 |
|
| 314 |
if __name__ == "__main__": |
| 315 |
app() |
| 316 |
|
| 317 |
Output: |
| 318 |
|
| 319 |
.. raw:: html |
| 320 |
|
| 321 |
<div class="highlight-default notranslate"> |
| 322 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: my-app [ARGS] [OPTIONS]</span> |
| 323 |
|
| 324 |
Process a file with combined customizations. |
| 325 |
|
| 326 |
<span style="color: #00aaaa">╭─ Commands ──────────────────────────────────────────────────────────╮</span> |
| 327 |
<span style="color: #00aaaa">│ </span><span style="color: #0088cc">--help -h</span> Display this message and exit. <span style="color: #00aaaa">│</span> |
| 328 |
<span style="color: #00aaaa">│ </span><span style="color: #0088cc">--version</span> Display application version. <span style="color: #00aaaa">│</span> |
| 329 |
<span style="color: #00aaaa">╰─────────────────────────────────────────────────────────────────────╯</span> |
| 330 |
<span style="color: #00aaaa">╭─ Parameters ────────────────────────────────────────────────────────╮</span> |
| 331 |
<span style="color: #00aaaa">│ </span><span style="color: #cc3333; font-weight: bold">*</span> <span style="color: #0088cc">PATH --path</span> <span style="color: #cc3333; opacity: 0.7">[required]</span> <span style="color: #00aaaa">│</span> |
| 332 |
<span style="color: #00aaaa">│ </span><span style="color: #0088cc">VERBOSE --verbose</span> <span style="opacity: 0.7">[default: False]</span> <span style="color: #00aaaa">│</span> |
| 333 |
<span style="color: #00aaaa">╰─────────────────────────────────────────────────────────────────────╯</span></pre> |
| 334 |
</div> |
| 335 |
|
| 336 |
----------------------- |
| 337 |
Group-Level Formatting |
| 338 |
----------------------- |
| 339 |
|
| 340 |
Different parameter groups can have different formatting styles, allowing you to |
| 341 |
visually distinguish between different types of options: |
| 342 |
|
| 343 |
.. code-block:: python |
| 344 |
|
| 345 |
from cyclopts import App, Group, Parameter |
| 346 |
from cyclopts.help import DefaultFormatter, PanelSpec |
| 347 |
from rich.box import DOUBLE, MINIMAL |
| 348 |
from typing import Annotated |
| 349 |
|
| 350 |
# Create groups with different styles |
| 351 |
required_group = Group( |
| 352 |
"Required Options", |
| 353 |
help_formatter=DefaultFormatter( |
| 354 |
panel_spec=PanelSpec( |
| 355 |
box=DOUBLE, |
| 356 |
border_style="red bold", |
| 357 |
) |
| 358 |
) |
| 359 |
) |
| 360 |
|
| 361 |
optional_group = Group( |
| 362 |
"Optional Settings", |
| 363 |
help_formatter=DefaultFormatter( |
| 364 |
panel_spec=PanelSpec( |
| 365 |
box=MINIMAL, |
| 366 |
border_style="green", |
| 367 |
) |
| 368 |
) |
| 369 |
) |
| 370 |
|
| 371 |
app = App() |
| 372 |
|
| 373 |
@app.default |
| 374 |
def main( |
| 375 |
# Required parameters with red double border |
| 376 |
input_file: Annotated[str, Parameter(group=required_group)], |
| 377 |
output_dir: Annotated[str, Parameter(group=required_group)], |
| 378 |
|
| 379 |
# Optional parameters with green minimal border |
| 380 |
verbose: Annotated[bool, Parameter(group=optional_group)] = False, |
| 381 |
threads: Annotated[int, Parameter(group=optional_group)] = 4, |
| 382 |
): |
| 383 |
"""Process files with styled help groups.""" |
| 384 |
print(f"Processing {input_file} -> {output_dir}") |
| 385 |
if verbose: |
| 386 |
print(f"Using {threads} threads") |
| 387 |
|
| 388 |
if __name__ == "__main__": |
| 389 |
app() |
| 390 |
|
| 391 |
Output: |
| 392 |
|
| 393 |
.. raw:: html |
| 394 |
|
| 395 |
<div class="highlight-default notranslate"> |
| 396 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: test_group_formatting.py [ARGS] [OPTIONS]</span> |
| 397 |
|
| 398 |
Process files with styled help groups. |
| 399 |
|
| 400 |
╭─ Commands ───────────────────────────────────────────────────────────────────╮ |
| 401 |
│ <span style="color: #0088cc">--help -h</span> Display this message and exit. │ |
| 402 |
│ <span style="color: #0088cc">--version</span> Display application version. │ |
| 403 |
╰──────────────────────────────────────────────────────────────────────────────╯ |
| 404 |
<span style="color: #00aa00"> Optional Settings </span> |
| 405 |
<span style="color: #00aa00"> </span> <span style="color: #0088cc">VERBOSE --verbose</span> <span style="opacity: 0.7">[default: False]</span> <span style="color: #00aa00"> </span> |
| 406 |
<span style="color: #00aa00"> </span> <span style="color: #0088cc"> --no-verbose</span> <span style="opacity: 0.7"></span> <span style="color: #00aa00"> </span> |
| 407 |
<span style="color: #00aa00"> </span> <span style="color: #0088cc">THREADS --threads</span> <span style="opacity: 0.7">[default: 4]</span> <span style="color: #00aa00"> </span> |
| 408 |
<span style="color: #00aa00"> </span> |
| 409 |
<span style="color: #cc3333; font-weight: bold">╔═ Required Options ═══════════════════════════════════════════════════════════╗</span> |
| 410 |
<span style="color: #cc3333; font-weight: bold">║</span> <span style="color: #cc3333; font-weight: bold">*</span> <span style="color: #0088cc">INPUT-FILE --input-file</span> <span style="color: #cc3333; opacity: 0.7">[required]</span> <span style="color: #cc3333; font-weight: bold">║</span> |
| 411 |
<span style="color: #cc3333; font-weight: bold">║</span> <span style="color: #cc3333; font-weight: bold">*</span> <span style="color: #0088cc">OUTPUT-DIR --output-dir</span> <span style="color: #cc3333; opacity: 0.7">[required]</span> <span style="color: #cc3333; font-weight: bold">║</span> |
| 412 |
<span style="color: #cc3333; font-weight: bold">╚══════════════════════════════════════════════════════════════════════════════╝</span></pre> |
| 413 |
</div> |
| 414 |
|
| 415 |
--------------------- |
| 416 |
Custom Column Layout |
| 417 |
--------------------- |
| 418 |
|
| 419 |
For complete control over the help table layout, you can define custom columns |
| 420 |
using :class:`~cyclopts.help.ColumnSpec`: |
| 421 |
|
| 422 |
.. code-block:: python |
| 423 |
|
| 424 |
from cyclopts import App, Group, Parameter |
| 425 |
from cyclopts.help import DefaultFormatter, ColumnSpec, TableSpec |
| 426 |
from typing import Annotated |
| 427 |
|
| 428 |
# Define custom column renderers |
| 429 |
def names_renderer(entry): |
| 430 |
"""Combine parameter names and shorts.""" |
| 431 |
names = " ".join(entry.names) if entry.names else "" |
| 432 |
shorts = " ".join(entry.shorts) if entry.shorts else "" |
| 433 |
return f"{names} {shorts}".strip() |
| 434 |
|
| 435 |
def type_renderer(entry): |
| 436 |
"""Show the parameter type.""" |
| 437 |
from cyclopts.annotations import get_hint_name |
| 438 |
return get_hint_name(entry.type) if entry.type else "" |
| 439 |
|
| 440 |
# Create custom columns |
| 441 |
custom_group = Group( |
| 442 |
"Custom Layout", |
| 443 |
help_formatter=DefaultFormatter( |
| 444 |
table_spec=TableSpec(show_header=True), |
| 445 |
column_specs=( |
| 446 |
ColumnSpec( |
| 447 |
renderer=lambda e: "★" if e.required else " ", |
| 448 |
header="", |
| 449 |
width=2, |
| 450 |
style="yellow bold", |
| 451 |
), |
| 452 |
ColumnSpec( |
| 453 |
renderer=names_renderer, |
| 454 |
header="Option", |
| 455 |
style="cyan", |
| 456 |
max_width=30, |
| 457 |
), |
| 458 |
ColumnSpec( |
| 459 |
renderer=type_renderer, |
| 460 |
header="Type", |
| 461 |
style="magenta", |
| 462 |
justify="center", |
| 463 |
), |
| 464 |
ColumnSpec( |
| 465 |
renderer="description", # Use attribute name |
| 466 |
header="Description", |
| 467 |
overflow="fold", |
| 468 |
), |
| 469 |
) |
| 470 |
) |
| 471 |
) |
| 472 |
|
| 473 |
app = App() |
| 474 |
|
| 475 |
@app.default |
| 476 |
def main( |
| 477 |
input_path: Annotated[str, Parameter(group=custom_group, help="Input file path")], |
| 478 |
output_path: Annotated[str, Parameter(group=custom_group, help="Output file path")], |
| 479 |
count: Annotated[int, Parameter(group=custom_group, help="Number of iterations")] = 1, |
| 480 |
): |
| 481 |
"""Demo custom column layout.""" |
| 482 |
print(f"Processing {input_path} -> {output_path} ({count} times)") |
| 483 |
|
| 484 |
if __name__ == "__main__": |
| 485 |
app() |
| 486 |
|
| 487 |
Output: |
| 488 |
|
| 489 |
.. raw:: html |
| 490 |
|
| 491 |
<div class="highlight-default notranslate"> |
| 492 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: test_custom_column.py [ARGS] [OPTIONS]</span> |
| 493 |
|
| 494 |
Demo custom column layout. |
| 495 |
|
| 496 |
╭─ Commands ───────────────────────────────────────────────────────────────────╮ |
| 497 |
│ <span style="color: #0088cc">--help -h</span> Display this message and exit. │ |
| 498 |
│ <span style="color: #0088cc">--version</span> Display application version. │ |
| 499 |
╰──────────────────────────────────────────────────────────────────────────────╯ |
| 500 |
╭─ Custom Layout ──────────────────────────────────────────────────────────────╮ |
| 501 |
│ Option Type Description │ |
| 502 |
│ <span style="color: #ffaa00; font-weight: bold">★</span> <span style="color: #0088cc">INPUT-PATH --input-path</span> <span style="color: #aa00aa">str</span> Input file path │ |
| 503 |
│ <span style="color: #ffaa00; font-weight: bold">★</span> <span style="color: #0088cc">OUTPUT-PATH --output-path</span> <span style="color: #aa00aa">str</span> Output file path │ |
| 504 |
│ <span style="color: #0088cc">COUNT --count</span> <span style="color: #aa00aa">int</span> Number of iterations │ |
| 505 |
╰──────────────────────────────────────────────────────────────────────────────╯</pre> |
| 506 |
</div> |
| 507 |
|
| 508 |
Dynamic Column Builders |
| 509 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 510 |
|
| 511 |
For even more flexibility, you can create columns dynamically based on runtime |
| 512 |
conditions: |
| 513 |
|
| 514 |
.. code-block:: python |
| 515 |
|
| 516 |
from cyclopts import App, Parameter |
| 517 |
from cyclopts.help import DefaultFormatter, ColumnSpec |
| 518 |
from typing import Annotated |
| 519 |
|
| 520 |
def dynamic_columns(console, options, entries): |
| 521 |
"""Build columns based on console width and entries.""" |
| 522 |
columns = [] |
| 523 |
|
| 524 |
# Only show required indicator if there are required params |
| 525 |
if any(e.required for e in entries): |
| 526 |
columns.append(ColumnSpec( |
| 527 |
renderer=lambda e: "*" if e.required else "", |
| 528 |
width=2, |
| 529 |
style="red", |
| 530 |
)) |
| 531 |
|
| 532 |
# Adjust name column width based on console size |
| 533 |
max_width = min(40, int(console.width * 0.3)) |
| 534 |
columns.append(ColumnSpec( |
| 535 |
renderer=lambda e: " ".join(e.names + e.shorts), |
| 536 |
header="Option", |
| 537 |
max_width=max_width, |
| 538 |
style="cyan", |
| 539 |
)) |
| 540 |
|
| 541 |
# Always include description |
| 542 |
columns.append(ColumnSpec( |
| 543 |
renderer="description", |
| 544 |
header="Description", |
| 545 |
overflow="fold", |
| 546 |
)) |
| 547 |
|
| 548 |
return tuple(columns) |
| 549 |
|
| 550 |
app = App( |
| 551 |
help_formatter=DefaultFormatter( |
| 552 |
column_specs=dynamic_columns |
| 553 |
) |
| 554 |
) |
| 555 |
|
| 556 |
@app.default |
| 557 |
def main( |
| 558 |
input_file: str, |
| 559 |
output_file: str, |
| 560 |
verbose: bool = False, |
| 561 |
): |
| 562 |
"""Process files with dynamic columns.""" |
| 563 |
print(f"Processing {input_file} -> {output_file}") |
| 564 |
|
| 565 |
if __name__ == "__main__": |
| 566 |
app() |
| 567 |
|
| 568 |
Output (adjusts based on terminal width): |
| 569 |
|
| 570 |
.. raw:: html |
| 571 |
|
| 572 |
<div class="highlight-default notranslate"> |
| 573 |
<pre style="font-family: monospace;"><span style="font-weight: bold">Usage: test_dynamic_columns.py [ARGS] [OPTIONS]</span> |
| 574 |
|
| 575 |
Process files with dynamic columns. |
| 576 |
|
| 577 |
╭─ Commands ───────────────────────────────────────────────────────────────────╮ |
| 578 |
│ Option Description │ |
| 579 |
│ <span style="color: #0088cc">--help -h</span> Display this message and exit. │ |
| 580 |
│ <span style="color: #0088cc">--version</span> Display application version. │ |
| 581 |
╰──────────────────────────────────────────────────────────────────────────────╯ |
| 582 |
╭─ Parameters ─────────────────────────────────────────────────────────────────╮ |
| 583 |
│ Option Description │ |
| 584 |
│ <span style="color: #cc3333">*</span> <span style="color: #0088cc">INPUT-FILE --input-file</span> │ |
| 585 |
│ <span style="color: #cc3333">*</span> <span style="color: #0088cc">OUTPUT-FILE</span> │ |
| 586 |
│ <span style="color: #0088cc">--output-file</span> │ |
| 587 |
│ <span style="color: #0088cc">VERBOSE --verbose</span> │ |
| 588 |
│ <span style="color: #0088cc">--no-verbose</span> │ |
| 589 |
╰──────────────────────────────────────────────────────────────────────────────╯</pre> |
| 590 |
</div> |
| 591 |
|
| 592 |
-------------------------- |
| 593 |
Creating Custom Formatters |
| 594 |
-------------------------- |
| 595 |
|
| 596 |
For complete control, you can implement your own formatter by following the |
| 597 |
:class:`~cyclopts.help.protocols.HelpFormatter` protocol. The formatter methods |
| 598 |
receive the console and options first, followed by the content to render: |
| 599 |
|
| 600 |
.. code-block:: python |
| 601 |
|
| 602 |
from cyclopts import App |
| 603 |
from cyclopts.help import HelpPanel |
| 604 |
from rich.console import Console, ConsoleOptions |
| 605 |
from rich.table import Table |
| 606 |
from rich.panel import Panel |
| 607 |
|
| 608 |
class MyCustomFormatter: |
| 609 |
"""A custom formatter with unique styling.""" |
| 610 |
|
| 611 |
def __call__(self, console: Console, options: ConsoleOptions, panel: HelpPanel) -> None: |
| 612 |
"""Render a help panel with custom styling.""" |
| 613 |
if not panel.entries: |
| 614 |
return |
| 615 |
|
| 616 |
# Create a custom table |
| 617 |
table = Table(show_header=True, header_style="bold magenta") |
| 618 |
table.add_column("Option", style="cyan", no_wrap=True) |
| 619 |
table.add_column("Description", style="white") |
| 620 |
|
| 621 |
for entry in panel.entries: |
| 622 |
name = " ".join(entry.names + entry.shorts) |
| 623 |
# Extract plain text from description (handles InlineText, etc) |
| 624 |
desc = "" |
| 625 |
if entry.description: |
| 626 |
if hasattr(entry.description, 'plain'): |
| 627 |
desc = entry.description.plain |
| 628 |
elif hasattr(entry.description, '__rich_console__'): |
| 629 |
# Render to plain text without styles |
| 630 |
with console.capture() as capture: |
| 631 |
console.print(entry.description, end="") |
| 632 |
desc = capture.get() |
| 633 |
else: |
| 634 |
desc = str(entry.description) |
| 635 |
table.add_row(name, desc) |
| 636 |
|
| 637 |
# Wrap in a custom panel |
| 638 |
panel_title = panel.title or "Options" |
| 639 |
styled_panel = Panel( |
| 640 |
table, |
| 641 |
title=f"[bold blue]{panel_title}[/bold blue]", |
| 642 |
border_style="blue", |
| 643 |
) |
| 644 |
|
| 645 |
console.print(styled_panel) |
| 646 |
|
| 647 |
def render_usage(self, console: Console, options: ConsoleOptions, usage) -> None: |
| 648 |
"""Render the usage line.""" |
| 649 |
if usage: |
| 650 |
console.print(f"[bold green]Usage:[/bold green] {usage}") |
| 651 |
|
| 652 |
def render_description(self, console: Console, options: ConsoleOptions, description) -> None: |
| 653 |
"""Render the description.""" |
| 654 |
if description: |
| 655 |
console.print(f"\n[italic]{description}[/italic]\n") |
| 656 |
|
| 657 |
# Use the custom formatter |
| 658 |
app = App(help_formatter=MyCustomFormatter()) |
| 659 |
|
| 660 |
@app.default |
| 661 |
def main(input_file: str, output_file: str, verbose: bool = False): |
| 662 |
"""Process files with custom formatter.""" |
| 663 |
print(f"Processing {input_file} -> {output_file}") |
| 664 |
|
| 665 |
if __name__ == "__main__": |
| 666 |
app() |
| 667 |
|
| 668 |
Output: |
| 669 |
|
| 670 |
.. raw:: html |
| 671 |
|
| 672 |
<div class="highlight-default notranslate"> |
| 673 |
<pre style="font-family: monospace;"><span style="color: #00aa00; font-weight: bold">Usage:</span> test_custom_formatter.py [ARGS] [OPTIONS] |
| 674 |
|
| 675 |
<span style="font-style: italic">Process files with custom formatter.</span> |
| 676 |
|
| 677 |
<span style="color: #0088cc">╭─ </span><span style="color: #0088cc; font-weight: bold">Commands</span><span style="color: #0088cc"> ───────────────────────────────────────────────────────────────────╮</span> |
| 678 |
<span style="color: #0088cc">│</span> ┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ <span style="color: #0088cc">│</span> |
| 679 |
<span style="color: #0088cc">│</span> ┃<span style="color: #aa00aa; font-weight: bold"> Option </span>┃<span style="color: #aa00aa; font-weight: bold"> Description </span>┃ <span style="color: #0088cc">│</span> |
| 680 |
<span style="color: #0088cc">│</span> ┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ <span style="color: #0088cc">│</span> |
| 681 |
<span style="color: #0088cc">│</span> │<span style="color: #00aaaa"> --help -h </span>│ Display this message and exit. │ <span style="color: #0088cc">│</span> |
| 682 |
<span style="color: #0088cc">│</span> │<span style="color: #00aaaa"> --version </span>│ Display application version. │ <span style="color: #0088cc">│</span> |
| 683 |
<span style="color: #0088cc">│</span> └───────────┴────────────────────────────────┘ <span style="color: #0088cc">│</span> |
| 684 |
<span style="color: #0088cc">╰──────────────────────────────────────────────────────────────────────────────╯</span> |
| 685 |
<span style="color: #0088cc">╭─ </span><span style="color: #0088cc; font-weight: bold">Parameters</span><span style="color: #0088cc"> ─────────────────────────────────────────────────────────────────╮</span> |
| 686 |
<span style="color: #0088cc">│</span> ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓ <span style="color: #0088cc">│</span> |
| 687 |
<span style="color: #0088cc">│</span> ┃<span style="color: #aa00aa; font-weight: bold"> Option </span>┃<span style="color: #aa00aa; font-weight: bold"> Description </span>┃ <span style="color: #0088cc">│</span> |
| 688 |
<span style="color: #0088cc">│</span> ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩ <span style="color: #0088cc">│</span> |
| 689 |
<span style="color: #0088cc">│</span> │<span style="color: #00aaaa"> INPUT-FILE --input-file </span>│ │ <span style="color: #0088cc">│</span> |
| 690 |
<span style="color: #0088cc">│</span> │<span style="color: #00aaaa"> OUTPUT-FILE --output-file </span>│ │ <span style="color: #0088cc">│</span> |
| 691 |
<span style="color: #0088cc">│</span> │<span style="color: #00aaaa"> VERBOSE --verbose --no-verbose </span>│ │ <span style="color: #0088cc">│</span> |
| 692 |
<span style="color: #0088cc">│</span> └────────────────────────────────┴─────────────┘ <span style="color: #0088cc">│</span> |
| 693 |
<span style="color: #0088cc">╰──────────────────────────────────────────────────────────────────────────────╯</span></pre> |
| 694 |
</div> |
| 695 |
|
| 696 |
--------- |
| 697 |
Reference |
| 698 |
--------- |
| 699 |
|
| 700 |
For complete API documentation of help formatting components, see: |
| 701 |
|
| 702 |
* :class:`cyclopts.help.DefaultFormatter` - Rich-based formatter with full customization |
| 703 |
* :class:`cyclopts.help.PlainFormatter` - Plain text formatter for accessibility |
| 704 |
* :class:`cyclopts.help.PanelSpec` - Panel appearance specification |
| 705 |
* :class:`cyclopts.help.TableSpec` - Table styling specification |
| 706 |
* :class:`cyclopts.help.ColumnSpec` - Column definition and rendering |
| 707 |
* :class:`cyclopts.help.protocols.HelpFormatter` - Protocol for custom formatters |
| 708 |
|
| 709 |
See also: |
| 710 |
|
| 711 |
* :ref:`Help` - General help system documentation |
| 712 |
* :ref:`Groups` - Organizing parameters into groups |