8000 ttx option parse order · Issue #3507 · fonttools/fonttools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
8000

ttx option parse order #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
za3k opened this issue May 16, 2024 · 10 comments · Fixed by #3509
Closed

ttx option parse order #3507

za3k opened this issue May 16, 2024 · 10 comments · Fixed by #3509

Comments

@za3k
Copy link
za3k commented May 16, 2024

For me, the following command fails (tried to write to DejaVuSerif.ttx):

ttx -t cmap -o - /usr/share/fonts/TTF/DejaVuSerif.ttf

While this one succeeds:

ttx /usr/share/fonts/TTF/DejaVuSerif.ttf -t cmap -o -

I would like them both to work.

@anthrotype
Copy link
Member

how does it fail?

@anthrotype
Copy link
Member
anthrotype commented May 17, 2024

I just tried your first ttx command and it actually works for me, i'm on macos using bash

@anthrotype
Copy link
Member

oh it's the second command ttx path/to/font.ttf -t cmap -o - that doesn't work, not the first one (you confused the two probably).

I'm getting ERROR: File not found: "-t".

Yeah, ideally both should work. Thanks for letting us know.

@justvanrossum
Copy link
Collaborator

This has been a limitation of ttx's command line parser for a long time. I agree that it is pretty bad not ideal, and probably should be rewritten using argparse.

@anthrotype
Copy link
Member

hm it seems that it's a limitation of Python's getopt implementation

Note Unlike GNU getopt(), after a non-option argument, all further arguments are considered also non-options. This is similar to the way non-GNU Unix systems work.

https://docs.python.org/3/library/getopt.html#getopt.getopt

Maybe switching to a different argument parser (e.g. argparse) would fix this, but we'd have to be careful to match the existing command line behavior

@anthrotype
Copy link
Member

i'm inclined to leave as is and document its behavior in the --help maybe. Unless someone is volunteering to port it over to argparse

@anthrotype
Copy link
Member

looks like there's a https://docs.python.org/3/library/getopt.html#getopt.gnu_getopt which uses

GNU style scanning mode is used by default. This means that option and non-option arguments may be intermixed.

Maybe that works? I'll give it a try and report back

@anthrotype
Copy link
Member
anthrotype commented May 17, 2024
diff --git a/Lib/fontTools/ttx.py b/Lib/fontTools/ttx.py
index e7a068748..0adda52d7 100644
--- a/Lib/fontTools/ttx.py
+++ b/Lib/fontTools/ttx.py
@@ -375,7 +375,7 @@ def guessFileType(fileName):


 def parseOptions(args):
-    rawOptions, files = getopt.getopt(
+    rawOptions, files = getopt.gnu_getopt(
         args,
         "ld:o:fvqht:x:sgim:z:baey:",
         [

I can now do e.g. ttx font.ttf -t cmap -o - | less as well as ttx -t cmap -o - font.ttf | less

it seems to work! I wonder if this has other side effects though...

@anthrotype
Copy link
Member

this is the source for gnu_getopts:
https://github.com/python/cpython/blob/fa359dfe07a07eb96e12b0053c5ae7089efdf9cc/Lib/getopt.py#L99-L147

i think it's safe to enable, i'll make a PR

@za3k
Copy link
Author
za3k commented May 17, 2024

Whoa, speedy! thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0