implement custom output path flag and integration tests
This commit is contained in:
@@ -25,23 +25,30 @@ struct Bucket {
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool quiet = false;
|
||||
std::string output_path = "palette.json";
|
||||
std::string imagefile;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
std::string arg = argv[i];
|
||||
if (arg == "--help" || arg == "-h") {
|
||||
cout <<
|
||||
cout <<
|
||||
"ccolors - extract color palette from images\n\n"
|
||||
"Usage:\n"
|
||||
" " << argv[0] << " <image> [options]\n\n"
|
||||
"Options:\n"
|
||||
" -h, --help Show this help page\n"
|
||||
" -q, --quiet Suppress terminal output (JSON is still written)\n\n"
|
||||
" -h, --help Show this help page\n"
|
||||
" -q, --quiet Suppress terminal output (JSON is still written)\n"
|
||||
" -o, --output <file> Write JSON palette to <file> (default: palette.json)\n\n"
|
||||
"Output:\n"
|
||||
" Prints dominant colors as HEX and terminal swatches\n"
|
||||
" Also writes palette.json\n";
|
||||
" Also writes palette.json (or the path given with -o)\n";
|
||||
return 0;
|
||||
} else if (arg == "--quiet" || arg == "-q") {
|
||||
quiet = true;
|
||||
} else if ((arg == "--output" || arg == "-o") && i + 1 < argc) {
|
||||
output_path = argv[++i];
|
||||
} else if ((arg == "--output" || arg == "-o") && i + 1 >= argc) {
|
||||
cout << "Option " << arg << " requires an argument.\n";
|
||||
return 1;
|
||||
} else if (imagefile.empty()) {
|
||||
imagefile = arg;
|
||||
} else {
|
||||
@@ -167,7 +174,12 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
int paletteSize = std::min(5, (int)buckets.size());
|
||||
|
||||
std::ofstream json("palette.json");
|
||||
std::ofstream json(output_path);
|
||||
if (!json) {
|
||||
cout << "Cannot open output file: " << output_path << "\n";
|
||||
stbi_image_free(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
json << "{\n";
|
||||
json << " \"image\": \"" << imagefile << "\",\n";
|
||||
|
||||
Reference in New Issue
Block a user