From 3c9db52fd70a6697de7b58fc353fe8348d822845 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Mon, 8 Apr 2019 12:04:40 -0700 Subject: [PATCH] Stack: Replace getopt with argparse Modernize and make it easier to add new options. Test: m Test: manual Change-Id: Ib7b1de4626e6b2cd27dca1dd911c594db93cb292 --- scripts/stack | 54 +++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/scripts/stack b/scripts/stack index 8e65dba61..3c447b750 100755 --- a/scripts/stack +++ b/scripts/stack @@ -16,55 +16,33 @@ """stack symbolizes native crash dumps.""" -import getopt +import argparse import sys import stack_core import symbol - -def PrintUsage(): - """Print usage and exit with error.""" - # pylint: disable-msg=C6310 - print - print " usage: " + sys.argv[0] + " [options] [FILE]" - print - print " --arch=arm|arm64|mips|mips64|x86|x86_64" - print " the target architecture" - print - print " FILE should contain a stack trace in it somewhere" - print " the tool will find that and re-print it with" - print " source files and line numbers. If you don't" - print " pass FILE, or if file is -, it reads from" - print " stdin." - print - # pylint: enable-msg=C6310 - sys.exit(1) - - def main(): - try: - options, arguments = getopt.getopt(sys.argv[1:], "", - ["arch=", - "help"]) - except getopt.GetoptError, unused_error: - PrintUsage() + parser = argparse.ArgumentParser(description='Parse and symbolize crashes') + parser.add_argument('--arch', help='the target architecture') + parser.add_argument('file', + metavar='FILE', + default='-', + help='should contain a stack trace in it somewhere the ' + 'tool will find that and re-print it with source ' + 'files and line numbers. If you don\'t pass FILE, ' + 'or if file is -, it reads from stdin.') - for option, value in options: - if option == "--help": - PrintUsage() - elif option == "--arch": - symbol.ARCH = value + args = parser.parse_args() - if len(arguments) > 1: - PrintUsage() - - if not arguments or arguments[0] == "-": + if args.arch: + symbol.ARCH = args.arch + if args.file == '-': print "Reading native crash info from stdin" f = sys.stdin else: - print "Searching for native crashes in %s" % arguments[0] - f = open(arguments[0], "r") + print "Searching for native crashes in %s" % args.file + f = open(args.file, "r") lines = f.readlines() f.close()