Factorize
  • Overview
  • Repository
  • Tickets
  • Statistics
  • Projects

Repository

Fix option handling

Parent commits : 55f2f4c36a5cc5dfaa27815ee7205a89b67e2310,
Children commits : f2f4643e07c5824dec42ec4b9decb892c81e6ed4,

By lds on 2010-03-23 21:49:02
Fix option handling


git-svn-id: http://shan/svn/repos/factorize@18 51b56bfc-b269-4856-8701-af1c38ee6d62

Browse content
Difference with parent commit 55f2f4c36a5cc5dfaa27815ee7205a89b67e2310
Files modified:
factorize.py
--- 
+++ 
@@ -5,8 +5,6 @@
 import gui
 from optparse import OptionParser
 from copy import copy
-
-opt = None
 
 class FactorizedLine:
 	def __init__(s, parent = None):
@@ -173,11 +171,13 @@
 		return False
 
 class LogLine:
-	def __init__(s, txt):
+	def __init__(s, txt, opt):
+		#print "skipp", opt.skipped_fields
 		s.full_line = txt.strip('\r\n')
 		s.words = [Word(a.strip('\r\n')) for a in txt.split()]
 		s.date = [a.word for a in s.words[0:int(opt.skipped_fields)]]
 		s.words = s.words[int(opt.skipped_fields):]
+
 
 class Word:
 	def __init__(s, word):
@@ -192,33 +192,37 @@
 		return True
 
 class Log:
-	def __init__(s, file):
+	def __init__(s, file, opt):
 		s.f = open(file, 'r')
 		s.tree = FactorizedLine()
 		s.line_count = 0
+		s.opt = opt
 		s.lines = []
 
 		while 1:
 			line = s.f.readline()
 			if not line:
 				break
-			line = LogLine(line)
+			line = LogLine(line, s.opt)
 			s.tree.add_line(line)
 			s.line_count += 1
 			s.lines.append(line)
-	
+
 		s.tree.build()
 
 if __name__ == "__main__":
-	filename = sys.argv[-1]
-	sys.argv = sys.argv[:len(sys.argv) - 2]
-
-	opt_parser = OptionParser()
-	opt_parser.add_option("-s", "--skip", help="skip the first X field of each line",
+	opt_parser = OptionParser(usage="usage: %prog [options] file_to_view")
+	opt_parser.add_option("-s", "--skip", help="skip the first X fields of each line",
 			dest="skipped_fields", metavar="X")
 	(opt, args) = opt_parser.parse_args()
 	
 	if opt.skipped_fields is None:
-		opt.skipped_fields = "3"
-	log = Log(filename)
+		opt.skipped_fields = 3
+	else:
+		opt.skipped_fields = int(opt.skipped_fields)
+
+	if len(args) == 0:
+		opt_parser.print_help()
+		exit(1)
+	log = Log(args[0], opt)
 	gui.start(log, opt)

Generated with KisssPM