JamieMason / ImageOptim-CLI

Make optimisation of images part of your automated build process
https://foldleft.io/image-tools
MIT License
3.45k stars 126 forks source link

Add support for use with xargs #112

Closed vitorgalvao closed 5 years ago

vitorgalvao commented 9 years ago

Doing find … | imageoptim works as expected, but find … | xargs imageoptim does not (it quits and shows the help message). This matters because if we want to find … -print0 | xargs -0 imageoptim, we can’t.

JamieMason commented 8 years ago

Thanks for this @vitorgalvao. I'll need some help here as I've not used xargs before, is it not something that would be piped, as in find … | xargs | imageoptim rather than find … | xargs imageoptim?

Please add some more info/context and I'll do my best to help, thanks.

vitorgalvao commented 8 years ago

No, you don’t pipe from xargs, but to it. The example I gave is correct, and that link explains well what it does.

You may close this if you want, though. I’ve since started using the ImageOptim app for this directly since it behaves correctly in this situation.

JamieMason commented 8 years ago

Thanks @vitorgalvao, my inexperience with xargs is showing here. I'll leave this open as it sounds like something that should be supported, so you can also run those images through imagealpha and JPEGmini.

Thanks for the link, that will be useful.

jamesstout commented 7 years ago

I can have a look at this one. Quick q though, why do you want to null terminate the list from the find command? Is there a special case? thanks

Files with spaces in the name?

vitorgalvao commented 7 years ago

@jamesstout Filenames are full of surprises. Piping from find to xargs should always be done that way. Look at their respective man pages and you’ll see they both acknowledge this — the options were essentially made for each other.

jamesstout commented 7 years ago

I had a stab at this last night. Tricky. When invoked via xargs, all the files are passed to imageoptim along with any imageoptim args. So, you'd need:

  1. To know if you were invoked via xargs
  2. If so, process the args in a different way to strip out files vs command args

There are ways to tell if you are in a sub-shell, or part of a command expansion, or an interactive shell, but I couldn't find a portable (works in all shells) way to determine if we were invoked via xargs.

I did get it working with ps and grep, but it seems like a hack and then a lot of work to have two ways to process the args. I could spend time on it, but @JamieMason let me know what you think.

vitorgalvao commented 7 years ago

To reiterate, if I’m the only one asking for this, feel free to close it.

That said, being piped from xargs is expected behaviour from *nix CLI utilities.

vitorgalvao commented 5 years ago

To clarify, is the fix live or have you decided not to support it?

JamieMason commented 5 years ago

Hey @vitorgalvao, I decided not to proceed but if anyone has a nice solution they are welcome to get involved.