From 49adbf3cc45fc0021c3862e015dec0451e9aa355 Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Sun, 22 Sep 2024 11:57:57 +0100 Subject: [PATCH] Add shell completion via python-shtab 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: https://github.com/fsfe/reuse-tool/issues/629 Signed-off-by: Emil Velikov --- .github/workflows/test.yaml | 2 ++ AUTHORS.rst | 1 + docs/man/reuse.rst | 7 +++++++ src/reuse/_main.py | 7 +++++++ 4 files changed, 17 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7a056a1a..417633fe 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -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: | @@ -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: | diff --git a/AUTHORS.rst b/AUTHORS.rst index f15295bf..6bc2b4c8 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -39,6 +39,7 @@ Contributors - Dimitris Apostolou - Dirk Brömmel - Dmitry Bogatov +- Emil Velikov - Ethan Kerrick - FeRD (Frank Dana) - FestplattenSchnitzel diff --git a/docs/man/reuse.rst b/docs/man/reuse.rst index 9ffbd659..37bbe644 100644 --- a/docs/man/reuse.rst +++ b/docs/man/reuse.rst @@ -1,6 +1,7 @@ .. SPDX-FileCopyrightText: 2019 Free Software Foundation Europe e.V. SPDX-FileCopyrightText: © 2020 Liferay, Inc. + SPDX-FileCopyrightText: 2024 Emil Velikov SPDX-License-Identifier: CC-BY-SA-4.0 @@ -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. diff --git a/src/reuse/_main.py b/src/reuse/_main.py index 37d13305..d4745de6 100644 --- a/src/reuse/_main.py +++ b/src/reuse/_main.py @@ -3,6 +3,7 @@ # SPDX-FileCopyrightText: 2024 Carmen Bianca BAKKER # SPDX-FileCopyrightText: © 2020 Liferay, Inc. # SPDX-FileCopyrightText: 2024 Kerry McAdams +# SPDX-FileCopyrightText: 2024 Emil Velikov # # SPDX-License-Identifier: GPL-3.0-or-later @@ -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",