1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
==========
Parameters
==========
Typically, Cyclopts gets all the information it needs from object names, type hints, and the function docstring:
.. code-block:: python
from cyclopts import App
app = App(help="This is help for the root application.")
@app.command
def foo(value: int): # Cyclopts uses the ``value`` name and ``int`` type hint
"""Cyclopts uses this short description for help.
Parameters
----------
value: int
Cyclopts uses this description for ``value``'s help.
"""
app()
Running the example:
.. code-block:: console
$ my-script --help
Usage: my-script COMMAND
This is help for the root application.
╭─ Commands ──────────────────────────────────────────────────────────╮
│ foo Cyclopts uses this short description for help. │
│ --help,-h Display this message and exit. │
│ --version Display application version. │
╰─────────────────────────────────────────────────────────────────────╯
$ my-script foo --help
Usage: my-script [ARGS] [OPTIONS]
Cyclopts uses this short description for help.
╭─ Parameters ─────────────────────────────────────────────────────────────────────────╮
│ * VALUE --value Cyclopts uses this description for value's help. [required] │
╰──────────────────────────────────────────────────────────────────────────────────────╯
This keeps the code as clean and terse as possible.
However, if more control is required, we can provide additional information by `annotating <https://docs.python.org/3/library/typing.html#typing.Annotated>`_ type hints with :class:`.Parameter`.
.. code-block:: python
from cyclopts import App, Parameter
from typing import Annotated
app = App()
@app.command
def foo(bar: Annotated[int, Parameter(...)]):
pass
app()
:class:`.Parameter` gives complete control on how Cyclopts processes the annotated parameter.
See the :ref:`API` page for all configurable options.
This page will investigate some of the more common use-cases.
.. note::
:class:`.Parameter` can also be used as a decorator.
This is :ref:`particularly useful for class definitions <Namespace Flattening>`.
------
Naming
------
Like :ref:`command names <Command Changing Name>`, CLI parameter names are derived from their python counterparts.
However, sometimes customization is needed.
.. _Parameters - Naming - Manual Naming:
^^^^^^^^^^^^^
Manual Naming
^^^^^^^^^^^^^
Parameter names (and their short forms) can be manually specified:
.. code-block:: python
from cyclopts import App, Parameter
from typing import Annotated
app = App()
@app.default
def main(
*,
foo: Annotated[str, Parameter(name=["--foo", "-f"])], # Adding a short-form
bar: Annotated[str, Parameter(name="--something-else")],
):
pass
app()
.. code-block:: console
$ my-script --help
Usage: main COMMAND [OPTIONS]
╭─ Commands ──────────────────────────────────────────────╮
│ --help -h Display this message and exit. │
│ --version Display application version. │
╰─────────────────────────────────────────────────────────╯
╭─ Parameters ────────────────────────────────────────────╮
│ * --foo -f [required] │
│ * --something-else [required] │
╰─────────────────────────────────────────────────────────╯
Manually set names via :attr:`Parameter.name <cyclopts.Parameter.name>` are not subject to :attr:`Parameter.name_transform <cyclopts.Parameter.name_transform>`.
^^^^^^^^^^^^^^
Name Transform
^^^^^^^^^^^^^^
The name transform function that converts the python variable name to it's CLI counterpart can be configured by setting :attr:`Parameter.name_transform <cyclopts.Parameter.name_transform>` (defaults to :func:`.default_name_transform`).
.. code-block:: python
from cyclopts import App, Parameter
from typing import Annotated
app = App()
def name_transform(s: str) -> str:
return s.upper()
@app.default
def main(
*,
foo: Annotated[str, Parameter(name_transform=name_transform)],
bar: Annotated[str, Parameter(name_transform=name_transform)],
):
pass
app()
.. code-block:: console
$ my-script --help
Usage: main COMMAND [OPTIONS]
╭─ Commands ──────────────────────────────────────────────╮
│ --help -h Display this message and exit. │
│ --version Display application version. │
╰─────────────────────────────────────────────────────────╯
╭─ Parameters ────────────────────────────────────────────╮
│ * --FOO [required] │
│ * --BAR [required] │
╰─────────────────────────────────────────────────────────╯
Notice how the parameter is now ``--FOO`` instead of the standard ``--foo``.
.. note::
The returned string is **before** the standard ``--`` is prepended.
Generally, it is not very useful to set the name transform on **individual** parameters; it would be easier/clearer :ref:`to manually specify the name <Parameters - Naming - Manual Naming>`.
However, we can change the default name transform for the **entire app** by configuring the app's :ref:`default_parameter <Default Parameter>`.
To change the :attr:`~cyclopts.Parameter.name_transform` across your entire app, add the following to your :class:`~cyclopts.App` configuration:
.. code-block:: python
app = App(
default_parameter=Parameter(name_transform=my_custom_name_transform),
)
----
Help
----
It is recommended to use docstrings for your parameter help, but if necessary, you can explicitly set a help string:
.. code-block:: python
@app.command
def foo(value: Annotated[int, Parameter(help="THIS IS USED.")]):
"""
Parameters
----------
value: int
This description is not used; got overridden.
"""
.. code-block:: console
$ my-script foo --help
╭─ Parameters ──────────────────────────────────────────────────╮
│ * VALUE,--value THIS IS USED. [required] │
╰───────────────────────────────────────────────────────────────╯
.. _Converters:
----------
Converters
----------
Cyclopts has a powerful coercion engine that automatically converts CLI string tokens to the types hinted in a function signature.
However, sometimes a custom :attr:`~.Parameter.converter` is required.
Lets consider a case where we want the user to specify a file size, and we want to allows suffixes like `"MB"`.
.. code-block:: python
from cyclopts import App, Parameter, Token
from typing import Annotated, Sequence
from pathlib import Path
app = App()
mapping = {
"kb": 1024,
"mb": 1024 * 1024,
"gb": 1024 * 1024 * 1024,
}
def byte_units(type_, tokens: Sequence[Token]) -> int:
# type_ is ``int``,
value = tokens[0].value.lower()
try:
return type_(value) # If this works, it didn't have a suffix.
except ValueError:
pass
number, suffix = value[:-2], value[-2:]
return int(number) * mapping[suffix]
@app.command
def zero(file: Path, size: Annotated[int, Parameter(converter=byte_units)]):
"""Creates a file of all-zeros."""
print(f"Writing {size} zeros to {file}.")
file.write_bytes(bytes(size))
app()
.. code-block:: console
$ my-script zero out.bin 100
Writing 100 zeros to out.bin.
$ my-script zero out.bin 1kb
Writing 1024 zeros to out.bin.
$ my-script zero out.bin 3mb
Writing 3145728 zeros to out.bin.
The converter function gets the annotated type, and the :class:`.Token` s parsed for this argument.
Tokens are Cyclopt's way of bookkeeping user inputs; in the last command the ``tokens`` object would look like:
.. code-block:: python
# tokens is a length-1 tuple. The variable "size" only takes in 1 token:
tuple(
Token(
keyword=None, # "3mb" was provided positionally, not by keyword
value='3mb', # The string from the command line
source='cli', # The value came from the command line, as opposed to other Cyclopts mechanisms.
index=0, # For the variable "size", this is the first (0th) token.
),
)
----------------
Validating Input
----------------
Just because data is of the correct type, doesn't mean it's valid.
If we had a program that accepts integer user age as an input, ``-1`` is an integer, but not a valid age.
.. code-block:: python
from cyclopts import App, Parameter
from typing import Annotated
app = App()
def validate_age(type_, value):
if value < 0:
raise ValueError("Negative ages not allowed.")
if value > 150:
raise ValueError("You are too old to be using this application.")
@app.default
def allowed_to_buy_alcohol(age: Annotated[int, Parameter(validator=validate_age)]):
print("Under 21: prohibited." if age < 21 else "Good to go!")
app()
.. code-block:: console
$ my-script 30
Good to go!
$ my-script 10
Under 21: prohibited.
$ my-script -1
╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ Invalid value "-1" for "AGE". Negative ages not allowed. │
╰──────────────────────────────────────────────────────────────────────────────╯
$ my-script 200
╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ Invalid value "200" for "AGE". You are too old to be using this application. │
╰──────────────────────────────────────────────────────────────────────────────╯
Certain builtin error types (:exc:`ValueError`, :exc:`TypeError`, :exc:`AssertionError`) will be re-interpreted by Cyclopts and formatted into a prettier message for the application user.
Cyclopts has some :ref:`builtin validators <Parameter Validators>` for common situations
We can create a similar app as above:
.. code-block:: python
from cyclopts import App, Parameter, validators
from typing import Annotated
app = App()
@app.default
def allowed_to_buy_alcohol(age: Annotated[int, Parameter(validator=validators.Number(gte=0, lte=150))]):
# gte - greater than or equal to
# lte - less than or equal to
print("Under 21: prohibited." if age < 21 else "Good to go!")
app()
Taking this one step further, Cyclopts has some :ref:`builtin convenience types <Annotated Types>`. If we didn't care about the upper age bound, we could simplify the application to:
.. code-block:: python
from cyclopts import App
from cyclopts.types import NonNegativeInt
app = App()
@app.default
def allowed_to_buy_alcohol(age: NonNegativeInt):
print("Under 21: prohibited." if age < 21 else "Good to go!")
app()
--------------------
Parameter Resolution
--------------------
Cyclopts can combine multiple :class:`.Parameter` annotations together.
Say you want to define a new :obj:`int` type that uses the :ref:`byte-centric converter from above<Converters>`.
We can define the type:
.. code-block:: python
ByteSize = Annotated[int, Parameter(converter=byte_units)]
We can then either directly annotate a function parameter with this:
.. code-block:: python
@app.command
def zero(size: ByteSize):
pass
or even stack annotations to add additional features, like a validator:
.. code-block:: python
def must_be_multiple_of_4096(type_, value):
assert value % 4096 == 0, "Size must be a multiple of 4096"
@app.command
def zero(size: Annotated[ByteSize, Parameter(validator=must_be_multiple_of_4096)]):
pass
Python automatically flattens out annotations, so this is interpreted as:
.. code-block:: python
Annotated[ByteSize, Parameter(converter=byte_units), Parameter(validator=must_be_multiple_of_4096)]
Cyclopts will search **right-to-left** for **set** parameter attributes until one is found. I.e. right-most parameter attributes have the highest priority.
.. code-block:: console
$ my-script 1234
╭─ Error ──────────────────────────────────────────────────────────────────────╮
│ Invalid value "1234" for "SIZE". Size must be a multiple of 4096 │
╰──────────────────────────────────────────────────────────────────────────────╯
See :ref:`Parameter Resolution Order<Parameter Resolution Order>` for more details.
|