Skip to content

Commit

Permalink
feat(cli): add autocompletions
Browse files Browse the repository at this point in the history
  • Loading branch information
xastap00 committed Aug 6, 2024
1 parent 14e5d93 commit 7cc35e4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
38 changes: 38 additions & 0 deletions docs/en/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,41 @@ If updating directly is unavoidable, make sure you update to a compatible versio
::

$ pip install esptool==3.3.2

Shell completions
-----------------

To activate autocompletion, you can manually add commands provided below to your shell's config file
or run them in your current terminal session for one-time activation.
You will likely have to restart or re-login for the autocompletion to start working.

bash:
::

eval "$(register-python-argcomplete esptool.py espsecure.py espefuse.py)"

zsh:

To activate completions in zsh, first make sure `compinit` is marked for
autoload and run autoload:

.. code-block:: bash
autoload -U compinit
compinit
Afterwards you can enable completions for esptool.py, espsecure.py and espefuse.py:

::

eval "$(register-python-argcomplete esptool.py espsecure.py espefuse.py)"

fish:

Not required to be in the config file, only run once

::

register-python-argcomplete --shell fish esptool.py espsecure.py espefuse.py >~/.config/fish/completions/esptool.py.fish

Other shells nor OS Windows are not supported.
10 changes: 10 additions & 0 deletions espefuse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: GPL-2.0-or-later
# PYTHON_ARGCOMPLETE_OK

import argparse
import os
Expand Down Expand Up @@ -285,6 +286,15 @@ def main(custom_commandline=None, esp=None):

efuse_operations.add_commands(subparsers, efuses)

# Enable argcomplete only on Unix-like systems
if sys.platform != "win32":
try:
import argcomplete

argcomplete.autocomplete(parser)
except ImportError:
pass

grouped_remaining_args, used_cmds = split_on_groups(remaining_args)
if len(grouped_remaining_args) == 0:
parser.print_help()
Expand Down
11 changes: 10 additions & 1 deletion espsecure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-FileCopyrightText: 2016-2023 Espressif Systems (Shanghai) CO LTD
#
# SPDX-License-Identifier: GPL-2.0-or-later

# PYTHON_ARGCOMPLETE_OK
import argparse
import hashlib
import operator
Expand Down Expand Up @@ -1867,6 +1867,15 @@ def main(custom_commandline=None):
type=argparse.FileType("rb"),
)

# Enable argcomplete only on Unix-like systems
if sys.platform != "win32":
try:
import argcomplete

argcomplete.autocomplete(parser)
except ImportError:
pass

args = parser.parse_args(custom_commandline)
print("espsecure.py v%s" % esptool.__version__)
if args.operation is None:
Expand Down
11 changes: 10 additions & 1 deletion esptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later

# PYTHON_ARGCOMPLETE_OK
__all__ = [
"chip_id",
"detect_chip",
Expand Down Expand Up @@ -690,6 +690,15 @@ def add_spi_flash_subparsers(
for operation in subparsers.choices.keys():
assert operation in globals(), "%s should be a module function" % operation

# Enable argcomplete only on Unix-like systems
if sys.platform != "win32":
try:
import argcomplete

argcomplete.autocomplete(parser)
except ImportError:
pass

argv = expand_file_arguments(argv or sys.argv[1:])

args = parser.parse_args(argv)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"reedsolo>=1.5.3,<1.8",
"PyYAML>=5.1",
"intelhex",
'argcomplete>=3; sys_platform != "win32"',
]

[project.urls]
Expand Down

8 comments on commit 7cc35e4

@Jason2866
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@radimkarnis adding more and more dependencies makes it complicated to install on different targets.
Could you consider to remove the argcomplete feature?

@radimkarnis
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry, we carefully consider each new dependency and only include it if it provides enough value for the users.
We have removed the argcomplete package dependency (although it is now replaced by another new one -rich_click). Hopefully that doesn't make things too difficult for you.

@Jason2866
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, for the information. Knowing this early helps. Can change "stuff" before causing problems

@dobairoland
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jason2866 It could help if you could compile a list of things which makes difficult to maintain your esptool fork. Please ping me by name if you do so. I would try to reflect them in our long term strategy. Thanks!

@Jason2866
Copy link
Contributor

@Jason2866 Jason2866 commented on 7cc35e4 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dobairoland Very kind of you to ask :-) In short it is solved now. But let me explain the background.
esptool.py is used in the Platform fork of espressif32 -> pioarduino to provide the actual espressif32 Arduino core compatible to use with Platformio. esptool.py is not installed (via pypi), it is just copied, very early during install of the espressif32 platform (not my decision, it is Platformio).
So the used pyenv has to met the requirements of esptool.py to work. This requirements are set in platformio-core as the name already suggests the core function of Platformio which is used for all Platforms Platformio supports. Since modifying this core component is not the way i want to do (fork pioarduino would not be compatible anymore...). I added the first new dependency intelhex directly in esptool.py (code parts of intelhex). Later argcomplete was added as further dependency.
At this point i decided not to add the argcomplete in esptool.py as I did for intelhex. Instead i heavily modified the pioarduino espressif32 platform builder. The builder now checks before doing "the main job" the pyenv for the needed pypi dependencies (defined in the espressif32 platform) and adds/changes or even rebuild the used pyenv completely.
So it is now easily possible to add every needed pypi dependencies.
As long you add "just" pypi dependencies, I just need to know.
When overlooked this change, not a big deal anymore, since a clear error message will happen whats missing and needs adding in the pioarduino platforms pypi requirements config

@dobairoland
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jason2866 Thank you for explaining!

esptool.py is not installed (via pypi), it is just copied

It is true that we don't really think about using esptool this way.

Nothing can be installed via pypi? Not even a custom fork of esptool (released on pypi)?

If you copy esptool and run pip install . from the directory then all dependencies should be installed and you don't have to worry about them. Does this approach have a blocker in the Platformio environment?

@Jason2866
Copy link
Contributor

@Jason2866 Jason2866 commented on 7cc35e4 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dobairoland Thx for the feedback, and your thoughts how to solve. Very appreciated!

Nothing can be installed via pypi? Not even a custom fork of esptool (released on pypi)?

No, since it has a fixed system (path) where it is expected to be. Like for all needed tools. Not only Python tools.

If you copy esptool and run pip install . from the directory then all dependencies should be installed and you don't have to worry about them. Does this approach have a blocker in the Platformio environment?

This approach could/should work too. Honestly it is easier to add manually the dependencies which are needed (for esptool.py and others) and let install them automatically.
A change of the used esptool.py version needs maintainer action, so no unexpected surprises.

Looking forward for v4.9 :-)

@dobairoland
Copy link
Collaborator

@dobairoland dobairoland commented on 7cc35e4 Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All clear now. Thanks!

We will do a couple more releases from the release/v4 branch: 4.9, 4.9.1, .... Maybe even 4.10, ...

If you look at the master branch then it is already tuned for the v5.0 release. That is why so many and significant changes are happening there.

Please sign in to comment.