diff --git a/Lib/extractor/__init__.py b/Lib/extractor/__init__.py index 9dec481..2d7451f 100644 --- a/Lib/extractor/__init__.py +++ b/Lib/extractor/__init__.py @@ -84,28 +84,57 @@ def cmdline(): Usage: extractufo font [font ...] """ - from sys import argv, exit - try: - from ufoLib2 import Font - library = "ufoLib2" - except ImportError: + from sys import exit + from argparse import ArgumentParser + + parser = ArgumentParser( + description="Extract data from font binaries and build UFO objects from them.", + epilog="Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE.", + ) + parser.add_argument('FONT_FILE', help='Input font path', nargs="+") + parser.add_argument('-m', '--ufo-module', choices=['ufoLib2', 'defcon'], help='Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)') + parser.add_argument('-z', '--zip', action="store_true", help="Output UFO ZIP") + parser.add_argument('-o', '--overwrite', action="store_true", help="Overwrite output location if exists") + + args = parser.parse_args() + if args.ufo_module is None: + try: + from ufoLib2 import Font + print("Will use ufoLib2 for UFO output.") + except ImportError: + try: + from defcon import Font + print("Will use defcon for UFO output.") + except ImportError: + print("Either ufoLib2 or, alternatively, defcon library is required to run this command.\nPlease install one of them.") + exit(1) + elif args.ufo_module == 'ufoLib2': + try: + from ufoLib2 import Font + except ImportError: + print("Can't find ufoLib2 installed. Please install it or specify a different UFO library.") + exit(1) + else: try: from defcon import Font - library = "defcon" except ImportError: - print("Either ufoLib2 or defcon library is required for this command to work.\nPlease install one of them.") + print("Can't find defcon installed. Please install it or specify a different UFO library.") exit(1) - if len(argv) <= 1: - print("No font path supplied.\nUsage: extractufo font [font ...]") - exit(1) + structure="zip" if args.zip else "package" + had_write_errors = False - print(f"Will use {library} library for UFO output.") - - for font_path in argv[1:]: - ufo_path = f"{font_path}.ufo" + for font_path in args.FONT_FILE: + ufo_path = f"{font_path}.ufo" if not args.zip else f"{font_path}.ufoz" print(f"Extracting {ufo_path}... ", end="") ufo = Font() extractUFO(font_path, ufo) - ufo.save(ufo_path, overwrite=True) + try: + ufo.save(ufo_path, overwrite=args.overwrite, structure=structure) + except FileExistsError: + print("path already exists, skipping.") + had_write_errors = True + continue print("done.") + + exit(had_write_errors)