Jason R. Coombs committed d977205

Updated tcp to use argparse

Comments (0)

Files changed (1)


-from __future__ import print_function
+from __future__ import print_function, unicode_literals
 import socket
-from optparse import OptionParser
+import argparse
 from jaraco.util.string import local_format as lf
 def get_connect_options():
-	parser = OptionParser(conflict_handler="resolve")
-	parser.add_option('-h', '--host', default='localhost')
-	parser.add_option('-p', '--port', default=80, type='int')
-	options, args = parser.parse_args()
-	if not len(args) == 0:
-		parser.error("Unexpected positional argument")
-	return options
+	parser = argparse.ArgumentParser(conflict_handler="resolve")
+	parser.add_argument('-h', '--host', default='localhost')
+	parser.add_argument('-p', '--port', default=80, type=int)
+	args = parser.parse_args()
+	return args
 def test_connect():
-	options = get_connect_options()
-	addr =, options.port
+	args = get_connect_options()
+	addr =, args.port
 	family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(*addr)[0]
 	sock = socket.socket(family, socktype, proto)
 	except socket.error as e:
 		raise SystemExit(1)
-	args = vars(options)
-	host, port = addr
-	print(lf("Successfully connected to {host} on port {port}"))
+	print(lf("Successfully connected to {} on port {args.port}"))
 def start_echo_server():
 	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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
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.