[Lldb-commits] [lldb] r254533 - Use sub-commands instead of --mode={client, server}.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Wed Dec 2 11:00:52 PST 2015


Author: zturner
Date: Wed Dec  2 13:00:52 2015
New Revision: 254533

URL: http://llvm.org/viewvc/llvm-project?rev=254533&view=rev
Log:
Use sub-commands instead of --mode={client,server}.

This is more pythonic and allows a more idiomatic way of getting
detailed usage information for each individual sub-command.

Modified:
    lldb/trunk/scripts/swig_bot.py
    lldb/trunk/scripts/swig_bot_lib/client.py
    lldb/trunk/scripts/swig_bot_lib/server.py

Modified: lldb/trunk/scripts/swig_bot.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/swig_bot.py?rev=254533&r1=254532&r2=254533&view=diff
==============================================================================
--- lldb/trunk/scripts/swig_bot.py (original)
+++ lldb/trunk/scripts/swig_bot.py Wed Dec  2 13:00:52 2015
@@ -14,16 +14,27 @@ import traceback
 # LLDB modules
 import use_lldb_suite
 
+# swig_bot modules
+from swig_bot_lib import client
+from swig_bot_lib import server
+
 def process_args(args):
     parser = argparse.ArgumentParser(
         description='Run swig-bot client or server.')
 
-    # Arguments to control whether swig-bot runs as a client or server.
-    parser.add_argument(
-        "--mode",
-        required=True,
-        choices=["client", "server"],
-        help="Run swig_bot in either client or server mode.")
+    # Create and populate subparser arguments for when swig_bot is
+    # run in client or server mode
+    subparsers = parser.add_subparsers(
+        help="Pass --help to a sub-command to print detailed usage")
+    client_parser = subparsers.add_parser("client",
+                                          help="Run SWIG generation client")
+    client.add_subparser_args(client_parser)
+    client_parser.set_defaults(func=run_client)
+
+    server_parser = subparsers.add_parser("server",
+                                          help="Run SWIG generation server")
+    server.add_subparser_args(server_parser)
+    server_parser.set_defaults(func=run_server)
 
     # Arguments to control logging verbosity.
     parser.add_argument(
@@ -32,7 +43,7 @@ def process_args(args):
         default=False,
         help="Increase logging verbosity level.")
 
-    (options, remaining) = parser.parse_known_args(args)
+    options = parser.parse_args(args)
     # Set logging level.
     if options.verbose:
         log_level = logging.DEBUG
@@ -41,22 +52,26 @@ def process_args(args):
     logging.basicConfig(level=log_level)
     logging.info("logging is using level: %d", log_level)
 
-    return (options, remaining)
+    return options
+
+def run_client(options):
+    logging.info("Running swig_bot in client mode")
+    client.finalize_subparser_options(options)
+    client.run(options)
+
+def run_server(options):
+    logging.info("Running swig_bot in server mode")
+    server.finalize_subparser_options(options)
+    server.run(options)
 
 if __name__ == "__main__":
-    (options, remaining) = process_args(sys.argv[1:])
+    options = process_args(sys.argv[1:])
     try:
-        if options.mode == "client":
-            logging.info("Running swig_bot in client mode")
-            from swig_bot_lib import client
-            client.run(remaining)
-        elif options.mode == "server":
-            logging.info("Running swig_bot in server mode")
-            from swig_bot_lib import server
-            server.run(remaining)
-        else:
+        if options.func is None:
             logging.error("Unknown mode specified.  Expected client or server.")
             sys.exit(-1)
+        else:
+            options.func(options)
     except KeyboardInterrupt as e:
         logging.info("Ctrl+C received.  Shutting down...")
         sys.exit(-1)

Modified: lldb/trunk/scripts/swig_bot_lib/client.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/swig_bot_lib/client.py?rev=254533&r1=254532&r2=254533&view=diff
==============================================================================
--- lldb/trunk/scripts/swig_bot_lib/client.py (original)
+++ lldb/trunk/scripts/swig_bot_lib/client.py Wed Dec  2 13:00:52 2015
@@ -30,7 +30,7 @@ from . import remote
 default_ip = "127.0.0.1"
 default_port = 8537
 
-def process_args(args):
+def add_subparser_args(parser):
     """Returns options processed from the provided command line.
 
     @param args the command line to process.
@@ -75,10 +75,6 @@ def process_args(args):
                     raise ValueError("Invalid connection string")
             setattr(namespace, self.dest, ip_port)
 
-    # Setup the parser arguments that are accepted.
-    parser = argparse.ArgumentParser(
-        description='Generate SWIG bindings.')
-
     parser.add_argument(
         "--local",
         action=FindLocalSwigAction,
@@ -112,9 +108,7 @@ def process_args(args):
         action="append",
         help="Specifies the language to generate bindings for")
 
-    # Process args.
-    options = parser.parse_args(args)
-
+def finalize_subparser_options(options):
     if options.languages is None:
         options.languages = ['python']
 
@@ -173,9 +167,7 @@ def handle_response(options, connection,
     finally:
         os.unlink(response_file_path)
 
-def run(args):
-    options = process_args(args)
-
+def run(options):
     if options.remote is None:
         logging.info("swig bot client using local swig installation at '{}'"
                      .format(options.swig_executable))

Modified: lldb/trunk/scripts/swig_bot_lib/server.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/swig_bot_lib/server.py?rev=254533&r1=254532&r2=254533&view=diff
==============================================================================
--- lldb/trunk/scripts/swig_bot_lib/server.py (original)
+++ lldb/trunk/scripts/swig_bot_lib/server.py Wed Dec  2 13:00:52 2015
@@ -33,10 +33,7 @@ from . import remote
 
 default_port = 8537
 
-def process_args(args):
-    # Setup the parser arguments that are accepted.
-    parser = argparse.ArgumentParser(description='SWIG generation server.')
-
+def add_subparser_args(parser):
     parser.add_argument(
         "--port",
         action="store",
@@ -49,8 +46,8 @@ def process_args(args):
         default=fs.find_executable("swig"),
         dest="swig_executable")
 
-    # Process args.
-    return parser.parse_args(args)
+def finalize_subparser_options(options):
+    pass
 
 def initialize_listening_socket(options):
     logging.debug("Creating socket...")
@@ -134,8 +131,7 @@ def accept_loop(sock, options):
             logging.error("An error occurred while processing the connection.")
             logging.error(error)
 
-def run(args):
-    options = process_args(args)
+def run(options):
     print(options)
     sock = initialize_listening_socket(options)
     accept_loop(sock, options)




More information about the lldb-commits mailing list