Add version option to CLI.

This commit is contained in:
sneakypete81 2013-07-06 11:38:32 +01:00
parent 9fc9177742
commit a8bde4a438
2 changed files with 23 additions and 10 deletions

View file

@ -4,7 +4,7 @@ import sys
import json
from optparse import OptionParser
from openphoto import OpenPhoto
import openphoto
CONFIG_ERROR = """
You must create a configuration file with the following contents:
@ -44,6 +44,8 @@ def main(args=sys.argv[1:]):
action="store_true", dest="pretty", default=False)
parser.add_option('-v', help="Verbose output",
action="store_true", dest="verbose", default=False)
parser.add_option('--version', help="Display the current version information",
action="store_true")
parser.add_option('--help', help='show this help message',
action="store_true")
@ -53,6 +55,10 @@ def main(args=sys.argv[1:]):
parser.print_help()
return
if options.version:
print(openphoto.__version__)
return
if args:
parser.error("Unknown argument: %s" % args)
@ -64,10 +70,10 @@ def main(args=sys.argv[1:]):
# Host option overrides config file settings
if options.host:
client = OpenPhoto(host=options.host)
client = openphoto.OpenPhoto(host=options.host)
else:
try:
client = OpenPhoto(config_file=options.config_file)
client = openphoto.OpenPhoto(config_file=options.config_file)
except IOError as error:
print(error)
print(CONFIG_ERROR)

View file

@ -23,7 +23,7 @@ def raise_exception(_):
class TestCli(unittest.TestCase):
test_file = os.path.join("tests", "unit", "data", "test_file.txt")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_defaults(self, _, mock_openphoto):
"""Check that the default behaviour is correct"""
@ -32,14 +32,14 @@ class TestCli(unittest.TestCase):
mock_openphoto.assert_called_with(config_file=None)
get.assert_called_with("/photos/list.json", process_response=False)
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_config(self, _, mock_openphoto):
"""Check that a config file can be specified"""
main(["--config=test"])
mock_openphoto.assert_called_with(config_file="test")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_get(self, mock_stdout, mock_openphoto):
"""Check that the get operation is working"""
@ -52,7 +52,7 @@ class TestCli(unittest.TestCase):
process_response=False)
self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_post(self, mock_stdout, mock_openphoto):
"""Check that the post operation is working"""
@ -65,7 +65,7 @@ class TestCli(unittest.TestCase):
files={}, process_response=False)
self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_post_files(self, _, mock_openphoto):
"""Check that files are posted correctly"""
@ -104,7 +104,7 @@ class TestCli(unittest.TestCase):
mock_stdout.getvalue())
self.assertIn("To get your credentials", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_verbose(self, mock_stdout, _):
"""Check that the verbose option is working"""
@ -112,7 +112,7 @@ class TestCli(unittest.TestCase):
self.assertIn("Method: GET", mock_stdout.getvalue())
self.assertIn("Endpoint: /photos/list.json", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch.object(openphoto.main.openphoto, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_pretty_print(self, mock_stdout, mock_openphoto):
"""Check that the pretty-print option is working"""
@ -120,3 +120,10 @@ class TestCli(unittest.TestCase):
get.return_value = '{"test":1}'
main(["-p"])
self.assertEqual(mock_stdout.getvalue(), '{\n "test":1\n}\n')
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_version(self, mock_stdout):
"""Check that the version string is correctly printed"""
main(["--version"])
self.assertEqual(mock_stdout.getvalue(), openphoto.__version__ + "\n")