Skip to content

Commit

Permalink
Remove exceptions from CLI (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Sep 7, 2021
1 parent 38926dc commit f9a603b
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 181 deletions.
62 changes: 33 additions & 29 deletions apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ void add_options(const cli_command& cli, convert_params& params) {
// convert images
void run_convert(const convert_params& params) {
// load
auto image = load_image(params.image);
auto error = string{};
auto image = image_data{};
if (!load_image(params.image, image, error)) print_fatal(error);

// resize if needed
if (params.width != 0 || params.height != 0) {
Expand All @@ -74,7 +76,7 @@ void run_convert(const convert_params& params) {
}

// save
save_image(params.output, image);
if (!save_image(params.output, image, error)) print_fatal(error);
}

// view params
Expand All @@ -92,18 +94,17 @@ void add_options(const cli_command& cli, view_params& params) {
#ifndef YOCTO_OPENGL

// view images
void run_view(const view_params& params) {
throw io_error::not_supported_error("Opengl not compiled");
}
void run_view(const view_params& params) { print_fatal("Opengl not compiled"); }

#else

// view images
void run_view(const view_params& params) {
// load
auto error = string{};
auto images = vector<image_data>(params.images.size());
for (auto idx = 0; idx < (int)params.images.size(); idx++) {
images[idx] = load_image(params.images[idx]);
if (!load_image(params.images[idx], images[idx], error)) print_fatal(error);
}

// run viewer
Expand All @@ -128,15 +129,17 @@ void add_options(const cli_command& cli, grade_params& params) {

// grade images
void run_grade(const grade_params& params) {
throw io_error::not_supported_error("Opengl not compiled");
print_fatal("Opengl not compiled");
}

#else

// grade images
void run_grade(const grade_params& params) {
// load image
auto image = load_image(params.image);
auto error = string{};
auto image = image_data{};
if (!load_image(params.image, image, error)) print_fatal(error);

// run viewer
colorgrade_image("yimage", params.image, image);
Expand Down Expand Up @@ -165,33 +168,35 @@ void add_options(const cli_command& cli, diff_params& params) {
// resize images
void run_diff(const diff_params& params) {
// load
auto image1 = load_image(params.image1);
auto image2 = load_image(params.image2);
auto error = string{};
auto image1 = image_data{}, image2 = image_data{};
if (!load_image(params.image1, image1, error)) print_fatal(error);
if (!load_image(params.image2, image2, error)) print_fatal(error);

// check sizes
if (image1.width != image2.width || image1.height != image2.height) {
throw io_error{
params.image1 + "," + params.image2 + ": image different sizes"};
print_fatal(
params.image1 + "," + params.image2 + ": image different sizes");
}

// check types
if (image1.linear != image2.linear) {
throw io_error{
params.image1 + "," + params.image2 + "image different types"};
print_fatal(params.image1 + "," + params.image2 + "image different types");
}

// compute diff
auto diff = image_difference(image1, image2, true);

// save
if (params.output != "") save_image(params.output, diff);
if (params.output != "")
if (!save_image(params.output, diff, error)) print_fatal(error);

// check diff
if (params.signal) {
for (auto& c : diff.pixels) {
if (max(xyz(c)) > params.threshold) {
throw io_error{
params.image1 + "," + params.image2 + "image content differs"};
print_fatal(
params.image1 + "," + params.image2 + "image content differs");
}
}
}
Expand Down Expand Up @@ -220,19 +225,19 @@ void add_options(const cli_command& cli, setalpha_params& params) {
// setalpha images
void run_setalpha(const setalpha_params& params) {
// load
auto image = load_image(params.image);
auto alpha = load_image(params.alpha);
auto error = string{};
auto image = image_data{}, alpha = image_data{};
if (!load_image(params.image, image, error)) print_fatal(error);
if (!load_image(params.alpha, alpha, error)) print_fatal(error);

// check sizes
if (image.width != alpha.width || image.height != alpha.height) {
throw io_error{
params.image + "," + params.alpha + ": image different size"};
print_fatal(params.image + "," + params.alpha + ": image different size");
}

// check types
if (image.linear != alpha.linear) {
throw io_error{
params.image + "," + params.alpha + ": image different types"};
print_fatal(params.image + "," + params.alpha + ": image different types");
}

// edit alpha
Expand All @@ -252,7 +257,7 @@ void run_setalpha(const setalpha_params& params) {
}

// save
save_image(params.output, out);
if (!save_image(params.output, out, error)) print_fatal(error);
}

struct app_params {
Expand All @@ -278,8 +283,9 @@ void add_options(const cli_command& cli, app_params& params) {
void run(const vector<string>& args) {
// command line parameters
auto params = app_params{};
auto error = string{};
auto cli = make_cli("yimage", params, "Process and view images.");
parse_cli(cli, args);
if (!parse_cli(cli, args, error)) print_fatal(error);

// dispatch commands
if (params.command == "convert") {
Expand All @@ -293,11 +299,9 @@ void run(const vector<string>& args) {
} else if (params.command == "setalpha") {
return run_setalpha(params.setalpha);
} else {
throw io_error("yimage: unknown command");
print_fatal("yimage: unknown command");
}
}

// Main
int main(int argc, const char* argv[]) {
handle_errors(run, make_cli_args(argc, argv));
}
int main(int argc, const char* argv[]) { run(make_cli_args(argc, argv)); }
44 changes: 26 additions & 18 deletions apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ void add_options(const cli_command& cli, view_params& params) {
#ifndef YOCTO_OPENGL

// view shapes
void run_view(const view_params& params) {
throw io_error::not_implemented_error("Opengl not compiled");
}
void run_view(const view_params& params) { print_fatal("Opengl not compiled"); }

#else

// view shapes
void run_view(const view_params& params) {
// load mesh
auto shape = load_shape(params.shape, true);
auto error = string{};
auto shape = shape_data{};
if (!load_shape(params.shape, shape, error, true)) print_fatal(error);

// make scene
auto scene = make_shape_scene(shape, params.addsky);
Expand All @@ -91,7 +91,7 @@ void add_options(const cli_command& cli, glview_params& params) {

// view shapes
void run_glview(const glview_params& params) {
throw io_error::not_implemented_error("Opengl not compiled");
print_fatal("Opengl not compiled");
}

#else
Expand Down Expand Up @@ -145,7 +145,9 @@ static scene_data make_shapescene(const shape_data& ioshape_) {

void run_glview(const glview_params& params) {
// loading shape
auto shape = load_shape(params.shape, true);
auto error = string{};
auto shape = shape_data{};
if (!load_shape(params.shape, shape, error, true)) print_fatal(error);

// create scene
auto scene = make_shapescene(shape);
Expand All @@ -169,7 +171,7 @@ void add_options(const cli_command& cli, glpath_params& params) {

// view shapes
void run_glpath(const glpath_params& params) {
throw io_error::not_implemented_error("Opengl not compiled");
print_fatal("Opengl not compiled");
}

#else
Expand Down Expand Up @@ -239,7 +241,9 @@ static scene_data make_pathscene(const shape_data& ioshape_) {

void run_glpath(const glpath_params& params) {
// loading shape
auto ioshape = load_shape(params.shape, true);
auto error = string{};
auto ioshape = shape_data{};
if (!load_shape(params.shape, ioshape, error, true)) print_fatal(error);
if (!ioshape.quads.empty()) {
ioshape.triangles = quads_to_triangles(ioshape.quads);
ioshape.quads = {};
Expand Down Expand Up @@ -339,7 +343,7 @@ void add_options(const cli_command& cli, glpathd_params& params) {

// view shapes
void run_glpathd(const glpathd_params& params) {
throw io_error::not_implemented_error("Opengl not compiled");
print_fatal("Opengl not compiled");
}

#else
Expand Down Expand Up @@ -438,7 +442,9 @@ static scene_data make_pathdscene(const shape_data& ioshape) {

void run_glpathd(const glpathd_params& params) {
// loading shape
auto ioshape = load_shape(params.shape, true);
auto error = string{};
auto ioshape = shape_data{};
if (!load_shape(params.shape, ioshape, error, true)) print_fatal(error);
if (!ioshape.quads.empty()) {
ioshape.triangles = quads_to_triangles(ioshape.quads);
ioshape.quads = {};
Expand Down Expand Up @@ -572,7 +578,7 @@ inline void add_options(const cli_command& cli, glsculpt_params& params) {

// view scene
void run_glsculpt(const glsculpt_params& params) {
throw io_error::not_implemented_error("Opengl not compiled");
print_fatal("Opengl not compiled");
}

#else
Expand Down Expand Up @@ -1207,15 +1213,18 @@ static pair<bool, bool> sculpt_update(sculpt_state& state, shape_data& shape,

void run_glsculpt(const glsculpt_params& params_) {
// loading shape
auto ioshape = load_shape(params_.shape, true);
auto error = string{};
auto ioshape = shape_data{};
if (!load_shape(params_.shape, ioshape, error, true)) print_fatal(error);
if (!ioshape.quads.empty()) {
ioshape.triangles = quads_to_triangles(ioshape.quads);
ioshape.quads.clear();
}

// loading texture
auto texture = texture_data{};
if (!params_.texture.empty()) texture = load_texture(params_.texture);
if (!params_.texture.empty())
if (!load_texture(params_.texture, texture, error)) print_fatal(error);

// setup app
auto scene = make_sculptscene(ioshape);
Expand Down Expand Up @@ -1292,9 +1301,10 @@ void add_options(const cli_command& cli, app_params& params) {
// Run
void run(const vector<string>& args) {
// command line parameters
auto error = string{};
auto params = app_params{};
auto cli = make_cli("ymesh", params, "Process and view meshes.");
parse_cli(cli, args);
if (!parse_cli(cli, args, error)) print_fatal(error);

// dispatch commands
if (params.command == "view") {
Expand All @@ -1308,11 +1318,9 @@ void run(const vector<string>& args) {
} else if (params.command == "glsculpt") {
return run_glsculpt(params.glsculpt);
} else {
throw io_error("ymesh: unknown command");
print_fatal("ymesh: unknown command");
}
}

// Main
int main(int argc, const char* argv[]) {
handle_errors(run, make_cli_args(argc, argv));
}
int main(int argc, const char* argv[]) { run(make_cli_args(argc, argv)); }
Loading

0 comments on commit f9a603b

Please sign in to comment.