| 1 | """Run Cyclopts applications from Python scripts.""" |
| 2 | |
| 3 | from pathlib import Path |
| 4 | from typing import Annotated |
| 5 | |
| 6 | from cyclopts.cli import app |
| 7 | from cyclopts.loader import load_app_from_script |
| 8 | from cyclopts.parameter import Parameter |
| 9 | |
| 10 | |
| 11 | @app.command(help_flags="") |
| 12 | def run( |
| 13 | script: Annotated[ |
| 14 | Path, |
| 15 | Parameter(allow_leading_hyphen=True), |
| 16 | ], |
| 17 | /, |
| 18 | *args: Annotated[str, Parameter(allow_leading_hyphen=True)], |
| 19 | ): |
| 20 | """Run a Cyclopts application from a Python script with dynamic shell completion. |
| 21 | |
| 22 | All arguments after the script path are passed to the loaded application. |
| 23 | |
| 24 | Shell completion is available. Run once to install (persistent): |
| 25 | ``cyclopts --install-completion`` |
| 26 | |
| 27 | Parameters |
| 28 | ---------- |
| 29 | script : str |
| 30 | Python script path with optional ':app_object' notation. |
| 31 | args : str |
| 32 | Arguments to pass to the loaded application. |
| 33 | |
| 34 | Examples |
| 35 | -------- |
| 36 | Run a script: |
| 37 | cyclopts run myapp.py --verbose foo bar |
| 38 | |
| 39 | Specify app object: |
| 40 | cyclopts run myapp.py:app --help |
| 41 | """ |
| 42 | if str(script) in app.help_flags: |
| 43 | app.help_print() |
| 44 | return |
| 45 | app_obj, _ = load_app_from_script(script) |
| 46 | return app_obj(args) |