Skip to content

Commit 888e33f

Browse files
committed
command: add optional shell completion support using argcomplete
1 parent ab1bdb3 commit 888e33f

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ content is hashed. If it does, only content of the first capturing
1818
group is.
1919

2020
The command line tool operates as a pipe, reading standard input and
21-
outputting to standard output.
21+
outputting to standard output. It has optional shell completion support
22+
using [argcomplete](https://pypi.org/project/argcomplete/).
2223

2324
## Examples
2425

bin/hashpipe

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/python3 -u
2+
# PYTHON_ARGCOMPLETE_OK
23

34
"""Hashpipe entry point."""
45

hashpipe/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,16 @@ def main(
145145
help="Prefix to add in replacements",
146146
)
147147

148-
parser.add_argument(
148+
algorithm_arg = parser.add_argument(
149149
"-a",
150150
"--algorithm",
151151
type=str,
152152
default=DEFAULT_ALGORITHM,
153153
help="Digest algorithm to use, one of: %s"
154154
% ", ".join(sorted(_available_algorithms(), key=lambda x: x.lower())),
155155
)
156+
# type ignore: argcomplete adds the "completer" attribute
157+
algorithm_arg.completer = lambda **_: _available_algorithms() # type: ignore
156158

157159
parser.add_argument(
158160
"-A",
@@ -175,6 +177,13 @@ def pattern(arg: str) -> Pattern[bytes]:
175177
"regex", type=pattern, metavar="REGEX", help="Regular expression to match"
176178
)
177179

180+
try:
181+
import argcomplete # type: ignore # pylint: disable=import-outside-toplevel
182+
except ImportError:
183+
pass
184+
else:
185+
argcomplete.autocomplete(parser)
186+
178187
args = parser.parse_args()
179188

180189
if args.available_algorithms:

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ tests_require =
2727
packages = hashpipe
2828
scripts = bin/hashpipe
2929

30+
[options.extras_require]
31+
completion =
32+
argcomplete
33+
3034
[options.package_data]
3135
hashpipe =
3236
py.typed

0 commit comments

Comments
 (0)