Stack: Replace getopt with argparse

Modernize and make it easier to add new options.

Test: m
Test: manual
Change-Id: Ib7b1de4626e6b2cd27dca1dd911c594db93cb292
This commit is contained in:
Andreas Gampe
2019-04-08 12:04:40 -07:00
parent bb57864370
commit 3c9db52fd7

View File

@@ -16,55 +16,33 @@
"""stack symbolizes native crash dumps.""" """stack symbolizes native crash dumps."""
import getopt import argparse
import sys import sys
import stack_core import stack_core
import symbol 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(): def main():
try: parser = argparse.ArgumentParser(description='Parse and symbolize crashes')
options, arguments = getopt.getopt(sys.argv[1:], "", parser.add_argument('--arch', help='the target architecture')
["arch=", parser.add_argument('file',
"help"]) metavar='FILE',
except getopt.GetoptError, unused_error: default='-',
PrintUsage() 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: args = parser.parse_args()
if option == "--help":
PrintUsage()
elif option == "--arch":
symbol.ARCH = value
if len(arguments) > 1: if args.arch:
PrintUsage() symbol.ARCH = args.arch
if args.file == '-':
if not arguments or arguments[0] == "-":
print "Reading native crash info from stdin" print "Reading native crash info from stdin"
f = sys.stdin f = sys.stdin
else: else:
print "Searching for native crashes in %s" % arguments[0] print "Searching for native crashes in %s" % args.file
f = open(arguments[0], "r") f = open(args.file, "r")
lines = f.readlines() lines = f.readlines()
f.close() f.close()