Browse Source

Added check for arguments and ranges for args

refactor
5 years ago
parent
commit
6c188c4bad
  1. 43
      source/main.cpp

43
source/main.cpp

@ -2,6 +2,34 @@ @@ -2,6 +2,34 @@
#include <iostream>
using namespace std;
// redundancy 1 to 10
// dot size 50 to 100
// dpi 40 to 300
bool validate(cxxopts::Options &o) {
bool is_ok = true;
if (o["i"].as<string>().empty()) {
cerr << "error: no input file given" << endl;
is_ok = false;
}
if (o["o"].as<string>().empty()) {
cerr << "error: no output file given" << endl;
is_ok = false;
}
if (o["s"].as<int>() < 50 || o["s"].as<int>() > 100) {
cerr << "error: invalid value for dot size" << endl;
is_ok = false;
}
if (o["d"].as<int>() < 40 || o["d"].as<int>() > 300) {
cerr << "error: invalid value for dpi" << endl;
is_ok = false;
}
if (o["r"].as<int>() < 2 || o["r"].as<int>() > 10) {
cerr << "error: invalid value for redundancy" << endl;
is_ok = false;
}
return is_ok;
}
cxxopts::Options arguments(int ac, char **av) {
cxxopts::Options o(av[0],
"Encodes or decodes high-density printable file backups.");
@ -23,24 +51,21 @@ cxxopts::Options arguments(int ac, char **av) { @@ -23,24 +51,21 @@ cxxopts::Options arguments(int ac, char **av) {
cxxopts::value<string>(),
"FILE")
("d,dpi",
"dots per inch of input or output bitmap",
"dots per inch of input or output bitmap, between 40 and 300",
cxxopts::value<int>() -> default_value("200"))
("s,dotsize",
"size of the dots in bitmap as percentage of maximum dot size in pixels",
"size of the dots in bitmap as percentage of maximum dot size in pixels, between 50 and 100",
cxxopts::value<int>() -> default_value("70"))
("r,redundancy",
"data redundancy ratio of input or output bitmap as a reciprocal",
cxxopts::value<int>() -> implicit_value("5"))
"data redundancy ratio of input or output bitmap as a reciprocal, between 2 and 10",
cxxopts::value<int>() -> default_value("5"))
;
o.parse_positional(parg);
o.parse(ac, av);
if (o.count("help")) {
cout << o.help() << endl;
}
// if there is no given input or output, then invalid
if ((o.count("i") < 1 || o.count("o") < 1) ||
(o.count("encode") < 1 && o.count("decode") < 1)) {
cerr << "error: no input or output file - exiting" << endl;
exit(EXIT_SUCCESS);
} else if (!validate(o)) {
exit(EXIT_FAILURE);
}
return o;

Loading…
Cancel
Save