From 9bb2a4ec6f32704746863b538f503683e7eab22f Mon Sep 17 00:00:00 2001 From: jvoisin Date: Wed, 11 Jun 2014 09:01:24 +0200 Subject: Port mat from optparse to argparse --- mat | 70 ++++++++++++++++++++++++++++++--------------------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/mat b/mat index 03a7367..18dd456 100755 --- a/mat +++ b/mat @@ -5,7 +5,7 @@ import sys import xml.sax -import optparse +import argparse import os import hachoir_core @@ -18,41 +18,28 @@ from libmat import archive def parse(): ''' Get, and parse options passed to the program ''' - parser = optparse.OptionParser(usage='%prog [options] files\n\ -The default behaviour is to clean files given in argument') - options = optparse.OptionGroup(parser, 'Options') - options.add_option('--add2archive', '-a', action='store_true', - default=False, help='Add to output archive non-supported filetypes (Off by default)') - options.add_option('--backup', '-b', action='store_true', default=False, - help='Keep a backup copy') - options.add_option('--low-pdf-quality', '-L', action='store_true', default=False, - help='Produces a lighter, but lower quality PDF') - info = optparse.OptionGroup(parser, 'Informations') - info.add_option('--check', '-c', action='store_true', default=False, - help='Check if a file is free of harmful metadatas') - info.add_option('--display', '-d', action='store_true', default=False, - help='List all the harmful metadata of a file without removing them') - info.add_option('--list', '-l', action='store_true', default=False, - help='List all supported fileformats') - info.add_option('--version', '-v', action='callback', - callback=display_version, help='Display version and exit') - parser.add_option_group(options) - parser.add_option_group(info) - - values, arguments = parser.parse_args() - if not arguments and not values.list: - # if no argument and no files are passed, - # print help and exit - parser.print_help() - return values, arguments - - -def display_version(*_): - ''' Display the program's version, and exit - ''' - print('Metadata Anonymisation Toolkit %s' % mat.__version__) - print('Hachoir %s' % hachoir_core.__version__) - sys.exit(0) + parser = argparse.ArgumentParser(description='Metadata anonymisation toolkit') + parser.add_argument('files', nargs='*') + + options = parser.add_argument_group('Options') + options.add_argument('-a', '--add2archive', action='store_true', + help='add to output archive non-supported filetypes (Off by default)') + options.add_argument('-b', '--backup', '-b', action='store_true', + help='keep a backup copy') + options.add_argument('-L', '--low-pdf-quality', '-L', action='store_true', + help='produces a lighter, but lower quality PDF') + + info = parser.add_argument_group('Information') + info.add_argument('-c', '--check', action='store_true', + help='check if a file is free of harmful metadatas') + info.add_argument('-d', '--display', action='store_true', + help='list all the harmful metadata of a file without removing them') + info.add_argument('-l', '--list', action='store_true', + help='list all supported fileformats') + info.add_argument('-v', '--version', action='version', + version='MAT %s - Hachoir %s' % (mat.__version__, hachoir_core.__version__)) + + return parser.parse_args() def list_meta(class_file, filename, add2archive): @@ -118,7 +105,7 @@ def list_supported(): def main(): ''' Main function: get args and launch the appropriate function ''' - args, filenames = parse() + args = parse() #func receives the function corresponding to the options given as parameters if args.display: # only print metadatas @@ -131,12 +118,15 @@ def main(): func = clean_meta ret = 0 - while filenames: - filename = filenames.pop() + # We're using a while loop, instead of a for, + # because we support folders. This allow us + # to add their content, and to process it. + while args.files: + filename = args.files.pop() if os.path.isdir(filename): for root, sub, files in os.walk(filename): for file in files: - filenames.append(os.path.join(root, file)) + args.files.append(os.path.join(root, file)) continue class_file = mat.create_class_file(filename, args.backup, -- cgit v1.3