Skip to content

Commit e169236

Browse files
committed
Improve the cmdline script to accept more options
1 parent 29fc2ec commit e169236

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

Lib/extractor/__init__.py

+44-16
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,56 @@ def cmdline():
8484
8585
Usage: extractufo font [font ...]
8686
"""
87-
from sys import argv, exit
88-
try:
89-
from ufoLib2 import Font
90-
library = "ufoLib2"
91-
except ImportError:
87+
from sys import exit
88+
from argparse import ArgumentParser
89+
90+
parser = ArgumentParser(
91+
description="Extract data from font binaries and build UFO objects from them.",
92+
epilog="Each resulting UFO will be saved as FONT_FILE.ufo(z) in the same directory as the original FONT_FILE.",
93+
)
94+
parser.add_argument('FONT_FILE', help='Input font path', nargs="+")
95+
parser.add_argument('-m', '--ufo-module', choices=['ufoLib2', 'defcon'], help='Select the default library for writing UFOs (default: autodetect, prefer ufoLib2)')
96+
parser.add_argument('-z', '--zip', action="store_true", help="Output UFO ZIP")
97+
parser.add_argument('-o', '--overwrite', action="store_true", help="Overwrite output location if exists")
98+
99+
args = parser.parse_args()
100+
if args.ufo_module is None:
101+
try:
102+
from ufoLib2 import Font
103+
print("Will use ufoLib2 for UFO output.")
104+
except ImportError:
105+
try:
106+
from defcon import Font
107+
print("Will use defcon for UFO output.")
108+
except ImportError:
109+
print("Either ufoLib2 or, alternatively, defcon library is required to run this command.\nPlease install one of them.")
110+
exit(1)
111+
elif args.ufo_module == 'ufoLib2':
112+
try:
113+
from ufoLib2 import Font
114+
except ImportError:
115+
print("Can't find ufoLib2 installed. Please install it or specify a different UFO library.")
116+
exit(1)
117+
else:
92118
try:
93119
from defcon import Font
94-
library = "defcon"
95120
except ImportError:
96-
print("Either ufoLib2 or defcon library is required for this command to work.\nPlease install one of them.")
121+
print("Can't find defcon installed. Please install it or specify a different UFO library.")
97122
exit(1)
98123

99-
if len(argv) <= 1:
100-
print("No font path supplied.\nUsage: extractufo font [font ...]")
101-
exit(1)
102-
103-
print(f"Will use {library} library for UFO output.")
104-
105-
for font_path in argv[1:]:
106-
ufo_path = f"{font_path}.ufo"
124+
structure="zip" if args.zip else "package"
125+
had_write_errors = False
126+
for font_path in args.FONT_FILE:
127+
ufo_path = f"{font_path}.ufo" if not args.zip else f"{font_path}.ufoz"
107128
print(f"Extracting {ufo_path}... ", end="")
108129
ufo = Font()
109130
extractUFO(font_path, ufo)
110-
ufo.save(ufo_path, overwrite=True)
131+
try:
132+
ufo.save(ufo_path, overwrite=args.overwrite, structure=structure)
133+
except FileExistsError:
134+
print("path already exists, skipping.")
135+
had_write_errors = True
136+
continue
111137
print("done.")
138+
139+
exit(had_write_errors)

0 commit comments

Comments
 (0)