| 1 |
======================== |
| 2 |
Interactive Shell & Help |
| 3 |
======================== |
| 4 |
Cyclopts has a `builtin interactive shell-like feature <../api.html#cyclopts.App.interactive_shell>`__: |
| 5 |
|
| 6 |
.. code-block:: python |
| 7 |
|
| 8 |
from cyclopts import App |
| 9 |
|
| 10 |
app = App() |
| 11 |
|
| 12 |
@app.command |
| 13 |
def foo(p1): |
| 14 |
"""Foo Docstring. |
| 15 |
|
| 16 |
Parameters |
| 17 |
---------- |
| 18 |
p1: str |
| 19 |
Foo's first parameter. |
| 20 |
""" |
| 21 |
print(f"foo {p1}") |
| 22 |
|
| 23 |
@app.command |
| 24 |
def bar(p1): |
| 25 |
"""Bar Docstring. |
| 26 |
|
| 27 |
Parameters |
| 28 |
---------- |
| 29 |
p1: str |
| 30 |
Bar's first parameter. |
| 31 |
""" |
| 32 |
print(f"bar {p1}") |
| 33 |
|
| 34 |
# A blocking call, launching an interactive shell. |
| 35 |
app.interactive_shell(prompt="cyclopts> ") |
| 36 |
|
| 37 |
|
| 38 |
To make the application still work as-expected from the CLI, it is more appropriate to set a command (or ``@app.default``) to launch the shell: |
| 39 |
|
| 40 |
.. code-block:: python |
| 41 |
|
| 42 |
@app.command |
| 43 |
def shell(): |
| 44 |
app.interactive_shell() |
| 45 |
|
| 46 |
if __name__ == "__main__": |
| 47 |
app() # Don't call ``app.interactive_shell()`` here. |
| 48 |
|
| 49 |
Special flags like ``--help`` and ``--version`` work in the shell, but could be a bit awkward for the root-help: |
| 50 |
|
| 51 |
.. code-block:: console |
| 52 |
|
| 53 |
$ python interactive-shell-demo.py |
| 54 |
Interactive shell. Press Ctrl-D to exit. |
| 55 |
cyclopts> --help |
| 56 |
Usage: interactive-shell-demo.py COMMAND |
| 57 |
|
| 58 |
╭─ Parameters ──────────────────────────────────────────────────╮ |
| 59 |
│ --version Display application version. │ |
| 60 |
│ --help -h Display this message and exit. │ |
| 61 |
╰───────────────────────────────────────────────────────────────╯ |
| 62 |
╭─ Commands ────────────────────────────────────────────────────╮ |
| 63 |
│ bar Bar Docstring. │ |
| 64 |
│ foo Foo Docstring. │ |
| 65 |
╰───────────────────────────────────────────────────────────────╯ |
| 66 |
cyclopts> foo --help |
| 67 |
Usage: interactive-shell-demo.py foo [ARGS] [OPTIONS] |
| 68 |
|
| 69 |
Foo Docstring |
| 70 |
|
| 71 |
╭─ Parameters ──────────────────────────────────────────────────╮ |
| 72 |
│ * P1,--p1 Foo's first parameter. [required] │ |
| 73 |
╰───────────────────────────────────────────────────────────────╯ |
| 74 |
cyclopts> |
| 75 |
|
| 76 |
To resolve this, we can explicitly add a ``help`` command: |
| 77 |
|
| 78 |
.. code-block:: python |
| 79 |
|
| 80 |
@app.command |
| 81 |
def help(): |
| 82 |
"""Display the help screen.""" |
| 83 |
app.help_print() |
| 84 |
|
| 85 |
.. code-block:: console |
| 86 |
|
| 87 |
$ python interactive-shell-demo.py |
| 88 |
Interactive shell. Press Ctrl-D to exit. |
| 89 |
cyclopts> help |
| 90 |
Usage: interactive-shell-demo.py COMMAND |
| 91 |
|
| 92 |
╭─ Parameters ──────────────────────────────────────────────────╮ |
| 93 |
│ --version Display application version. │ |
| 94 |
│ --help -h Display this message and exit. │ |
| 95 |
╰───────────────────────────────────────────────────────────────╯ |
| 96 |
╭─ Commands ────────────────────────────────────────────────────╮ |
| 97 |
│ bar Bar Docstring. │ |
| 98 |
│ foo Foo Docstring. │ |
| 99 |
│ help Display the help screen. │ |
| 100 |
╰───────────────────────────────────────────────────────────────╯ |