Add version option to CLI.
This commit is contained in:
parent
9fc9177742
commit
a8bde4a438
2 changed files with 23 additions and 10 deletions
|
@ -4,7 +4,7 @@ import sys
|
||||||
import json
|
import json
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
from openphoto import OpenPhoto
|
import openphoto
|
||||||
|
|
||||||
CONFIG_ERROR = """
|
CONFIG_ERROR = """
|
||||||
You must create a configuration file with the following contents:
|
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)
|
action="store_true", dest="pretty", default=False)
|
||||||
parser.add_option('-v', help="Verbose output",
|
parser.add_option('-v', help="Verbose output",
|
||||||
action="store_true", dest="verbose", default=False)
|
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',
|
parser.add_option('--help', help='show this help message',
|
||||||
action="store_true")
|
action="store_true")
|
||||||
|
|
||||||
|
@ -53,6 +55,10 @@ def main(args=sys.argv[1:]):
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if options.version:
|
||||||
|
print(openphoto.__version__)
|
||||||
|
return
|
||||||
|
|
||||||
if args:
|
if args:
|
||||||
parser.error("Unknown argument: %s" % args)
|
parser.error("Unknown argument: %s" % args)
|
||||||
|
|
||||||
|
@ -64,10 +70,10 @@ def main(args=sys.argv[1:]):
|
||||||
|
|
||||||
# Host option overrides config file settings
|
# Host option overrides config file settings
|
||||||
if options.host:
|
if options.host:
|
||||||
client = OpenPhoto(host=options.host)
|
client = openphoto.OpenPhoto(host=options.host)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
client = OpenPhoto(config_file=options.config_file)
|
client = openphoto.OpenPhoto(config_file=options.config_file)
|
||||||
except IOError as error:
|
except IOError as error:
|
||||||
print(error)
|
print(error)
|
||||||
print(CONFIG_ERROR)
|
print(CONFIG_ERROR)
|
||||||
|
|
|
@ -23,7 +23,7 @@ def raise_exception(_):
|
||||||
class TestCli(unittest.TestCase):
|
class TestCli(unittest.TestCase):
|
||||||
test_file = os.path.join("tests", "unit", "data", "test_file.txt")
|
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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_defaults(self, _, mock_openphoto):
|
def test_defaults(self, _, mock_openphoto):
|
||||||
"""Check that the default behaviour is correct"""
|
"""Check that the default behaviour is correct"""
|
||||||
|
@ -32,14 +32,14 @@ class TestCli(unittest.TestCase):
|
||||||
mock_openphoto.assert_called_with(config_file=None)
|
mock_openphoto.assert_called_with(config_file=None)
|
||||||
get.assert_called_with("/photos/list.json", process_response=False)
|
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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_config(self, _, mock_openphoto):
|
def test_config(self, _, mock_openphoto):
|
||||||
"""Check that a config file can be specified"""
|
"""Check that a config file can be specified"""
|
||||||
main(["--config=test"])
|
main(["--config=test"])
|
||||||
mock_openphoto.assert_called_with(config_file="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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_get(self, mock_stdout, mock_openphoto):
|
def test_get(self, mock_stdout, mock_openphoto):
|
||||||
"""Check that the get operation is working"""
|
"""Check that the get operation is working"""
|
||||||
|
@ -52,7 +52,7 @@ class TestCli(unittest.TestCase):
|
||||||
process_response=False)
|
process_response=False)
|
||||||
self.assertEqual(mock_stdout.getvalue(), "Result\n")
|
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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_post(self, mock_stdout, mock_openphoto):
|
def test_post(self, mock_stdout, mock_openphoto):
|
||||||
"""Check that the post operation is working"""
|
"""Check that the post operation is working"""
|
||||||
|
@ -65,7 +65,7 @@ class TestCli(unittest.TestCase):
|
||||||
files={}, process_response=False)
|
files={}, process_response=False)
|
||||||
self.assertEqual(mock_stdout.getvalue(), "Result\n")
|
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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_post_files(self, _, mock_openphoto):
|
def test_post_files(self, _, mock_openphoto):
|
||||||
"""Check that files are posted correctly"""
|
"""Check that files are posted correctly"""
|
||||||
|
@ -104,7 +104,7 @@ class TestCli(unittest.TestCase):
|
||||||
mock_stdout.getvalue())
|
mock_stdout.getvalue())
|
||||||
self.assertIn("To get your credentials", 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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_verbose(self, mock_stdout, _):
|
def test_verbose(self, mock_stdout, _):
|
||||||
"""Check that the verbose option is working"""
|
"""Check that the verbose option is working"""
|
||||||
|
@ -112,7 +112,7 @@ class TestCli(unittest.TestCase):
|
||||||
self.assertIn("Method: GET", mock_stdout.getvalue())
|
self.assertIn("Method: GET", mock_stdout.getvalue())
|
||||||
self.assertIn("Endpoint: /photos/list.json", 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)
|
@mock.patch('sys.stdout', new_callable=io.StringIO)
|
||||||
def test_pretty_print(self, mock_stdout, mock_openphoto):
|
def test_pretty_print(self, mock_stdout, mock_openphoto):
|
||||||
"""Check that the pretty-print option is working"""
|
"""Check that the pretty-print option is working"""
|
||||||
|
@ -120,3 +120,10 @@ class TestCli(unittest.TestCase):
|
||||||
get.return_value = '{"test":1}'
|
get.return_value = '{"test":1}'
|
||||||
main(["-p"])
|
main(["-p"])
|
||||||
self.assertEqual(mock_stdout.getvalue(), '{\n "test":1\n}\n')
|
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")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue