File: default_parameter.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (76 lines) | stat: -rw-r--r-- 2,839 bytes parent folder | download | duplicates (2)
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
.. _Default Parameter:

=================
Default Parameter
=================
The default values of :class:`Parameter` for an app can be configured via :attr:`.App.default_parameter`.

For example, to disable the :attr:`~.Parameter.negative` flag feature across your entire app:

.. code-block:: python

   from cyclopts import App, Parameter

   app = App(default_parameter=Parameter(negative=()))

   @app.command
   def foo(*, flag: bool):
       pass

   app()

Consequently, ``--no-flag`` is no longer an allowed flag:

.. code-block::

   $ my-script foo --help
   Usage: my-script foo [ARGS] [OPTIONS]

   ╭─ Parameters ──────────────────────────────────────────────────╮
   │ *  --flag  [required]                                         │
   ╰───────────────────────────────────────────────────────────────╯

Explicitly annotating the parameter with  :attr:`~.Parameter.negative` overrides this configuration and works as expected:


.. code-block:: python

   from cyclopts import App, Parameter
   from typing import Annotated

   app = App(default_parameter=Parameter(negative=()))

   @app.command
   def foo(*, flag: Annotated[bool, Parameter(negative="--anti-flag")]):
       pass

   app()

.. code-block:: console

   $ my-script foo --help
   Usage: my-script foo [ARGS] [OPTIONS]

   ╭─ Parameters ──────────────────────────────────────────────────╮
   │ *  --flag --anti-flag  [required]                             │
   ╰───────────────────────────────────────────────────────────────╯

.. _Parameter Resolution Order:

----------------
Resolution Order
----------------

When resolving what the :class:`.Parameter` values for an individual function parameter should be, explicitly set attributes of higher priority :class:`.Parameter` s override lower priority :class:`.Parameter` s. The resolution order is as follows:

1. **Highest Priority:** Parameter-annotated command function signature ``Annotated[..., Parameter()]``.

2. :attr:`.Group.default_parameter` that the **parameter** belongs to.

3. :attr:`.App.default_parameter` of the **app** that registered the command.

4. :attr:`.Group.default_parameter` of the **app** that the function belongs to.

5. **Lowest Priority:** (2-4) recursively of the parenting app call-chain.

Any of Parameter's fields can be set to `None` to revert back to the true-original Cyclopts default.