Skip to content

Commit

Permalink
Add --version command line option (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Apr 15, 2015
1 parent 41a206a commit 29dc290
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Change log
4.0.2 (unreleased)
==================

- TBD
- Add ``--version`` command line option (fixes
https://github.com/zopefoundation/zdaemon/issues/4).

4.0.1 (2014-12-26)
==================
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def alltests():
zip_safe=False,
entry_points=entry_points,
include_package_data = True,
install_requires=["ZConfig"],
install_requires=["ZConfig", "setuptools"],
extras_require=dict(test=tests_require),
test_suite='__main__.alltests',
tests_require=tests_require
Expand Down
14 changes: 12 additions & 2 deletions src/zdaemon/tests/testzdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ def restore_streams(self):
sys.stdout = self.save_stdout
sys.stderr = self.save_stderr

def check_exit_code(self, options, args):
def check_exit_code(self, options, args, exit_code=2):
save_sys_stderr = sys.stderr
try:
sys.stderr = StringIO()
try:
options.realize(args)
except SystemExit as err:
self.assertEqual(err.code, 2)
self.assertEqual(err.code, exit_code)
else:
self.fail("SystemExit expected")
finally:
Expand Down Expand Up @@ -157,6 +157,16 @@ class HasHelp(self.OptionsClass):
__doc__ = 'Some help'
self.help_test_helper(HasHelp, {'doc': 'Example help'}, 'Example help')

def test_version(self):
options = self.OptionsClass()
options.version = '2.4.frog-knows'
self.save_streams()
try:
self.check_exit_code(options, ['--version'], exit_code=0)
finally:
self.restore_streams()
self.assertNotEqual(self.stdout.getvalue(), "2.4.frog-knows")

def test_unrecognized(self):
# Check that we get an error for an unrecognized option
self.check_exit_code(self.OptionsClass(), ["-/"])
Expand Down
1 change: 1 addition & 0 deletions src/zdaemon/zdctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
-u/--user USER -- run as this user (or numeric uid)
-m/--umask UMASK -- use this umask for daemon subprocess (default is 022)
-x/--exit-codes LIST -- list of fatal exit codes (default "0,2")
--version -- print zdaemon version and exit
-z/--directory DIRECTORY -- directory to chdir to when using -d (default off)
action [arguments] -- see below
Expand Down
10 changes: 10 additions & 0 deletions src/zdaemon/zdoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
import sys
import getopt

import pkg_resources
import ZConfig


class ZDOptions:
"""a zdaemon script.
Expand All @@ -27,6 +29,7 @@ class ZDOptions:
Options:
-C/--configure URL -- configuration file or URL
-h/--help -- print usage message and exit
--version -- print zdaemon version and exit
Actions are commands like "start", "stop" and "status". If -i is
specified or no action is specified on the command line, a "shell"
Expand Down Expand Up @@ -63,10 +66,17 @@ def __init__(self):
self.required_map = {}
self.environ_map = {}
self.zconfig_options = []
self.version = pkg_resources.get_distribution("zdaemon").version
self.add(None, None, "h", "help", self.help)
self.add(None, None, None, "version", self.print_version)
self.add("configfile", None, "C:", "configure=")
self.add(None, None, "X:", handler=self.zconfig_options.append)

def print_version(self, dummy):
"""Print zdaemon version number to stdout and exit(0)."""
print(self.version)
sys.exit(0)

def help(self, dummy):
"""Print a long help message (self.doc) to stdout and exit(0).
Expand Down

0 comments on commit 29dc290

Please sign in to comment.