Skip to content

Commit

Permalink
Add shell completion via python-shtab
Browse files Browse the repository at this point in the history
Having a shell completion improves developers' workflow and makes it
easier for everyone to notice as the program gains new options.

This commit adds support for generatic static completion files via
python-shtab. Unlike other solutions which repeatedly invoke the
underlying program, to retrieve the next suggestion, shtab parses
argparse options and produces a complete static file.

It currently supports bash, tcsh and bash with PR opened for fish
support. For example, to generate zsh completion use:
 - reuse --print-completion zsh > /usr/share/zsh/site-functions/_reuse

This can be done by the reuse project itself, the package maintainer or
end-user.

For more details, see https://github.com/iterative/shtab

Closes: #629
Signed-off-by: Emil Velikov <[email protected]>
  • Loading branch information
evelikov committed Sep 22, 2024
1 parent dcef820 commit 49adbf3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ jobs:
- name: Install dependencies
run: |
pip install poetry~=1.3.0
pip install shtab
poetry install --no-interaction --only main,dev,test
- name: Lint with Pylint
run: |
Expand Down Expand Up @@ -86,6 +87,7 @@ jobs:
- name: Install dependencies
run: |
pip install poetry~=1.3.0
pip install shtab
poetry install --no-interaction --only main,dev,test
- name: Test typing with mypy
run: |
Expand Down
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Contributors
- Dimitris Apostolou <[email protected]>
- Dirk Brömmel <[email protected]>
- Dmitry Bogatov <[email protected]>
- Emil Velikov <[email protected]>
- Ethan Kerrick <[email protected]>
- FeRD (Frank Dana) <[email protected]>
- FestplattenSchnitzel <[email protected]>
Expand Down
7 changes: 7 additions & 0 deletions docs/man/reuse.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
..
SPDX-FileCopyrightText: 2019 Free Software Foundation Europe e.V. <https://fsfe.org>
SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>
SPDX-License-Identifier: CC-BY-SA-4.0

Expand Down Expand Up @@ -77,6 +78,12 @@ Options
current working directory's VCS repository, or to the current working
directory.

.. option:: -s, --print-completion SHELL

Print a static shell completion file, for the given shell. This option depends
on python-shtab and as such defines which shells are supported. Presently this
includes bash, tcsh and zsh, with fish support being proposed.

.. option:: -h, --help

Display help and exit. If no command is provided, this option is implied.
Expand Down
7 changes: 7 additions & 0 deletions src/reuse/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-FileCopyrightText: 2024 Carmen Bianca BAKKER <[email protected]>
# SPDX-FileCopyrightText: © 2020 Liferay, Inc. <https://liferay.com>
# SPDX-FileCopyrightText: 2024 Kerry McAdams <github@klmcadams>
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -103,6 +104,12 @@ def parser() -> argparse.ArgumentParser:
type=PathType("r", force_directory=True),
help=_("define root of project"),
)
try:
import shtab

shtab.add_argument_to(parser, ["-s", "--print-completion"])
except ImportError:
pass
parser.add_argument(
"--version",
action="store_true",
Expand Down

0 comments on commit 49adbf3

Please sign in to comment.