1. Timothy Corbett-Clark
  2. mock-http-server

Commits

Cmed Technology  committed a6ba7b8 Draft

Updating to use newer argparse rather than optparse (PEP-389)

  • Participants
  • Parent commits 2ae21a0
  • Branches default

Comments (0)

Files changed (1)

File mockserver.py

View file
 
 """
 from flask import Flask, request, json, jsonify, make_response
-import optparse
-import sys
+import argparse
 
 app = Flask(__name__)
 
     return response
 
 
-def main(argv):
-    parser = optparse.OptionParser(__doc__)
-    parser.add_option("--debug", dest="debug", action="store_const",
-                      const=True, default=True)
-    parser.add_option("--no-debug", dest="debug", action="store_const",
-                      const=False, default=True)
-    parser.add_option("--host", dest="host", default="127.0.0.1")
-    parser.add_option("--port", dest="port", type=int, default="5000")
-    options, args = parser.parse_args(argv)
-    if len(args) > 0:
-        parser.error("Unexpected: %r" % (args,))
-    options = eval(str(options))
-    app.run(**options)
+def parse_args():
+    parser = argparse.ArgumentParser(
+        formatter_class=argparse.RawDescriptionHelpFormatter,
+        description=__doc__)
+    parser.add_argument(
+        '--debug', action='store_true', default=False,
+        help='use debug mode (and reloader)')
+    parser.add_argument(
+        '--host', default='127.0.0.1', type=str,
+        help='host on which to bind')
+    parser.add_argument(
+        '--port', default=5000, type=int,
+        help='port on which to bind')
+    return parser.parse_args()
+
 
 if __name__ == "__main__":
-    sys.exit(main(sys.argv[1:]))
+    args = parse_args()
+    app.run(debug=args.debug, host=args.host, port=args.port)