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

0 comments on commit 7cc35e4

Please sign in to comment.