diff --git a/refscrub/refscrub.py b/refscrub/refscrub.py index ff6f3014fd83c0d11a258d0647cceef5bf899259..9a1da488f3b5285957d2250bda78390e02a0786a 100644 --- a/refscrub/refscrub.py +++ b/refscrub/refscrub.py @@ -14,11 +14,11 @@ Updates to work on both Python 2 & 3. Code cleanup to match PEP 8. Cleanup global vars. """ -from os.path import join, basename, getsize import sys +import argparse import struct +from os.path import basename, getsize, isfile -USAGE = 'usage: %prog [options] infile1 [ infile2 ... infileN]' PROG_VERSION = '2018.228' VERBOSE = False EXTRACT = False @@ -220,26 +220,40 @@ def main(): global PREFIX global FILESIZE summaryfh = None - from optparse import OptionParser - parser = OptionParser(USAGE, version="%prog " + PROG_VERSION) - parser.description = "infile can be a REFTEK file or a raw disk (/dev/disk1) of a CF card." - parser.add_option('-v', '--verbose', dest="VERBOSE", action='store_true', default=False, - help="Prints info about each packet, good or bad. This will increase runtime.") - parser.add_option('-e', '--extract', dest='EXTRACT', action='store_true', default=False, - help='Writes good packets to files named infile.SNXX.scrub.ref OR PREFIX.SNXX.scrub.ref, ' - ' if given, for each Serial Number found. If output file exists it will append. Be careful ' - 'not to duplicate data by running more than once on the same file in the same dir.') - parser.add_option('-p', '--prefix', dest='PREFIX', - help='Prefix of output filename. Defaults to inputfilename') - parser.add_option('-s', '--savesum', dest='SUMMARY', action='store_true', default=False, - help='Appends summary to %s' % SUMMARY_FILE) - options, args = parser.parse_args() - VERBOSE = options.VERBOSE - EXTRACT = options.EXTRACT - PREFIX = options.PREFIX - if options.SUMMARY: + parser = argparse.ArgumentParser(prog="refscrub", + usage="%(prog)s [options] infile1 " + "[ infile2 ... infileN]") + parser.add_argument('infile', nargs='*', metavar='infile', + help="infile can be a REFTEK file or a raw disk " + "(/dev/disk1) of a CF card.") + parser.add_argument('--version', action='version', + version="%(prog)s " + PROG_VERSION) + parser.add_argument('-v', '--verbose', dest="VERBOSE", action='store_true', + default=False, help="Prints info about each packet, " + "good or bad. This will increase runtime.") + parser.add_argument('-e', '--extract', dest='EXTRACT', action='store_true', + default=False, help="Writes good packets to files " + "named infile.SNXX.scrub.ref OR " + "PREFIX.SNXX.scrub.ref, if given, for each Serial " + "Number found. If output file exists it will append. " + "Be careful not to duplicate data by running more " + "than once on the same file in the same dir.") + parser.add_argument('-p', '--prefix', dest='PREFIX', + help="Prefix of output filename. Defaults to input" + "filename") + parser.add_argument('-s', '--savesum', dest='SUMMARY', action='store_true', + default=False, help='Appends summary to %s' + % SUMMARY_FILE) + args = parser.parse_args() + if not args.infile: + parser.print_help() + sys.exit(1) + VERBOSE = args.VERBOSE + EXTRACT = args.EXTRACT + PREFIX = args.PREFIX + if args.SUMMARY: summaryfh = open(SUMMARY_FILE, 'a') - for infilename in args: + for infilename in args.infile: print("Processing: %s" % infilename) if not PREFIX: PREFIX = basename(infilename) @@ -248,11 +262,13 @@ def main(): # Must open the file in binary mode or else we will have unicode issues when reading. with open(infilename, "rb") as infile: readfile(infile) - if options.SUMMARY: + if args.SUMMARY: summaryfh.write(infilename + ', ' + PREFIX + '\n') summaryfh.write(summary(infile)) print("----------------------------------------") + if isfile(SUMMARY_FILE): + summaryfh.close() if __name__ == '__main__':