From 8de6d03c4417a16cad2d089805f93da4b4160602 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Tue, 15 Jun 2010 01:37:14 -0400 Subject: [PATCH] Use optparse to parse gtester-report commandline options See bug #611778. --- glib/gtester-report | 54 +++++++++++++++++++++++------------------------------ 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/glib/gtester-report b/glib/gtester-report index c2162aa..52b9dcb 100755 --- a/glib/gtester-report +++ b/glib/gtester-report @@ -17,6 +17,7 @@ # License along with this library; if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. +import optparse import sys, re, xml.dom.minidom pkginstall_configvars = { #@PKGINSTALL_CONFIGVARS_IN24LINES@ # configvars are substituted upon script installation @@ -331,40 +332,31 @@ class ReportWriter (TreeProcess): self.oprint ('\n') # main program handling -def parse_files_and_args (): - from sys import argv, stdin - files = [] - arg_iter = sys.argv[1:].__iter__() - rest = len (sys.argv) - 1 - for arg in arg_iter: - rest -= 1 - if arg == '--help' or arg == '-h': - print_help () - sys.exit (0) - elif arg == '--version' or arg == '-v': - print_help (False) - sys.exit (0) - else: - files = files + [ arg ] - return files +def parse_opts(): + """Parse program options. + + :return: An options object and the program arguments. + """ + parser = optparse.OptionParser() + parser.version = pkginstall_configvars.get ('glib-version', '0.0-uninstalled') + parser.usage = "%prog [OPTIONS] " + parser.description = "Generate HTML reports from the XML log files generated by gtester." + parser.epilog = "gtester-report (GLib utils) version %s."% (parser.version,) + parser.add_option("-v", "--version", action="store_true", dest="version", default=False, + help="Show program version.") + options, files = parser.parse_args() + if options.version: + print parser.epilog + return None, None + if len(files) != 1: + parser.error("Must supply a log file to parse.") + return options, files -def print_help (with_help = True): - import os - print "gtester-report (GLib utils) version", pkginstall_configvars.get ('glib-version', '0.0-uninstalled') - if not with_help: - return - print "Usage: %s [OPTIONS] " % os.path.basename (sys.argv[0]) - print "Generate HTML reports from the XML log files generated by gtester." - print "Options:" - print " --help, -h print this help message" - print " --version, -v print version info" def main(): - from sys import argv, stdin - files = parse_files_and_args() - if len (files) != 1: - print_help (True) - sys.exit (1) + options, files = parse_opts() + if options is None: + return 0 xd = xml.dom.minidom.parse (files[0]) rr = ReportReader() rr.trampoline (xd) -- 2.7.4