Commit 3f009e7647f335dfafa1ed1da6c7a9ec6e09ee7b

Authored by Christian Herdtweck
1 parent 471b141f

oleobj: upgrade from optparse to argparse

Showing 1 changed file with 43 additions and 38 deletions
oletools/oleobj.py
@@ -46,7 +46,7 @@ from __future__ import print_function @@ -46,7 +46,7 @@ from __future__ import print_function
46 46
47 import logging 47 import logging
48 import struct 48 import struct
49 -import optparse 49 +import argparse
50 import os 50 import os
51 import re 51 import re
52 import sys 52 import sys
@@ -660,6 +660,13 @@ def process_file(filename, data, output_dir=None): @@ -660,6 +660,13 @@ def process_file(filename, data, output_dir=None):
660 # === MAIN ==================================================================== 660 # === MAIN ====================================================================
661 661
662 662
  663 +def existing_file(filename):
  664 + """ called by argument parser to see whether given file exists """
  665 + if not os.path.isfile(filename):
  666 + raise argparse.ArgumentTypeError('{0} is not a file.'.format(filename))
  667 + return filename
  668 +
  669 +
663 def main(): 670 def main():
664 """ main function, called when running this as script """ 671 """ main function, called when running this as script """
665 # print banner with version 672 # print banner with version
@@ -669,53 +676,51 @@ def main(): @@ -669,53 +676,51 @@ def main():
669 'https://github.com/decalage2/oletools/issues') 676 'https://github.com/decalage2/oletools/issues')
670 print('') 677 print('')
671 678
672 - usage = 'usage: %prog [options] <filename> [filename2 ...]'  
673 - parser = optparse.OptionParser(usage=usage)  
674 - # parser.add_option('-o', '--outfile', dest='outfile', 679 + usage = 'usage: %(prog)s [options] <filename> [filename2 ...]'
  680 + parser = argparse.ArgumentParser(usage=usage)
  681 + # parser.add_argument('-o', '--outfile', dest='outfile',
675 # help='output file') 682 # help='output file')
676 - # parser.add_option('-c', '--csv', dest='csv', 683 + # parser.add_argument('-c', '--csv', dest='csv',
677 # help='export results to a CSV file') 684 # help='export results to a CSV file')
678 - parser.add_option("-r", action="store_true", dest="recursive",  
679 - help='find files recursively in subdirectories.')  
680 - parser.add_option("-d", type="str", dest="output_dir", default=None,  
681 - help='use specified directory to output files.')  
682 - parser.add_option("-z", "--zip", dest='zip_password', type='str',  
683 - default=None,  
684 - help='if the file is a zip archive, open first file from'  
685 - 'it, using the provided password (requires Python '  
686 - '2.6+)')  
687 - parser.add_option("-f", "--zipfname", dest='zip_fname', type='str',  
688 - default='*',  
689 - help='if the file is a zip archive, file(s) to be opened'  
690 - 'within the zip. Wildcards * and ? are supported. '  
691 - '(default:*)')  
692 - parser.add_option('-l', '--loglevel', dest="loglevel", action="store",  
693 - default=DEFAULT_LOG_LEVEL,  
694 - help='logging level debug/info/warning/error/critical '  
695 - '(default=%default)') 685 + parser.add_argument("-r", action="store_true", dest="recursive",
  686 + help='find files recursively in subdirectories.')
  687 + parser.add_argument("-d", type=str, dest="output_dir", default=None,
  688 + help='use specified directory to output files.')
  689 + parser.add_argument("-z", "--zip", dest='zip_password', type=str,
  690 + default=None,
  691 + help='if the file is a zip archive, open first file '
  692 + 'from it, using the provided password (requires '
  693 + 'Python 2.6+)')
  694 + parser.add_argument("-f", "--zipfname", dest='zip_fname', type=str,
  695 + default='*',
  696 + help='if the file is a zip archive, file(s) to be '
  697 + 'opened within the zip. Wildcards * and ? are '
  698 + 'supported. (default:*)')
  699 + parser.add_argument('-l', '--loglevel', dest="loglevel", action="store",
  700 + default=DEFAULT_LOG_LEVEL,
  701 + help='logging level debug/info/warning/error/critical '
  702 + '(default=%(default)s)')
  703 + parser.add_argument('input', nargs='*', type=existing_file, metavar='FILE',
  704 + help='Office files to parse (same as -i)')
696 705
697 # options for compatibility with ripOLE 706 # options for compatibility with ripOLE
698 - parser.add_option('-i', '--more-input', type='str', default=None,  
699 - help='Additional file to parse (same as positional '  
700 - 'arguments)')  
701 - parser.add_option('-v', '--verbose', action='store_true',  
702 - help='verbose mode, set logging to DEBUG '  
703 - '(overwrites -l)')  
704 -  
705 - (options, args) = parser.parse_args() 707 + parser.add_argument('-i', '--more-input', type=str, metavar='FILE',
  708 + help='Additional file to parse (same as positional '
  709 + 'arguments)')
  710 + parser.add_argument('-v', '--verbose', action='store_true',
  711 + help='verbose mode, set logging to DEBUG '
  712 + '(overwrites -l)')
  713 +
  714 + options = parser.parse_args()
706 if options.more_input: 715 if options.more_input:
707 - args += [options.more_input, ] 716 + options.input += [options.more_input, ]
708 if options.verbose: 717 if options.verbose:
709 options.loglevel = 'debug' 718 options.loglevel = 'debug'
710 719
711 # Print help if no arguments are passed 720 # Print help if no arguments are passed
712 - if not args: 721 + if not options.input:
713 parser.print_help() 722 parser.print_help()
714 return RETURN_ERR_ARGS 723 return RETURN_ERR_ARGS
715 - for filename in args:  
716 - if not os.path.isfile(filename):  
717 - print('File does not exist: ' + filename)  
718 - return RETURN_ERR_ARGS  
719 724
720 # Setup logging to the console: 725 # Setup logging to the console:
721 # here we use stdout instead of stderr by default, so that the output 726 # here we use stdout instead of stderr by default, so that the output
@@ -731,7 +736,7 @@ def main(): @@ -731,7 +736,7 @@ def main():
731 any_did_dump = False 736 any_did_dump = False
732 737
733 for container, filename, data in \ 738 for container, filename, data in \
734 - xglob.iter_files(args, recursive=options.recursive, 739 + xglob.iter_files(options.input, recursive=options.recursive,
735 zip_password=options.zip_password, 740 zip_password=options.zip_password,
736 zip_fname=options.zip_fname): 741 zip_fname=options.zip_fname):
737 # ignore directory names stored in zip files: 742 # ignore directory names stored in zip files: