Add batch processing via --out-dir (closes #15)#72
Merged
Conversation
`imgcli -i "*.jpg" --out-dir thumbs -vf "scale=400:-1:lanczos" -f png` processes each input independently and writes one output per input. The #1 ask for an image CLI. - src/main.c: run_batch(). Glob-expands each -i (POSIX glob, GLOB_NOCHECK so a non-matching pattern surfaces as a clear per-file error; sorted = deterministic; shell-expansion fallback on Windows). Output is out_dir/<basename>.<ext>, ext from -f or the input's. Per-file errors are reported but don't abort the batch unless --fail-fast; --json emits a results array with processed/failed counts; exit is non-zero if any file failed. Creates the out-dir. Rejects stdin/ generator inputs in batch mode and OUTPUT + --out-dir together. - Docs (README, AGENTS.md incl. the JSON contract, --help) + a make check batch smoke and an ASan CI battery line (ok + per-file failure). Verified: glob expansion, per-file continue-on-error, --fail-fast, extension preservation, the guards, and ASan/UBSan clean across all batch paths. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
| if (slen >= sizeof outpath / 2) slen = sizeof outpath / 2; | ||
| snprintf(outpath, sizeof outpath, "%s/%.*s.%s", out_dir, (int)slen, base, ext); | ||
|
|
||
| if (overwrite != 1 && access(outpath, F_OK) == 0) { |
| const char *out_dir, int quality, int overwrite, int json, int quiet, int fail_fast) { | ||
| char m[1024]; | ||
| if (mkdir(out_dir, 0755) != 0 && errno != EEXIST) { | ||
| snprintf(m, sizeof m, "cannot create --out-dir '%s'", out_dir); |
| #endif | ||
|
|
||
| int nok = 0, nfail = 0; | ||
| char outpath[4096]; |
| const char *dot = strrchr(base, '.'); | ||
| const char *ext = format ? format : (dot ? dot + 1 : NULL); | ||
| if (!ext) { why = "no -f and input has no extension"; goto record; } | ||
| size_t slen = dot ? (size_t)(dot - base) : strlen(base); |
| fputs("{\"input\":", stdout); json_str(stdout, in); | ||
| if (ok) { | ||
| fputs(",\"output\":", stdout); json_str(stdout, outpath); | ||
| printf(",\"ok\":true,\"width\":%d,\"height\":%d,\"bytes\":%lld}", W, H, bytes); |
| fputc('}', stdout); | ||
| } | ||
| } else if (!quiet) { | ||
| if (ok) printf("imgcli: %s -> %s (%dx%d)\n", in, outpath, W, H); |
| } | ||
| } else if (!quiet) { | ||
| if (ok) printf("imgcli: %s -> %s (%dx%d)\n", in, outpath, W, H); | ||
| else fprintf(stderr, "imgcli: %s: FAILED — %s\n", in, why ? why : "unknown"); |
| if (!ok && fail_fast) break; | ||
| } | ||
|
|
||
| if (json) printf("],\"processed\":%d,\"failed\":%d}\n", nok + nfail, nfail); |
| } | ||
|
|
||
| if (json) printf("],\"processed\":%d,\"failed\":%d}\n", nok + nfail, nfail); | ||
| else if (!quiet) fprintf(stderr, "imgcli: batch: %d ok, %d failed\n", nok, nfail); |
mingw's mkdir(const char*) takes one arg; POSIX mkdir takes two. Wrap it in an MKDIR() macro (_mkdir on Windows) so the cross-compile builds. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
| * Returns 0 if every file succeeded, else 1. */ | ||
| static int run_batch(const char **patterns, int npat, const char *graph, const char *format, | ||
| const char *out_dir, int quality, int overwrite, int json, int quiet, int fail_fast) { | ||
| char m[1024]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The most-requested feature for an image CLI: process many files in one call.
What's new
--out-dir DIR— batch mode: each input is processed independently and written toDIR/<basename>.<ext>(ext from-f, else the input's). The dir is created if missing.-ipatterns (POSIXglob, sorted = deterministic). A non-matching pattern surfaces as a clear per-file error. Windows falls back to shell expansion.--fail-fastto opt out. Exit code is non-zero if any file failed.--jsonemits a results array:{"batch":true,"results":[…],"processed":N,"failed":M}.OUTPUT+--out-dirtogether.Verification
--fail-faststops early, extensions preserved without-f, the guards fire.make check+ the ASan CI battery exercise it; docs updated per CONTRIBUTING (README, AGENTS.md JSON contract,--help).🤖 Generated with Claude Code