From 77cb3e3bfbe5a219775a467f3ec26a1e5c5ddc8c Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Fri, 27 Oct 2017 14:55:09 +0200 Subject: [PATCH] olevba[3]: move arg-parsing to own function with optional custom args --- oletools/olevba.py | 27 +++++++++++++++++++++------ oletools/olevba3.py | 27 +++++++++++++++++++++------ 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/oletools/olevba.py b/oletools/olevba.py index 67d42ca..456edd2 100644 --- a/oletools/olevba.py +++ b/oletools/olevba.py @@ -3269,10 +3269,9 @@ class VBA_Parser_CLI(VBA_Parser): #=== MAIN ===================================================================== -def main(): - """ - Main function, called when olevba is run from the command line - """ +def parse_args(cmd_line_args=None): + """ parse command line arguments (given ones or per default sys.argv) """ + DEFAULT_LOG_LEVEL = "warning" # Default log level LOG_LEVELS = { 'debug': logging.DEBUG, @@ -3324,7 +3323,7 @@ def main(): parser.add_option('--relaxed', dest="relaxed", action="store_true", default=False, help="Do not raise errors if opening of substream fails") - (options, args) = parser.parse_args() + (options, args) = parser.parse_args(cmd_line_args) # Print help if no arguments are passed if len(args) == 0: @@ -3333,6 +3332,22 @@ def main(): parser.print_help() sys.exit(RETURN_WRONG_ARGS) + options.loglevel = LOG_LEVELS[options.loglevel] + + return options, args + + +def main(cmd_line_args=None): + """ + Main function, called when olevba is run from the command line + + Optional argument: command line arguments to be forwarded to ArgumentParser + in process_args. Per default (cmd_line_args=None), sys.argv is used. Option + mainly added for unit-testing + """ + + options, args = parse_args(cmd_line_args) + # provide info about tool and its version if options.output_mode == 'json': # prints opening [ @@ -3342,7 +3357,7 @@ def main(): else: print('olevba %s - http://decalage.info/python/oletools' % __version__) - logging.basicConfig(level=LOG_LEVELS[options.loglevel], format='%(levelname)-8s %(message)s') + logging.basicConfig(level=options.loglevel, format='%(levelname)-8s %(message)s') # enable logging in the modules: enable_logging() diff --git a/oletools/olevba3.py b/oletools/olevba3.py index 802b3c3..7d199f1 100644 --- a/oletools/olevba3.py +++ b/oletools/olevba3.py @@ -3232,10 +3232,9 @@ class VBA_Parser_CLI(VBA_Parser): #=== MAIN ===================================================================== -def main(): - """ - Main function, called when olevba is run from the command line - """ +def parse_args(cmd_line_args=None): + """ parse command line arguments (given ones or per default sys.argv) """ + DEFAULT_LOG_LEVEL = "warning" # Default log level LOG_LEVELS = { 'debug': logging.DEBUG, @@ -3287,7 +3286,7 @@ def main(): parser.add_option('--relaxed', dest="relaxed", action="store_true", default=False, help="Do not raise errors if opening of substream fails") - (options, args) = parser.parse_args() + (options, args) = parser.parse_args(cmd_line_args) # Print help if no arguments are passed if len(args) == 0: @@ -3295,6 +3294,22 @@ def main(): parser.print_help() sys.exit(RETURN_WRONG_ARGS) + options.loglevel = LOG_LEVELS[options.loglevel] + + return options, args + + +def main(cmd_line_args=None): + """ + Main function, called when olevba is run from the command line + + Optional argument: command line arguments to be forwarded to ArgumentParser + in process_args. Per default (cmd_line_args=None), sys.argv is used. Option + mainly added for unit-testing + """ + + options, args = parse_args(cmd_line_args) + # provide info about tool and its version if options.output_mode == 'json': # prints opening [ @@ -3304,7 +3319,7 @@ def main(): else: print('olevba %s - http://decalage.info/python/oletools' % __version__) - logging.basicConfig(level=LOG_LEVELS[options.loglevel], format='%(levelname)-8s %(message)s') + logging.basicConfig(level=options.loglevel, format='%(levelname)-8s %(message)s') # enable logging in the modules: log.setLevel(logging.NOTSET) -- libgit2 0.21.4