mike-lischke / antlr-format

A tool to format ANTLR grammars
MIT License
8 stars 2 forks source link

The -p option vs passing file names via non-option args #1

Closed kaby76 closed 12 months ago

kaby76 commented 12 months ago

Thank you for this tool. The tool requires the -p option, and there can only be one -p option value. I'm confused as to why. Why isn't a list of file names as args to the tool supported? E.g., antlr-format f1.g4 f2.g4 f3.g4 ....). Most shells already provide glob expansion, and Gnu Bash can support globstar expansion with env adjustments. So, antlr-format *.g4 would be expanded as antlr-format f1.g4 f2.g4 f3.g4 .....

Note, in the readme, you don't single quote the arg to the -p option. For Bash and Powershell, the default is to do glob pattern matching unless you set your environment carefully to not do that. So, it may be confusing to some.

I tried this out on a few different environments. See below. Note, globstar pattern expansion in the tool does not work for Windows.

Linux

The tool fine on Linux, e.g.:

ken@DESKTOP-DL44R7B:~/grammars-v4/abb$ ls
Generated-Java  abbLexer.g4  abbParser.g4  desc.xml  examples  pom.xml  readme.txt
ken@DESKTOP-DL44R7B:~/grammars-v4/abb$ antlr-format -p '*.g4'

antlr-format, processing options...

formatting 2 file(s)...

done [82 ms]

ken@DESKTOP-DL44R7B:~/grammars-v4/abb$

But, Bash does glob expansion. So, this does not work:

ken@DESKTOP-DL44R7B:~/grammars-v4/abb$ ls
Generated-Java  abbLexer.g4  abbParser.g4  desc.xml  examples  pom.xml  readme.txt
ken@DESKTOP-DL44R7B:~/grammars-v4/abb$ antlr-format -p *.g4

antlr-format, processing options...

formatting 1 file(s)...

done [70 ms]

ken@DESKTOP-DL44R7B:~/grammars-v4/abb$

The second file is not formatted, and there is no warning.

Windows

Over on Windows, the pattern expansion does not work.

11/26-15:51:13 ~/issues/g4-3833/abb
$ antlr-format -p '*.g4'
No grammar file found using this pattern: C:\msys64\home\Kenne\issues\g4-3833\abb\*.g4.

11/26-15:51:29 ~/issues/g4-3833/abb
$ antlr-format -p './*.g4'
No grammar file found using this pattern: C:\msys64\home\Kenne\issues\g4-3833\abb\*.g4.

11/26-15:51:36 ~/issues/g4-3833/abb

Note, I am using Msys2 Bash ("Cygwin"-based shell), so single quotes are necessary to avoid Bash glob expansion.

NB: The tool does work on Windows if I skip glob patterns, and simply use the file name explicitly. The tool works fine for either relative or absolute paths, and using either the forward slash or backward slash directory separators.

11/26-16:05:51 ~/issues/g4-3833/abb
$ antlr-format -p abbLexer.g4

antlr-format, processing options...

formatting 1 file(s)...

done [52 ms]

11/26-16:06:01 ~/issues/g4-3833/abb
$ antlr-format -p abbParser.g4

antlr-format, processing options...

formatting 1 file(s)...

done [44 ms]

11/26-16:06:11 ~/issues/g4-3833/abb

Multiple files with one call to antlr-format

If I try to give two files, only the first is formatted.

11/26-15:52:47 ~/issues/g4-3833/abb
$ antlr-format -p abbLexer.g4 abbParser.g4

antlr-format, processing options...

formatting 1 file(s)...

done [52 ms]

11/26-15:53:29 ~/issues/g4-3833/abb
$

If I try to give the -p option twice, the second -p option is ignored without warning.

11/26-15:53:29 ~/issues/g4-3833/abb
$ antlr-format -p abbLexer.g4 -p abbParser.g4

antlr-format, processing options...

formatting 1 file(s)...

done [44 ms]

11/26-15:54:21 ~/issues/g4-3833/abb
$

It does not matter if I use cmd.exe either.

Powershell on Linux

Powershell operates similarly to Bash. Glob patterns are expanded prior to exec() of the tool. So, one must single quote arguments when calling the tool.

What is the purpose of -p option? Doesn't every shell offer some equivalent to "find"?

Why is the -p option provided? Bash supports glob patterns, and globstar patterns when the env is set for it. (NB: expansion of the glob or globstar patter cannot work if multiple files are discovered, because the -p option only takes one parameter.)

I can also do a find and call the tool for each file:


$ find . -name '*.g4' -exec antlr-format -p '{}' ';'

antlr-format, processing options...

formatting 1 file(s)...

done [51 ms]

antlr-format, processing options...

formatting 1 file(s)...

done [45 ms]

11/26-15:52:47 ~/issues/g4-3833/a
mike-lischke commented 12 months ago

Fixed by release v1.0.1-cli