You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/api-guides/tools/idf-py.rst
+103Lines changed: 103 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -301,7 +301,110 @@ Arguments from a file can be combined with additional command line arguments, an
301
301
302
302
A further example of how this argument file can be used, e.g., creating configuration profile files via @filename, is in the :example_file:`Multiple Build Configurations Example <build_system/cmake/multi_config/README.md>`.
303
303
304
+
Extending ``idf.py``
305
+
====================
306
+
307
+
``idf.py`` can be extended with additional subcommands, global options, and callbacks provided by extension files in your project and components which participate in the build, as well as by external Python packages exposing entry points.
308
+
309
+
- **From components participating in the build**: Place a file named ``idf_ext.py`` in the project root or in a component's root directory that is registered in the project's ``CMakeLists.txt``. Component extensions are discovered after the project is configured - run ``idf.py build`` or ``idf.py reconfigure`` to make newly added commands available.
310
+
- **From Python entry points**: Any installed Python package may contribute extensions by defining an entry point in the ``idf_extension`` group. Package installation is sufficient, no project build is required.
311
+
312
+
.. important::
313
+
314
+
Extensions must not define subcommands or options that have the same names as the core ``idf.py`` commands. Custom actions and options are checked for name collisions, overriding defaults is not possible and a warning is printed. For Python entry points, use unique identifiers as duplicate entry point names will be ignored with a warning.
315
+
316
+
Extension File Example
317
+
----------------------
318
+
319
+
An extension file defines an ``action_extensions`` function which returns additional actions/options. The same structure is used for component-based extensions (``idf_ext.py``) and for package-based extensions (e.g., ``<package_name>_ext.py``):
The ``action_extensions`` function takes arguments ``base_actions`` (all currently registered commands) and ``project_path`` (absolute project directory) and returns a dictionary with up to three keys:
366
+
367
+
- ``global_options``: A list of options available globally for all commands. Each option is a dictionary with fields such as ``names``, ``help``, ``type``, ``is_flag``, ``scope``, etc.
368
+
- ``global_action_callbacks``: A list of functions called once before any task execution. Each global action callback function accepts three arguments:
369
+
370
+
- ``ctx`` — the `click context`_,
371
+
- ``global_args`` — all available global arguments,
372
+
- ``tasks`` — the list of tasks to be executed. Task refer to the action / sub-command used with `idf.py`
373
+
374
+
- ``actions``: A dictionary of new subcommands. Each action has a ``callback`` function and may also include ``options``, ``arguments``, ``dependencies``, etc. Each action callback function accepts three to four arguments:
375
+
376
+
- ``subcommand_name`` — the name of the command (useful if multiple commands share the same callback),
377
+
- ``ctx`` — the `click context`_,
378
+
- ``global_args`` — all available global arguments,
379
+
- ``**action_args`` — (optional) arguments passed to the action
380
+
381
+
Basic Usage Examples
382
+
--------------------
383
+
384
+
1) Provide an extension from a component in your project
385
+
386
+
Create ``idf_ext.py`` in the project root or in a registered component (for example ``components/my_component/idf_ext.py``). Use the extension file example above as your ``idf_ext.py`` implementation.
387
+
388
+
Run ``idf.py build`` or ``idf.py reconfigure`` to load the new command, then ``idf.py --help`` will show the new extension.
389
+
390
+
2) Provide an extension via a Python package entry point
391
+
392
+
Implement your extension in a module named ``<package_name>_ext.py`` using the extension file example above, and expose the ``action_extensions`` function via the ``idf_extension`` entry-point group. For example, with ``pyproject.toml``:
Install the package into the same Python environment as ``idf.py`` (for example with ``pip install -e .`` in the package directory). It is recommended to use a unique module name (e.g., ``<package_name>_ext.py``) to avoid name conflicts. After successful installation, ``idf.py --help`` will show the new extension.
0 commit comments