diff options
author | Christian Schwede <cschwede@redhat.com> | 2015-12-02 09:49:50 +0000 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2015-12-30 14:22:01 -0800 |
commit | 109e8f519f103334cc49479b3fb552084eb2929a (patch) | |
tree | b81d9946a9ef452f2992b790619235caa110ebe3 | |
parent | a564eb0e4cad452c5556c54c752ed896d0eb25af (diff) | |
download | python-swiftclient-109e8f519f103334cc49479b3fb552084eb2929a.tar.gz |
Fix debug and info option parsing
The debug and info options need to be set before a subcommand method is called,
otherwise they are simply ignored. This is kind of irritating - other options
(for example -U, -A, -K) are usable after a positional command.
This patch fixes this, and commands like these are no longer ignoring --debug or --info:
swift stat --debug
swift list container --info
Co-Authored-By: Alistair Coles <alistair.coles@hpe.com>
Change-Id: Ib19b05deef7a015881f1eed4a3946025e16bf922
-rwxr-xr-x | swiftclient/shell.py | 15 | ||||
-rw-r--r-- | tests/unit/test_shell.py | 15 |
2 files changed, 22 insertions, 8 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py index 8fc5c0c..010739c 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -1067,6 +1067,13 @@ def parse_args(parser, args, enforce_requires=True): if not args: args = ['-h'] (options, args) = parser.parse_args(args) + if enforce_requires and (options.debug or options.info): + logging.getLogger("swiftclient") + if options.debug: + logging.basicConfig(level=logging.DEBUG) + logging.getLogger('iso8601').setLevel(logging.WARNING) + elif options.info: + logging.basicConfig(level=logging.INFO) if len(args) > 1 and args[1] == '--help': _help = globals().get('st_%s_help' % args[0], @@ -1415,14 +1422,6 @@ Examples: signal.signal(signal.SIGINT, immediate_exit) - if options.debug or options.info: - logging.getLogger("swiftclient") - if options.debug: - logging.basicConfig(level=logging.DEBUG) - logging.getLogger('iso8601').setLevel(logging.WARNING) - elif options.info: - logging.basicConfig(level=logging.INFO) - with OutputManager() as output: parser.usage = globals()['st_%s_help' % args[0]] diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py index cac4da2..4dec22c 100644 --- a/tests/unit/test_shell.py +++ b/tests/unit/test_shell.py @@ -16,6 +16,7 @@ from __future__ import unicode_literals from genericpath import getmtime import hashlib +import logging import mock import os import tempfile @@ -1040,6 +1041,20 @@ class TestSubcommandHelp(testtools.TestCase): self.assertEqual(out.strip('\n'), expected) +@mock.patch.dict(os.environ, mocked_os_environ) +class TestOptionAfterPosArg(testtools.TestCase): + @mock.patch('logging.basicConfig') + @mock.patch('swiftclient.service.Connection') + def test_option_after_posarg(self, connection, mock_logging): + argv = ["", "stat", "--info"] + swiftclient.shell.main(argv) + mock_logging.assert_called_with(level=logging.INFO) + + argv = ["", "stat", "--debug"] + swiftclient.shell.main(argv) + mock_logging.assert_called_with(level=logging.DEBUG) + + class TestBase(testtools.TestCase): """ Provide some common methods to subclasses |