
import sys
import getopt
import Convert


def usage():
	print "Usage: to be done."

def main(argv):

	try:
		opts, args = getopt.getopt(argv, "hb:d", ["help", "book="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit(2)
		elif opt == "-d":
			global _debug
			_debug = 1
		elif opt in ("-b","--book"):
			filePath = arg

	try:
		content = None
		with open(filePath,'r') as file:
			content = file.read().decode("utf-8")
		content = Convert.Convert(content)
		with open(filePath,'w') as file:
			file.write(content.encode("utf-8"))
	except IOError:
		print ("IOError occurs while handling the file (" + filePath + ").")

if __name__ == '__main__':
	main(sys.argv[1:])

import sys
import argparse
import Convert

def main():
	parser = argparse.ArgumentParser(description='Generate a classic book with the desired format.')
	parser.add_argument('book', type=str, help='a book file')
	args = parser.parse_args()
	filePath = args.book
	try:
		content = None
		with open(filePath,'r') as file:
			content = file.read().decode("utf-8")
		content = Convert.Convert(content)
		with open(filePath,'w') as file:
			file.write(content.encode("utf-8"))
	except IOError:
		print ("IOError occurs while handling the file (" + filePath + ").")

if __name__ == '__main__':
	main()

Use argparse (instead of getopt) to get the usage information
Use argparse (instead of getopt) to get the usage information

  
  import sys
- import getopt
+ import argparse
  import Convert
  
- 
- def usage():
- 	print "Usage: to be done."
- 
- def main(argv):
+ def main():
+ 	parser = argparse.ArgumentParser(description='Generate a classic book with the desired format.')
+ 	parser.add_argument('book', type=str, help='a book file')
+ 	args = parser.parse_args()
- 
- 	try:
- 		opts, args = getopt.getopt(argv, "hb:d", ["help", "book="])
- 	except getopt.GetoptError:
- 		usage()
- 		sys.exit(2)
- 	for opt, arg in opts:
- 		if opt in ("-h", "--help"):
- 			usage()
- 			sys.exit(2)
- 		elif opt == "-d":
- 			global _debug
- 			_debug = 1
- 		elif opt in ("-b","--book"):
- 			filePath = arg
+ 	filePath = args.book
- 
  	try:
  		content = None
  		with open(filePath,'r') as file:
  			content = file.read().decode("utf-8")
  		content = Convert.Convert(content)
  		with open(filePath,'w') as file:
  			file.write(content.encode("utf-8"))
  	except IOError:
  		print ("IOError occurs while handling the file (" + filePath + ").")
  
  if __name__ == '__main__':
- 	main(sys.argv[1:])
+ 	main()
+ 
Use argparse (instead of getopt) to get the usage information
## Code Before:

import sys
import getopt
import Convert


def usage():
	print "Usage: to be done."

def main(argv):

	try:
		opts, args = getopt.getopt(argv, "hb:d", ["help", "book="])
	except getopt.GetoptError:
		usage()
		sys.exit(2)
	for opt, arg in opts:
		if opt in ("-h", "--help"):
			usage()
			sys.exit(2)
		elif opt == "-d":
			global _debug
			_debug = 1
		elif opt in ("-b","--book"):
			filePath = arg

	try:
		content = None
		with open(filePath,'r') as file:
			content = file.read().decode("utf-8")
		content = Convert.Convert(content)
		with open(filePath,'w') as file:
			file.write(content.encode("utf-8"))
	except IOError:
		print ("IOError occurs while handling the file (" + filePath + ").")

if __name__ == '__main__':
	main(sys.argv[1:])
## Instruction:
Use argparse (instead of getopt) to get the usage information
## Code After:

import sys
import argparse
import Convert

def main():
	parser = argparse.ArgumentParser(description='Generate a classic book with the desired format.')
	parser.add_argument('book', type=str, help='a book file')
	args = parser.parse_args()
	filePath = args.book
	try:
		content = None
		with open(filePath,'r') as file:
			content = file.read().decode("utf-8")
		content = Convert.Convert(content)
		with open(filePath,'w') as file:
			file.write(content.encode("utf-8"))
	except IOError:
		print ("IOError occurs while handling the file (" + filePath + ").")

if __name__ == '__main__':
	main()
