git » abk » commit d4d00bb

Improved command-line parser.

author Leandro Lucarella
2005-03-04 19:37:46 UTC
committer Leandro Lucarella
2005-03-04 19:37:46 UTC
parent de728a6a6bd9ee4dba42b902626cf6f84f2b8905

Improved command-line parser.
Added a new command-line parser using optparse. More descriptive error messages.
Command-line options accepted: --copy-mode, --hash-mode, --help, --version
Removed 'config' class and 'printe' function since they're not used any more.

abk +70 -52

diff --git a/abk b/abk
index 0cd91ea..b73578b 100644
--- a/abk
+++ b/abk
@@ -11,23 +11,14 @@ import cPickle
 from stat import *
 
 
-#
-# configuration
-#
-class config:
-	# copy mode; can be "raw", "gzip", "bzip2"
-	copy_mode = 'gzip'
-
-	# hash mode; can be "none", "md5", "sha"
-	hash_mode = "sha"
-
-
 #
 # constants
 #
 
 PSIZE = 4 * 1024
 
+VERSION = "0.03"
+
 
 #
 # classes
@@ -434,75 +425,102 @@ def build_idx(idx_path, path):
 
 
 #
-# main
+# helper functions
 #
-help = """abk - A backup script
-Alberto Bertogli (albertogli@telpin.com.ar)
 
-Use: abk command params
+def parse_options():
+	"Commandline options parser."
+	from optparse import OptionParser
+	class AbkOptionParser(OptionParser):
+		"Custom abk command line option parser."
+		def format_help (self, formatter=None):
+			"Displays the description before usage."
+			if formatter is None:
+				formatter = self.formatter
+			result = []
+			if self.description:
+				result.append(self._get_prog_name() + ' - ' + \
+					self.format_description(formatter)+"\n\n")
+			if self.usage:
+				result.append(self.get_usage() + "\n")
+			result.append(self.format_option_help(formatter))
+			return "".join(result)
+	usage = """%prog [options] command params
+
+commands:
+  show idx_file
+    shows the given index file contents
+  mkidx idx_file dir
+    builds an index file for the given directory
+  sync src src.idx dst dst.idx
+    synchronizes src with dst, using the given index files"""
+	parser = AbkOptionParser(usage=usage, description="A backup script - "
+		"Alberto Bertogli (albertogli@telpin.com.ar)",
+		version="%prog " + VERSION, prog='abk')
+	parser.add_option("-c", "--copy-mode", default='gzip', metavar="MODE",
+		help="select copy mode to use. Available modes: "
+		"raw, gzip, bzip2 [default: gzip]")
+	parser.add_option("-a", "--hash-mode", default='sha', metavar="MODE",
+		help="select the hash to use to check for file content change. "
+		"Available modes: none, sha, md5 [default: sha]")
+	(opts, args) = parser.parse_args()
+	return (parser, opts, args)
 
-Commands:
-	show idx_file
-		Shows the given index file contents.
-	mkidx idx_file dir
-		Builds an index file for the given directory.
-	sync src src.idx dst dst.idx
-		Synchronizes src with dst, using the given index files.
-"""
+
+#
+# main
+#
+
+# command line options
+(parser, opts, args) = parse_options()
 
 # configuration
-if config.copy_mode == 'raw':
+if opts.copy_mode == 'raw':
 	file_info.copy_file_reg = file_info.copy_file_reg_raw
-elif config.copy_mode == 'gzip':
+elif opts.copy_mode == 'gzip':
 	file_info.copy_file_reg = file_info.copy_file_reg_gzip
-elif config.copy_mode == 'bzip2':
+elif opts.copy_mode == 'bzip2':
 	file_info.copy_file_reg = file_info.copy_file_reg_bzip2
 else:
-	print "Configuration error"
-	sys.exit(1)
+	parser.error("Invalid copy mode (%s)." % opts.copy_mode)
 
-if config.hash_mode == 'none':
+if opts.hash_mode == 'none':
 	file_info.hash_file = file_info.hash_file_none
-elif config.hash_mode == 'md5':
+elif opts.hash_mode == 'md5':
 	file_info.hash_file = file_info.hash_file_md5
-elif config.hash_mode == 'sha':
+elif opts.hash_mode == 'sha':
 	file_info.hash_file = file_info.hash_file_sha
 else:
-	print "Configuration error"
-	sys.exit(1)
+	parser.error("Invalid hash mode (%s)." % opts.hash_mode)
 
 # main command
 try:
-	cmd = sys.argv[1]
+	cmd = args[0]
 except:
-	print help
-	sys.exit(1)
+	parser.error("Command missing.")
 
 if cmd == 'show':
-	if len(sys.argv) < 2:
-		print "Incorrect or missing parameters"
-		sys.exit(1)
-	show_idx(sys.argv[2])
+	try:
+		show_idx(args[1])
+	except:
+		parser.error("Missing idx_file parameter.")
 elif cmd == 'mkidx':
 	try:
-		idx_path = sys.argv[2]
-		path = sys.argv[3]
+		idx_path = args[1]
+		path = args[2]
 	except:
-		print "Incorrect or missing parameters"
-		sys.exit(1)
+		parser.error("Missing parameter(s) for command mkidx.")
 	build_idx(idx_path, path)
 elif cmd == 'sync':
 	try:
-		src_path = sys.argv[2]
-		srcidx_path = sys.argv[3]
-		dst_path = sys.argv[4]
-		dstidx_path = sys.argv[5]
+		src_path = args[1]
+		srcidx_path = args[2]
+		dst_path = args[3]
+		dstidx_path = args[4]
 	except:
-		print "Incorrect or missing parameters"
-		sys.exit(1)
+		parser.error("Missing parameter(s) for command sync.")
 	
 	make_sync(src_path, srcidx_path, dst_path, dstidx_path)
 else:
-	print "Unknown command"
-	sys.exit(1)
+	parser.error("Unknown command (%s)." % cmd)