Repository: aio-libs/aiosmtpd

Issue:
Add CLI support for `--version`
Subject says it all.

Discussion:

Patch:
diff --git a/aiosmtpd/docs/NEWS.rst b/aiosmtpd/docs/NEWS.rst
index b69ec8c..c53b1e6 100644
--- a/aiosmtpd/docs/NEWS.rst
+++ b/aiosmtpd/docs/NEWS.rst
@@ -11,6 +11,8 @@
   ignored.  **API BREAK** If you have a handler that implements
   ``handle_NOOP()``, it previously took zero arguments but now requires a
   single argument.  (Closes #107)
+* The command line options ``--version`` / ``-v`` has been added to print the
+  package's current version number.  (Closes #111)
 * General improvements in the ``Controller`` class.  (Closes #104)
 * When aiosmtpd handles a ``STARTTLS`` it must arrange for the original
   transport to be closed when the wrapped transport is closed.  This fixes a
diff --git a/aiosmtpd/main.py b/aiosmtpd/main.py
index 40fca4c..6e0c338 100644
--- a/aiosmtpd/main.py
+++ b/aiosmtpd/main.py
@@ -4,7 +4,7 @@ import signal
 import asyncio
 import logging
 
-from aiosmtpd.smtp import DATA_SIZE_DEFAULT, SMTP
+from aiosmtpd.smtp import DATA_SIZE_DEFAULT, SMTP, __version__
 from argparse import ArgumentParser
 from functools import partial
 from importlib import import_module
@@ -28,6 +28,9 @@ def parseargs(args=None):
     parser = ArgumentParser(
         prog=PROGRAM,
         description='An RFC 5321 SMTP server with extensions.')
+    parser.add_argument(
+        '-v', '--version', action='version',
+        version='%(prog)s {}'.format(__version__))
     parser.add_argument(
         '-n', '--nosetuid',
         dest='setuid', default=True, action='store_false',


Test patch:
diff --git a/aiosmtpd/tests/test_main.py b/aiosmtpd/tests/test_main.py
index 0eb7357..85518f1 100644
--- a/aiosmtpd/tests/test_main.py
+++ b/aiosmtpd/tests/test_main.py
@@ -6,7 +6,7 @@ import unittest
 
 from aiosmtpd.handlers import Debugging
 from aiosmtpd.main import main, parseargs
-from aiosmtpd.smtp import SMTP
+from aiosmtpd.smtp import SMTP, __version__
 from contextlib import ExitStack
 from functools import partial
 from io import StringIO
@@ -309,3 +309,23 @@ class TestParseArgs(unittest.TestCase):
             self.assertEqual(cm.exception.code, 2)
         usage_lines = stderr.getvalue().splitlines()
         self.assertEqual(usage_lines[-1][-24:], 'Invalid port number: foo')
+
+    def test_version(self):
+        stdout = StringIO()
+        with ExitStack() as resources:
+            resources.enter_context(patch('sys.stdout', stdout))
+            resources.enter_context(patch('aiosmtpd.main.PROGRAM', 'smtpd'))
+            cm = resources.enter_context(self.assertRaises(SystemExit))
+            parseargs(('--version',))
+            self.assertEqual(cm.exception.code, 0)
+        self.assertEqual(stdout.getvalue(), 'smtpd {}\n'.format(__version__))
+
+    def test_v(self):
+        stdout = StringIO()
+        with ExitStack() as resources:
+            resources.enter_context(patch('sys.stdout', stdout))
+            resources.enter_context(patch('aiosmtpd.main.PROGRAM', 'smtpd'))
+            cm = resources.enter_context(self.assertRaises(SystemExit))
+            parseargs(('-v',))
+            self.assertEqual(cm.exception.code, 0)
+        self.assertEqual(stdout.getvalue(), 'smtpd {}\n'.format(__version__))