Interactive Shell & Help
Cyclopts has a builtin interactive shell-like feature:
from cyclopts import App
app = App()
@app.command
def foo(p1):
"""Foo Docstring.
Parameters
----------
p1: str
Foo's first parameter.
"""
print(f"foo {p1}")
@app.command
def bar(p1):
"""Bar Docstring.
Parameters
----------
p1: str
Bar's first parameter.
"""
print(f"bar {p1}")
# A blocking call, launching an interactive shell.
app.interactive_shell(prompt="cyclopts> ")
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:
@app.command
def shell():
app.interactive_shell()
if __name__ == "__main__":
app() # Don't call ``app.interactive_shell()`` here.
Special flags like --help and --version work in the shell. Because typing dashes is
awkward at a prompt, the bare words help and version are also accepted at the root (see
remap_flags on App.interactive_shell()), and the root help screen lists them alongside
the other commands. Subcommands use the normal flags (foo --help). A user-defined help
command takes precedence over the remap.
$ python interactive-shell-demo.py
Interactive shell. Press Ctrl-D to exit.
cyclopts> help
Usage: interactive-shell-demo.py COMMAND
╭─ Commands ───────────────────────────────────────────────────╮
│ bar Bar Docstring. │
│ foo Foo Docstring. │
│ help (--help, -h) Display this message and exit. │
│ version (--version) Display application version. │
╰──────────────────────────────────────────────────────────────╯
cyclopts> foo --help
Usage: interactive-shell-demo.py foo P1
Foo Docstring.
╭─ Parameters ─────────────────────────────────────────────────╮
│ * P1 --p1 Foo's first parameter. [required] │
╰──────────────────────────────────────────────────────────────╯
cyclopts> exit
Type q, quit, or exit (or press Ctrl-D) to leave the shell. Pass history=True (or a
path) to persist command history between sessions.
Tab completes command names, option names, static choices (Literal/Enum), paths, and any
Parameter.completer values, wherever the stdlib readline module is available (not Windows).