This is modeled after the use of argcomplete in diffoscope, and it should be possible to use it for any other pythonic mailscript that uses argparse.
Signed-off-by: Daniel Kahn Gillmor <d...@fifthhorseman.net> --- debian/clean | 1 + debian/control | 3 +++ debian/mailscripts.bash-completion | 1 + debian/rules | 8 +++++++- email-print-mime-structure | 16 ++++++++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 debian/clean create mode 100644 debian/mailscripts.bash-completion diff --git a/debian/clean b/debian/clean new file mode 100644 index 0000000..2b2f39b --- /dev/null +++ b/debian/clean @@ -0,0 +1 @@ +debian/email-print-mime-structure.bash-completion diff --git a/debian/control b/debian/control index fc2bccc..fd0b7f9 100644 --- a/debian/control +++ b/debian/control @@ -4,9 +4,11 @@ Priority: optional Maintainer: Sean Whitton <spwhit...@spwhitton.name> Standards-Version: 4.1.5 Build-Depends: + bash-completion, debhelper (>= 10), dh-elpa, perl, + python3-argcomplete, Vcs-Git: https://git.spwhitton.name/mailscripts Vcs-Browser: https://git.spwhitton.name/mailscripts @@ -39,6 +41,7 @@ Recommends: devscripts, git, notmuch, + python3-argcomplete, python3-pgpy, Architecture: all Description: collection of scripts for manipulating e-mail on Debian diff --git a/debian/mailscripts.bash-completion b/debian/mailscripts.bash-completion new file mode 100644 index 0000000..6455ca6 --- /dev/null +++ b/debian/mailscripts.bash-completion @@ -0,0 +1 @@ +debian/email-print-mime-structure.bash-completion email-print-mime-structure diff --git a/debian/rules b/debian/rules index e8e22ba..0aceb0d 100755 --- a/debian/rules +++ b/debian/rules @@ -1,4 +1,10 @@ #!/usr/bin/make -f %: - dh $@ --with elpa + dh $@ --with elpa --with bash-completion + +debian/email-print-mime-structure.bash-completion: + register-python-argcomplete3 email-print-mime-structure > $@ + +override_dh_auto_build: debian/email-print-mime-structure.bash-completion + dh_auto_build diff --git a/email-print-mime-structure b/email-print-mime-structure index 644efb1..eebd5e0 100755 --- a/email-print-mime-structure +++ b/email-print-mime-structure @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# PYTHON_ARGCOMPLETE_OK # -*- coding: utf-8 -*- # Copyright (C) 2019 Daniel Kahn Gillmor @@ -29,6 +30,7 @@ Example: If you want to number the parts, i suggest piping the output through something like "cat -n" ''' +import os import sys import email import logging @@ -43,6 +45,11 @@ try: except ImportError: pgpy = None +try: + import argcomplete #type: ignore +except ImportError: + argcomplete = None + class MimePrinter(object): def __init__(self, args:Namespace): self.args = args @@ -122,6 +129,15 @@ def main() -> None: epilog="Example: email-print-mime-structure <message.eml") parser.add_argument('--pgpkey', metavar='KEYFILE', action='append', help='OpenPGP Transferable Secret Key for decrypting') + + if argcomplete: + argcomplete.autocomplete(parser) + elif '_ARGCOMPLETE' in os.environ: + logging.error('Argument completion requested but the "argcomplete" ' + 'module is not installed. ' + 'Maybe you want to "apt install python3-argcomplete"') + sys.exit(1) + args:Namespace = parser.parse_args() msg:Union[Message, str, int, Any] = email.message_from_file(sys.stdin) -- 2.24.0