Merge pull request #39 from clarete/master
Using argparse instead of using sys.argv directly
This commit is contained in:
commit
195e4362b6
2
Makefile
2
Makefile
@ -6,5 +6,5 @@ init:
|
|||||||
|
|
||||||
test:
|
test:
|
||||||
rm -f .coverage
|
rm -f .coverage
|
||||||
@nosetests --with-coverage ./tests/
|
@nosetests -sv --with-coverage ./tests/
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
|
import argparse
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from werkzeug.routing import BaseConverter
|
from werkzeug.routing import BaseConverter
|
||||||
@ -36,19 +37,33 @@ def configure_urls(service):
|
|||||||
app.route(url_path, methods=HTTP_METHODS)(convert_flask_to_httpretty_response(handler))
|
app.route(url_path, methods=HTTP_METHODS)(convert_flask_to_httpretty_response(handler))
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(argv=sys.argv):
|
||||||
if len(args) not in range(2, 4):
|
# Yes, I'm using those imports in the beginning of the file to create a
|
||||||
print("Usage: moto_server <service> [port]")
|
# dynamic list of available services to be shown in the help text when the
|
||||||
sys.exit(1)
|
# user tries to interact with moto_server.
|
||||||
service_name = args[1]
|
available_services = [
|
||||||
configure_urls(service_name)
|
x.split('_')[0] for x in globals() if x.endswith('_backend')]
|
||||||
try:
|
|
||||||
port = int(args[2])
|
parser = argparse.ArgumentParser()
|
||||||
except IndexError:
|
parser.add_argument(
|
||||||
port = None
|
'service', type=str,
|
||||||
|
choices=available_services,
|
||||||
|
help='Choose which mechanism you want to run')
|
||||||
|
parser.add_argument(
|
||||||
|
'-H', '--host', type=str,
|
||||||
|
help='Which host to bind',
|
||||||
|
default='0.0.0.0')
|
||||||
|
parser.add_argument(
|
||||||
|
'-p', '--port', type=int,
|
||||||
|
help='Port number to use for connection',
|
||||||
|
default=5000)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
configure_urls(args.service)
|
||||||
|
|
||||||
app.testing = True
|
app.testing = True
|
||||||
app.run(port=port)
|
app.run(host=args.host, port=args.port)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -15,11 +15,11 @@ def test_wrong_arguments():
|
|||||||
|
|
||||||
@patch('moto.server.app.run')
|
@patch('moto.server.app.run')
|
||||||
def test_right_arguments(app_run):
|
def test_right_arguments(app_run):
|
||||||
main(["name", "s3"])
|
main(["s3"])
|
||||||
app_run.assert_called_once_with(port=None)
|
app_run.assert_called_once_with(host='0.0.0.0', port=5000)
|
||||||
|
|
||||||
|
|
||||||
@patch('moto.server.app.run')
|
@patch('moto.server.app.run')
|
||||||
def test_port_argument(app_run):
|
def test_port_argument(app_run):
|
||||||
main(["name", "s3", 8080])
|
main(["s3", "--port", "8080"])
|
||||||
app_run.assert_called_once_with(port=8080)
|
app_run.assert_called_once_with(host='0.0.0.0', port=8080)
|
||||||
|
Loading…
Reference in New Issue
Block a user