From 818a7f807eb594f1822a376108f4682f540eb0ca Mon Sep 17 00:00:00 2001 From: Scott Kitterman Date: Sun, 4 Jun 2017 17:05:18 -0400 Subject: [PATCH] - Port dkimsign.py to use argparse; now gives standard usage message and is more extensible --- ChangeLog | 3 ++- README | 3 ++- dkimsign.py | 23 +++++++++++------------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1f40d88..e61bfac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ UNRELEASED Version 0.7.0 - - + - Port dkimsign.py to use argparse; now gives standard usage message and + is more extensible 2017-05-30 Version 0.6.2 - Fixed problem with header folding that caused the first line to be diff --git a/README b/README index 1807c39..3047946 100644 --- a/README +++ b/README @@ -11,7 +11,7 @@ signing and verification. VERSION -This is dkimpy 0.6.2. +This is dkimpy 0.7.0. REQUIREMENTS @@ -19,6 +19,7 @@ REQUIREMENTS tested on python < 2.7 or python3 < 3.4, but may still work on python2.6 and python 3.1 - 3.3. - dnspython or pydns. dnspython is preferred if both are present. + - argparse. Standard library in python2.7 and later. INSTALLATION diff --git a/dkimsign.py b/dkimsign.py index 9aff199..021029f 100644 --- a/dkimsign.py +++ b/dkimsign.py @@ -24,29 +24,28 @@ from __future__ import print_function import sys +import argparse import dkim -if len(sys.argv) < 4 or len(sys.argv) > 5: - print("Usage: dkimsign.py selector domain privatekeyfile [identity]", file=sys.stderr) - sys.exit(1) +# Backward compatibility hack because argparse doesn't support optional +# positional arguments +arguments=['--'+arg if arg[:8] == 'identity' else arg for arg in sys.argv[1:]] +parser = argparse.ArgumentParser(description='Produce DKIM signature for email messages.') +parser.add_argument('selector', action="store") +parser.add_argument('domain', action="store") +parser.add_argument('privatekeyfile', action="store") +parser.add_argument('--identity', help='Optional value for i= tag.') +args=parser.parse_args(arguments) if sys.version_info[0] >= 3: # Make sys.stdin and stdout binary streams. sys.stdin = sys.stdin.detach() sys.stdout = sys.stdout.detach() -selector = sys.argv[1].encode('ascii') -domain = sys.argv[2].encode('ascii') -privatekeyfile = sys.argv[3] -if len(sys.argv) > 5: - identity = sys.argv[4].encode('ascii') -else: - identity = None - message = sys.stdin.read() try: - sig = dkim.sign(message, selector, domain, open(privatekeyfile, "rb").read(), identity = identity) + sig = dkim.sign(message, args.selector, args.domain, open(args.privatekeyfile, "rb").read(), identity = args.identity) sys.stdout.write(sig) sys.stdout.write(message) except Exception as e: