| 1 |
======= |
| 2 |
Version |
| 3 |
======= |
| 4 |
|
| 5 |
All CLI applications should have the basic ability to check the installed version; i.e.: |
| 6 |
|
| 7 |
.. code-block:: console |
| 8 |
|
| 9 |
$ my-application --version |
| 10 |
7.5.8 |
| 11 |
|
| 12 |
By default, Cyclopts adds a command, :meth:`--version <cyclopts.App.version_print>`:, that does exactly this. |
| 13 |
Cyclopts try's to reasonably figure out your package's version by itself. |
| 14 |
The resolution order for determining the version string is as follows: |
| 15 |
|
| 16 |
1. An explicitly supplied version string or callable to the root Cyclopts application: |
| 17 |
|
| 18 |
.. code-block:: python |
| 19 |
|
| 20 |
from cyclopts import App |
| 21 |
|
| 22 |
app = App(version="7.5.8") |
| 23 |
|
| 24 |
app() |
| 25 |
|
| 26 |
If a callable is provided, it will be invoked when running the ``--version`` command: |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
from cyclopts import App |
| 31 |
|
| 32 |
def get_my_application_version() -> str: |
| 33 |
return "7.5.8" |
| 34 |
|
| 35 |
app = App(version=get_my_application_version) |
| 36 |
app() |
| 37 |
|
| 38 |
2. The invoking-package's `Distribution Package's Version Number`_ via `importlib.metadata.version`_. |
| 39 |
Cyclopts attempts to derive the package module that instantiated the :class:`.App` object by traversing the call stack. |
| 40 |
|
| 41 |
3. The invoking-package's `defacto PEP8 standard`_ ``__version__`` string. |
| 42 |
Cyclopts attempts to derive the package module that instantiated the :class:`.App` object by traversing the call stack. |
| 43 |
|
| 44 |
.. code-block:: python |
| 45 |
|
| 46 |
# mypackage/__init__.py |
| 47 |
__version__ = "7.5.8" |
| 48 |
|
| 49 |
# mypackage/__main__.py |
| 50 |
# ``App`` will use ``mypackage.__version__``. |
| 51 |
app = cyclopts.App() |
| 52 |
|
| 53 |
4. The default version string ``"0.0.0"`` will be displayed. |
| 54 |
|
| 55 |
In short, if your CLI application is a properly structured python package, Cyclopts will automatically derive the correct version. |
| 56 |
|
| 57 |
The ``--version`` flag can be changed to a different name(s) via the ``version_flags`` parameter. |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
app = cyclopts.App(version_flags="--show-version") |
| 62 |
app = cyclopts.App(version_flags=["--version", "-v"]) |
| 63 |
|
| 64 |
To disable the ``--version`` flag, set ``version_flags`` to an empty string or iterable. |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
|
| 68 |
app = cyclopts.App(version_flags="") |
| 69 |
app = cyclopts.App(version_flags=[]) |
| 70 |
|
| 71 |
|
| 72 |
.. _Distribution Package's Version Number: https://packaging.python.org/en/latest/glossary/#term-Distribution-Package |
| 73 |
.. _importlib.metadata.version: https://docs.python.org/3.12/library/importlib.metadata.html#distribution-versions |
| 74 |
.. _defacto PEP8 standard: https://peps.python.org/pep-0008/#module-level-dunder-names |