Adam Lindsay avatar Adam Lindsay committed 1606832

Added parsing options for all utilities.

Comments (0)

Files changed (5)

 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT
-from location import ping_until_found, loc2str, NodeNotFound
+from storeserver import remote_call, DEFAULTPORT, parser
+from location import ping_until_found, str2loc, NodeNotFound
 
 usage = '''
-  python storeget.py <key>
+  python %prog [options] <key>
 
-Looks for a storage node on the localhost starting from 
-the default port. Sends the remote command there, which 
-gets sent out to the actual node that has it. Returns the
-stored value for the key.
-'''
+Looks for a storage node at PEER, either as specified, or 
+auto-discovered on the localhost starting from the default 
+port. Sends the remote command there, which gets forwarded 
+to the actual node that has it.'''
+
+parser.set_usage(usage)
+parser.remove_option('--port')
 
 if __name__ == '__main__':
-    try:
-        key = sys.argv[1]
-    except:
-        print usage
-        sys.exit()
-    try:
-        loc = ping_until_found(Location('localhost', DEFAULTPORT))
-    except NodeNotFound:
-        print 'No host autodiscovered.'
-        sys.exit()
+    (options, args) = parser.parse_args()
+    if len(args) != 1:
+        parser.error("incorrect number of arguments")
+    (key,) = args
+    if options.peer:
+        loc = str2loc(options.peer)
+    else:
+        try:
+            loc = ping_until_found(Location('localhost', DEFAULTPORT))
+        except NodeNotFound:
+            print 'No peer autodiscovered.'
+            sys.exit()
     value = remote_call(loc, 'get', key)
     print value
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT
-from location import ping_until_found, loc2str, NodeNotFound
+from storeserver import remote_call, DEFAULTPORT, parser
+from location import ping_until_found, str2loc, NodeNotFound
 
 usage = '''
-  python storeprimer.py
+  python %prog
 
-Looks for a storage node on the localhost starting from 
-the default port. Sends the NATO alphabet to prime the 
-distributed store.
-'''
+Looks for a storage node at PEER, either as specified, or 
+auto-discovered on the localhost starting from the default 
+port. Sends the NATO alphabet to prime the distributed store.'''
+
+parser.set_usage(usage)
+parser.remove_option('--port')
 
 DICTIONARY = {
 'A': 'Alpha',
 'Y': 'Yankee',
 'Z': 'Zulu',
 }
+
 if __name__ == '__main__':
-    try:
-        loc = ping_until_found(Location('localhost', DEFAULTPORT))
-    except NodeNotFound:
-        print 'No host autodiscovered.'
-        sys.exit()
+    (options, args) = parser.parse_args()
+    if options.peer:
+        loc = str2loc(options.peer)
+    else:
+        try:
+            loc = ping_until_found(Location('localhost', DEFAULTPORT))
+        except NodeNotFound:
+            print 'No peer autodiscovered.'
+            sys.exit()
     for key, value in DICTIONARY.items():
         remote_call(loc, 'put', key, value)
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT
-from location import ping_until_found, loc2str, NodeNotFound
+from storeserver import remote_call, DEFAULTPORT, parser
+from location import ping_until_found, str2loc, NodeNotFound
 
 usage = '''
-  python storeput.py <key> <value>
+  python %prog [options] <key> <value>
 
-Looks for a storage node on the localhost starting from 
-the default port. Sends the remote command there, which 
-gets sent out to the actual node that has it.
-'''
+Looks for a storage node at PEER, either as specified, or 
+auto-discovered on the localhost starting from the default 
+port. Sends the remote command there, which gets forwarded 
+to the actual node that has it.'''
+
+parser.set_usage(usage)
+parser.remove_option('--port')
+
 if __name__ == '__main__':
-    try:
-        key = sys.argv[1]
-        value = sys.argv[2]
-    except:
-        print usage
-        sys.exit()
-    try:
-        loc = ping_until_found(Location('localhost', DEFAULTPORT))
-    except NodeNotFound:
-        print 'No host autodiscovered.'
-        sys.exit()
+    (options, args) = parser.parse_args()
+    if len(args) != 2:
+        parser.error("incorrect number of arguments")
+    (key, value) = args
+    if options.peer:
+        loc = str2loc(options.peer)
+    else:
+        try:
+            loc = ping_until_found(Location('localhost', DEFAULTPORT))
+        except NodeNotFound:
+            print 'No peer autodiscovered.'
+            sys.exit()
     remote_call(loc, 'put', key, value)
 WAITPERIOD = 0.01
 
 usage = '''
-  python %s [[peer] port]
+  python %prog [options]
 
-Starts a distributed key-value storage node on the designated port
-and contacting the designated peer. In the absence of these two, it 
-attempts to autodiscover an open port and a peer on localhost, working
-from the default port, %d.
+Starts a distributed key-value storage node on the designated PORT
+and contacting the designated PEER. In the absence of either of these
+two, it attempts to autodiscover an open port and a peer on localhost,
+working from the default port, 9900.
 
 After auto-joining, a node will receive key-value pairs from its 
 neighbors. When exiting cleanly (e.g., with a KeyboardInterrupt), the
-node hands off all its items to the appropriate neighbors.
+node hands off all its items to the appropriate neighbors.''' 
 
-Usage can be obtained with -h or --help as the first argument.
-''' % (sys.argv[0], DEFAULTPORT)
-
+parser = location.parser
+parser.set_usage(usage)
 
 def remote_call(destination, method, *args):
     transport = TSocket.TSocket(destination.address, destination.port)
     print 'done.'
 
 if __name__ == '__main__':
-    inputargs = {}
-    try:
-        if '-h' in sys.argv[1]:
-            print usage
-            sys.exit()
-        inputargs['port'] = int(sys.argv[-1])
-        inputargs['peer'] = location.str2loc(sys.argv[-2])
-    except StandardError:
-        pass
-    if 'port' not in inputargs:
+    (options, args) = parser.parse_args()
+    if not options.port:
         loc = location.ping_until_not_found(Location('localhost', DEFAULTPORT), 25)
-        inputargs['port'] = loc.port
-    if 'peer' not in inputargs:
+        options.port = loc.port
+    if options.peer:
+        options.peer = location.str2loc(options.peer)
+    else:
         try:
-            loc = location.ping_until_found(Location('localhost', DEFAULTPORT))
-            inputargs['peer'] = loc
+            options.peer = location.ping_until_found(Location('localhost', DEFAULTPORT))
         except location.NodeNotFound:
             print 'No peer autodiscovered.'
-    main(inputargs)
+    main(options.__dict__)
 
 sys.path.append('gen-py')
 
 from locator.ttypes import Location
-from storeserver import remote_call, DEFAULTPORT
-from location import ping_until_found, loc2str, NodeNotFound
+from storeserver import remote_call, DEFAULTPORT, parser
+from location import ping_until_found, str2loc, NodeNotFound
 
 usage = '''
-  python storeget.py <key>
+  python %prog
 
-Looks for a storage node on the localhost starting from 
-the default port. Sends a bunch of keys for resolution.
-'''
+Looks for a storage node at PEER, either as specified, or 
+auto-discovered on the localhost starting from the default 
+port. Sends a bunch of keys for resolution.'''
+
+parser.set_usage(usage)
+parser.remove_option('--port')
 
 KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
 if __name__ == '__main__':
-    try:
-        loc = ping_until_found(Location('localhost', DEFAULTPORT))
-    except NodeNotFound:
-        print 'No host autodiscovered.'
-        sys.exit()
+    (options, args) = parser.parse_args()
+    if options.peer:
+        loc = str2loc(options.peer)
+    else:
+        try:
+            loc = ping_until_found(Location('localhost', DEFAULTPORT))
+        except NodeNotFound:
+            print 'No peer autodiscovered.'
+            sys.exit()
     for key in KEYS:
         value = remote_call(loc, 'get', key)
         if value:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.