3 ms·
> doing it correctly is hard What is "correct" though? `grep -iEt file` complains that `-t` is not a valid option (i.e. compressed form of `grep -i -E t`) So
by stephenr 5y ago
> doing it correctly is hard
What is "correct" though?
`grep -iEt file` complains that `-t` is not a valid option (i.e. compressed form of `grep -i -E t`)
So Grep does not support passing values to the last of a series of short options that are grouped.
But tar does:
`tar -cvffoo.tar file` happily creates foo.tar from file.
and Python does:
`python3.5 -Ec'import sys; print(sys.argv[1:]);' foo` prints `['foo']`
and Gzip does:
`gzip -kvSsuff file` creates `file.suff`
I write a reasonable amount of shell. I've written a number of reasonably complex tools (mostly server admin related but also some more developer focused) in shell. I've written a library in shell, to make writing programs in shell easier. Part of that library does argument passing.
Until yesterday it supported short and long opts, with values, without values, with optional values, using space or `=` as a value separator. It also supported short opts with an 'attached' value (i.e. `-fmy-file.txt` in place of `-f my-file.txt`).
Yesterday, based on this discussion, I added the ability for it to handle `-abc` (e.g. `-a -b -c` where none of the options expects an argument) or `-abc` (e.g. `-a -b c` where the final option either requires or has an optional argument).
So, once again: what is "correct", when it comes to option handling? Your example was grep, but by the examples of other GNU tools, it only does an "approximation".
- faho 5y agoGNU grep doesn't take a value for `-E`, so it interprets this as `grep -i -E -t`, which explains the error. It doesn't have a `-t` option. I don't know of any grep that does take a value for `-E`. It does support taking a value for `-f`, and it also does support that as the last option in a group: grep -qf/path/to/file works, even if path is something like "patterns" (see that "t" in there?). >So, once again: what is "correct", when it comes to option handling? Your example was grep, but by the examples of other GNU tools, it only does an "approximation". I believe you're simply mistaken about what options it takes, and which options take values, the parsing seems "correct".
- stephenr 5y agoWelp yep, the folly of -E vs -e (was someone saying one character options are fine?) You're right that grep does support what I intended to try (`grep -iet`).