| 1 |
.. _Getting Started: |
| 2 |
|
| 3 |
=============== |
| 4 |
Getting Started |
| 5 |
=============== |
| 6 |
|
| 7 |
Cyclopts relies heavily on function parameter type hints. |
| 8 |
If you are new to type hints or need a refresher, `checkout the mypy cheatsheet`_. |
| 9 |
|
| 10 |
---------------------------- |
| 11 |
A Basic Cyclopts Application |
| 12 |
---------------------------- |
| 13 |
|
| 14 |
The most basic Cyclopts application is as follows: |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
from cyclopts import App |
| 19 |
|
| 20 |
app = App() |
| 21 |
|
| 22 |
@app.default |
| 23 |
def main(): |
| 24 |
print("Hello World!") |
| 25 |
|
| 26 |
if __name__ == "__main__": |
| 27 |
app() |
| 28 |
|
| 29 |
Save this as ``main.py`` and execute it to see: |
| 30 |
|
| 31 |
.. code-block:: console |
| 32 |
|
| 33 |
$ python main.py |
| 34 |
Hello World! |
| 35 |
|
| 36 |
|
| 37 |
The :class:`.App` class offers various configuration options that we'll explore in more detail later. |
| 38 |
The ``app`` object has a decorator method, :meth:`default <cyclopts.App.default>`, which registers a function as the **default action**. |
| 39 |
In this example, the ``main`` function is our default action, and is executed when no CLI command is provided. |
| 40 |
|
| 41 |
------------------ |
| 42 |
Function Arguments |
| 43 |
------------------ |
| 44 |
Let's add some arguments to make this program a little more interesting. |
| 45 |
|
| 46 |
.. code-block:: python |
| 47 |
|
| 48 |
from cyclopts import App |
| 49 |
|
| 50 |
app = App() |
| 51 |
|
| 52 |
@app.default |
| 53 |
def main(name): |
| 54 |
print(f"Hello {name}!") |
| 55 |
|
| 56 |
if __name__ == "__main__": |
| 57 |
app() |
| 58 |
|
| 59 |
Executing the script with the argument ``Alice`` produces the following: |
| 60 |
|
| 61 |
.. code-block:: console |
| 62 |
|
| 63 |
$ python main.py Alice |
| 64 |
Hello Alice! |
| 65 |
|
| 66 |
Code explanation: |
| 67 |
|
| 68 |
1. The function ``main()`` was registered to ``app`` as the **default** action. |
| 69 |
|
| 70 |
2. Calling ``app()`` at the bottom triggers the app to begin parsing CLI inputs. |
| 71 |
|
| 72 |
3. Cyclopts identifies ``"Alice"`` as a positional argument and matches it to the parameter ``name``. |
| 73 |
In the absence of an explicit type hint, Cyclopts defaults to parsing the value as a ``str``. |
| 74 |
|
| 75 |
.. note:: |
| 76 |
Without a type annotation, Cyclopts will actually first attempt to use the **type** of |
| 77 |
the parameter's **default value**. If the parameter doesn't have a default value, it will |
| 78 |
then fallback to ``str``. See :ref:`Coercion Rules`. |
| 79 |
|
| 80 |
|
| 81 |
4. Cyclopts calls the registered **default** function ``main("Alice")``, and the greeting is printed. |
| 82 |
|
| 83 |
|
| 84 |
------------------ |
| 85 |
Multiple Arguments |
| 86 |
------------------ |
| 87 |
Extending the example, lets add more arguments and type hints: |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
from cyclopts import App |
| 92 |
|
| 93 |
app = App() |
| 94 |
|
| 95 |
@app.default |
| 96 |
def main(name: str, count: int, formal: bool = False): |
| 97 |
for _ in range(count): |
| 98 |
if formal: |
| 99 |
print(f"Hello {name}!") |
| 100 |
else: |
| 101 |
print(f"Hey {name}!") |
| 102 |
|
| 103 |
if __name__ == "__main__": |
| 104 |
app() |
| 105 |
|
| 106 |
.. code-block:: console |
| 107 |
|
| 108 |
$ python main.py Alice 3 |
| 109 |
Hey Alice! |
| 110 |
Hey Alice! |
| 111 |
Hey Alice! |
| 112 |
|
| 113 |
$ python main.py Alice 3 --formal |
| 114 |
Hello Alice! |
| 115 |
Hello Alice! |
| 116 |
Hello Alice! |
| 117 |
|
| 118 |
|
| 119 |
The command line input ``"3"`` is converted to an integer because the parameter ``count`` has the type hint :obj:`int`. |
| 120 |
Boolean parameters (e.g., ``--formal`` in this example) are interpreted as flags. |
| 121 |
Cyclopts natively handles all python builtin types (:ref:`and more! <Coercion Rules>`). |
| 122 |
Cyclopts adheres to Python's argument binding rules, allowing for both positional and keyword arguments. |
| 123 |
All of the following CLI invocations are equivalent: |
| 124 |
|
| 125 |
.. code-block:: console |
| 126 |
|
| 127 |
$ python main.py Alice 3 # Supplying arguments positionally. |
| 128 |
$ python main.py --name Alice --count 3 # Supplying arguments via keywords. |
| 129 |
$ python main.py --name=Alice --count=3 # Using = for matching keywords to values is allowed. |
| 130 |
$ python main.py --count 3 --name=Alice # Keyword order does not matter. |
| 131 |
$ python main.py Alice --count 3 # Positional followed by keyword |
| 132 |
$ python main.py --count 3 Alice # Keywords can come before positional if the keyword is later in the function signature. |
| 133 |
$ python main.py --count 3 -- Alice # Using the POSIX convention to indicate the end of keywords |
| 134 |
|
| 135 |
Like calling functions in python, positional arguments cannot be specified after a **prior** argument in the function signature was specified via keyword. |
| 136 |
For example, you cannot supply the count value ``"3"`` positionally while the value for ``name`` is specified via keyword: |
| 137 |
|
| 138 |
.. code-block:: console |
| 139 |
|
| 140 |
# The following are NOT allowed. |
| 141 |
$ python main.py --name=Alice 3 # invalid python: main(name="Alice", 3) |
| 142 |
$ python main.py 3 --name=Alice # invalid python: main(3, name="Alice") |
| 143 |
|
| 144 |
------------------ |
| 145 |
Adding a Help Page |
| 146 |
------------------ |
| 147 |
All CLI apps need to have a help page explaining how to use the application. |
| 148 |
By default, Cyclopts adds the ``--help`` (and the shortform ``-h``) commands to your CLI. |
| 149 |
We can add application-level help documentation when creating our ``app``: |
| 150 |
|
| 151 |
.. code-block:: python |
| 152 |
|
| 153 |
from cyclopts import App |
| 154 |
|
| 155 |
app = App(help="Help string for this demo application.") |
| 156 |
|
| 157 |
@app.default |
| 158 |
def main(name: str, count: int): |
| 159 |
for _ in range(count): |
| 160 |
print(f"Hello {name}!") |
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
app() |
| 164 |
|
| 165 |
.. code-block:: console |
| 166 |
|
| 167 |
$ python main.py --help |
| 168 |
Usage: main COMMAND [ARGS] [OPTIONS] |
| 169 |
|
| 170 |
Help string for this demo application. |
| 171 |
|
| 172 |
╭─ Commands ──────────────────────────────────────────────────────────╮ |
| 173 |
│ --help -h Display this message and exit. │ |
| 174 |
│ --version Display application version. │ |
| 175 |
╰─────────────────────────────────────────────────────────────────────╯ |
| 176 |
╭─ Parameters ────────────────────────────────────────────────────────╮ |
| 177 |
│ * NAME --name [required] │ |
| 178 |
│ * COUNT --count [required] │ |
| 179 |
╰─────────────────────────────────────────────────────────────────────╯ |
| 180 |
|
| 181 |
.. note:: |
| 182 |
Help flags can be changed with :attr:`~cyclopts.App.help_flags`. |
| 183 |
|
| 184 |
Let's add some help documentation for our parameters. |
| 185 |
Cyclopts uses the function's docstring and can interpret ReST, Google, Numpydoc-style and Epydoc docstrings (shoutout to `docstring_parser <https://github.com/rr-/docstring_parser>`_). |
| 186 |
|
| 187 |
.. code-block:: python |
| 188 |
|
| 189 |
from cyclopts import App |
| 190 |
|
| 191 |
app = App() |
| 192 |
|
| 193 |
@app.default |
| 194 |
def main(name: str, count: int): |
| 195 |
"""Help string for this demo application. |
| 196 |
|
| 197 |
Parameters |
| 198 |
---------- |
| 199 |
name: str |
| 200 |
Name of the user to be greeted. |
| 201 |
count: int |
| 202 |
Number of times to greet. |
| 203 |
""" |
| 204 |
for _ in range(count): |
| 205 |
print(f"Hello {name}!") |
| 206 |
|
| 207 |
if __name__ == "__main__": |
| 208 |
app() |
| 209 |
|
| 210 |
.. code-block:: console |
| 211 |
|
| 212 |
$ python main.py --help |
| 213 |
Usage: main COMMAND [ARGS] [OPTIONS] |
| 214 |
|
| 215 |
Help string for this demo application. |
| 216 |
|
| 217 |
╭─ Commands ──────────────────────────────────────────────────────────╮ |
| 218 |
│ --help -h Display this message and exit. │ |
| 219 |
│ --version Display application version. │ |
| 220 |
╰─────────────────────────────────────────────────────────────────────╯ |
| 221 |
╭─ Parameters ────────────────────────────────────────────────────────╮ |
| 222 |
│ * NAME --name Name of the user to be greeted. [required] │ |
| 223 |
│ * COUNT --count Number of times to greet. [required] │ |
| 224 |
╰─────────────────────────────────────────────────────────────────────╯ |
| 225 |
|
| 226 |
.. note:: |
| 227 |
If :attr:`.App.help` is not explicitly set, Cyclopts will fallback to the first line |
| 228 |
(short description) of the registered ``@app.default`` function's docstring. |
| 229 |
|
| 230 |
--- |
| 231 |
Run |
| 232 |
--- |
| 233 |
An alternative, terser API is available for simple applications with a single command. |
| 234 |
The :func:`.run` function takes in a single callable (usually a function) and runs it |
| 235 |
as a Cyclopts application. |
| 236 |
|
| 237 |
.. code-block:: python |
| 238 |
|
| 239 |
import cyclopts |
| 240 |
|
| 241 |
def main(name: str, count: int): |
| 242 |
for _ in range(count): |
| 243 |
print(f"Hello {name}!") |
| 244 |
|
| 245 |
if __name__ == "__main__": |
| 246 |
cyclopts.run(main) |
| 247 |
|
| 248 |
The :func:`.run` function is intentionally simple. If greater control is required, then use the |
| 249 |
conventional :class:`.App` interface. |
| 250 |
|
| 251 |
.. _checkout the mypy cheatsheet: https://mypy.readthedocs.io/en/latest/cheat_sheet_py3.html |