Skip to content

Commit

Permalink
Use argparse library for commandline arguments parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Dec 21, 2019
1 parent a3d41f6 commit e1540e6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions PE2HEX.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import sys
import argparse

if len(sys.argv) < 2:
print('Usage: PE2HEX input_filename [output_filename]\nExample: PE2HEX input.dll output.txt')
exit(1)
parser = argparse.ArgumentParser(
description='PE2HEX - PE executable to byte array converter')
parser.add_argument('input', type=str,
help='Input PE filename or full path')
parser.add_argument('--out', default='output.txt', type=str,
help='Output filename or full path')

file = bytearray(open(sys.argv[1], 'rb').read())
args = parser.parse_args()
file = bytearray(open(args.input, 'rb').read())

with open(sys.argv[2] if len(sys.argv) >= 3 else 'output.txt', 'w') as output:
with open(args.out, 'w') as output:
for count, byte in enumerate(file, 1):
output.write(f'{byte:#0{4}x},' + ('\n' if not count % 16 else ' '))

0 comments on commit e1540e6

Please sign in to comment.