Commit 8f9df350de196ee991f38ccc7399ebe2dc86ace2

Authored by Christian Herdtweck
1 parent 66bc3f34

Show banner in error case

Also correct banner and use the argparse-way to test for file existence
Showing 1 changed file with 22 additions and 10 deletions
oletools/msodde.py
... ... @@ -93,8 +93,7 @@ TAG_W_FLDSIMPLE = '{%s}fldSimple' % NS_WORD
93 93 TAG_W_INSTRATTR= '{%s}instr' % NS_WORD
94 94  
95 95 # banner to be printed at program start
96   -BANNER = """
97   -msodde %s - http://decalage.info/python/oletools
  96 +BANNER = """msodde %s - http://decalage.info/python/oletools
98 97 THIS IS WORK IN PROGRESS - Check updates regularly!
99 98 Please report any issue at https://github.com/decalage2/oletools/issues
100 99 """ % __version__
... ... @@ -105,21 +104,34 @@ BANNER_JSON = dict(type='meta', version=__version__, name='msodde',
105 104 'Please report any issue at '
106 105 'https://github.com/decalage2/oletools/issues')
107 106  
108   -# === FUNCTIONS ==============================================================
  107 +# === ARGUMENT PARSING =======================================================
  108 +
  109 +class ArgParserWithBanner(argparse.ArgumentParser):
  110 + """ Print banner before showing any error """
  111 + def error(self, message):
  112 + print(BANNER)
  113 + super(ArgParserWithBanner, self).error(message)
  114 +
  115 +
  116 +def existing_file(filename):
  117 + """ called by argument parser to see whether given file exists """
  118 + if not os.path.exists(filename):
  119 + raise argparse.ArgumentTypeError('File {0} does not exist.'
  120 + .format(filename))
  121 + return filename
  122 +
109 123  
110 124 def process_args():
111   - parser = argparse.ArgumentParser(description='A python tool to detect and extract DDE links in MS Office files')
112   - parser.add_argument("filepath", help="path of the file to be analyzed")
  125 + parser = ArgParserWithBanner(description='A python tool to detect and extract DDE links in MS Office files')
  126 + parser.add_argument("filepath", help="path of the file to be analyzed",
  127 + type=existing_file, metavar='FILE')
113 128 parser.add_argument("--json", '-j', action='store_true',
114 129 help="Output in json format")
115 130  
116   - args = parser.parse_args()
  131 + return parser.parse_args()
117 132  
118   - if not os.path.exists(args.filepath):
119   - print('File {} does not exist.'.format(args.filepath))
120   - sys.exit(1)
121 133  
122   - return args
  134 +# === FUNCTIONS ==============================================================
123 135  
124 136 # from [MS-DOC], section 2.8.25 (PlcFld):
125 137 # A field consists of two parts: field instructions and, optionally, a result. All fields MUST begin with
... ...