From 7437e47ea4f6c1dea56b8b325c759518507b439b Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 3 May 2018 18:52:02 +0200 Subject: [PATCH] add a proper argument parser --- z64dump.py | 61 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/z64dump.py b/z64dump.py index bf30c72..8a32d75 100755 --- a/z64dump.py +++ b/z64dump.py @@ -340,39 +340,48 @@ def create_rom(d, compression=None, nocomplist=None): fix_rom(f) def run(args): - decompress = True - compression = None + import argparse + + parser = argparse.ArgumentParser( + description="z64dump: construct and deconstruct Zelda N64 ROMs") + + parser.add_argument( + 'path', metavar='ROM or folder', nargs='+', + help="ROM to deconstruct, or folder to construct") + parser.add_argument( + '-f', '--fix', action='store_true', + help="only update CIC checksums") + parser.add_argument( + '-D', '--no-decompress', action='store_true', + help="(deconstruction) leave compressed files compressed") + parser.add_argument( + '-C', '--no-compress', action='store_const', const=None, dest='compression', + help="(construction) do not compress files") + parser.add_argument( + '-y', '--yaz', action='store_const', const='yaz', dest='compression', + help="(construction) use yaz (Yaz0) compression") + parser.add_argument( + '-z', '--zlib', action='store_const', const='zlib', dest='compression', + help="(construction) use deflate (zlib) compression") + + a = parser.parse_args(args) + nocomplist = None # file indices to skip compression on - fix = False - for arg in args: - if arg == '-f': - fix = not fix - continue - if arg == '-c': - decompress = not decompress - continue - if arg == '-n': - compression = None - continue - if arg == '-y': - compression = 'yaz' - continue - if arg == '-z': - compression = 'zlib' - continue - - path = arg - if fix: - with open(path, 'r+b') as f: - fix_rom(f) + for path in a.path: + if a.fix: + if os.path.isdir(path): + lament("only ROM files are fixable:", path) + else: + with open(path, 'r+b') as f: + fix_rom(f) continue # directories are technically files, so check this first if os.path.isdir(path): - create_rom(path, compression, nocomplist) + create_rom(path, a.compression, nocomplist) elif os.path.isfile(path): - dump_rom(path, decompress) + dump_rom(path, not a.no_decompress) else: lament('no-op:', path)