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>
---
 Makefile                           |  9 ++++++++-
 debian/control                     |  3 +++
 debian/mailscripts.bash-completion |  1 +
 debian/rules                       |  2 +-
 email-print-mime-structure         | 15 +++++++++++++++
 5 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 debian/mailscripts.bash-completion

diff --git a/Makefile b/Makefile
index 352f6f0..0cd06b7 100644
--- a/Makefile
+++ b/Makefile
@@ -3,14 +3,21 @@ MANPAGES=mdmv.1 mbox2maildir.1 \
        email-extract-openpgp-certs.1 \
        email-print-mime-structure.1 \
        notmuch-import-patch.1
+COMPLETIONS=completions/bash/email-print-mime-structure
 
-all: $(MANPAGES)
+all: $(MANPAGES) $(COMPLETIONS)
 
 clean:
        rm -f $(MANPAGES)
+       rm -rf completions
 
 %.1: %.1.pod
        pod2man --section=1 --date="Debian Project" --center="User Commands" \
                --utf8 \
                --name=$(subst .1,,$@) \
                $^ $@
+
+completions/bash/%:
+       mkdir -p completions/bash
+       register-python-argcomplete3 $(notdir $@) > $@.tmp
+       mv $@.tmp $@
diff --git a/debian/control b/debian/control
index f92f7a1..782636f 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,
 Suggests:
  gpg,
diff --git a/debian/mailscripts.bash-completion 
b/debian/mailscripts.bash-completion
new file mode 100644
index 0000000..435576f
--- /dev/null
+++ b/debian/mailscripts.bash-completion
@@ -0,0 +1 @@
+completions/bash/email-print-mime-structure
diff --git a/debian/rules b/debian/rules
index e8e22ba..6d50bf4 100755
--- a/debian/rules
+++ b/debian/rules
@@ -1,4 +1,4 @@
 #!/usr/bin/make -f
 
 %:
-       dh $@ --with elpa
+       dh $@ --with elpa --with bash-completion
diff --git a/email-print-mime-structure b/email-print-mime-structure
index 5497597..5e1660e 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
@@ -45,6 +46,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
@@ -152,6 +158,15 @@ def main() -> None:
     parser.add_argument('--use-gpg-agent', metavar='true|false', type=bool,
                         default=False,
                         help='Ask local GnuPG installation for decryption')
+
+    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

Reply via email to