genetic-images/src/parseargs.cc

98 lines
3.9 KiB
C++
Raw Permalink Normal View History

2019-03-19 12:49:37 +00:00
#include "parseargs.hh"
2019-04-27 14:27:22 +00:00
2019-03-19 12:49:37 +00:00
#include <boost/program_options.hpp>
#include <iostream>
constexpr int DEFAULT_ITERATIONS = 2000;
2019-04-27 14:27:22 +00:00
using path = std::filesystem::path;
2019-03-20 11:34:46 +00:00
namespace po = boost::program_options;
2019-04-14 01:58:49 +00:00
/**
* \brief Ensures correct output path
*
* Checks if an output file exists, and if yes if it has an extension. In case
* it doesnt exist, `output_` is appended at the beginning of the input
* filename. If the output path does not have an extension, the type `.png` is
* appended at the end of the path.
*
* \param[in] t_vm Arguments passed to the program
* \param[out] t_input Input path
* \param[out] t_output Output path
*/
2019-04-27 14:27:22 +00:00
void processFilenames(po::variables_map const &t_vm, path const &t_input,
path &t_output)
2019-03-28 11:26:05 +00:00
{
2019-04-27 14:27:22 +00:00
if (!t_vm.count("output")) {
t_output.replace_filename("output_"
+ std::string{t_input.filename().string()});
} else if (!t_output.has_extension()) {
t_output.replace_extension(".png");
}
2019-03-20 11:34:46 +00:00
}
2019-04-14 01:58:49 +00:00
/**
* Parses the arguments given to the program, formats them and returns them as
* a tuple. If `-h` or `--help` or a malformed argument is passed, then the
* list of arguments and their comment will be displayed, and the program will
* exit.
*
* \param[in] t_ac Number of arguments passed to the program
* \param[in] t_av Arguments passed to the program
* \return Tuple of path, path, int, int, int, int, int, bool and bool
*/
2019-04-27 14:27:22 +00:00
[[nodiscard]] auto parse_args(int t_ac, char **t_av) -> ParsedArgs
2019-03-28 11:26:05 +00:00
{
2019-04-27 14:27:22 +00:00
ParsedArgs ret{};
po::options_description desc("Allowed options");
2019-04-27 16:04:32 +00:00
desc.add_options()("help,h", "Display this help message")(
"input,i", po::value<path>(),
"Input image")("output,o", po::value<path>(),
"Image output path (default: \"output_\" + input path)")(
"iterations,n", po::value<int>(), "Number of iterations (default: 2000)")(
"method,m", po::value<int>(), "Method number to be used (default: 1)")(
"form,f", po::value<int>(), "Select shape (1:square, 2:triangle)")(
"cols,c", po::value<int>(),
"For method 5 only, number of columns the reference image should be "
"divided into. If the value is equal to 0, then it will be assumed "
"there will be as many rows as there are collumns. (default: 0)")(
"rows,r", po::value<int>(),
"For method 5 only, number of rows the reference image should be "
"divided into. (default: 1)")(
"submethod,S", po::value<int>(),
"Sub-method that will be used to generate the individual tiles from "
"method 5. (default: 1)")("size,s",
"Enables controlled size of the random shapes")(
"verbose,v", "Enables verbosity");
2019-04-27 14:27:22 +00:00
po::variables_map vm;
po::store(po::parse_command_line(t_ac, t_av, desc), vm);
po::notify(vm);
if (vm.count("help") || !vm.count("input")) {
std::cout << desc << "\n";
std::exit(!vm.count("help"));
}
2019-03-20 11:34:46 +00:00
2019-04-27 14:27:22 +00:00
auto const input_path = vm["input"].as<path>();
auto output_path
= vm.count("output") ? vm["output"].as<path>() : input_path.filename();
processFilenames(vm, input_path, output_path);
2019-03-20 11:34:46 +00:00
2019-04-27 14:27:22 +00:00
ret.input_path = input_path;
ret.output_path = output_path;
ret.iterations = vm.count("iterations") ? vm["iterations"].as<int>()
: DEFAULT_ITERATIONS;
ret.method = vm.count("method") ? vm["method"].as<int>() : 1;
switch (vm.count("form") ? vm["form"].as<int>() : 1) {
case 2: ret.shape = Shape::ShapeType::Triangle; break;
2019-04-27 16:04:32 +00:00
case 1: ret.shape = Shape::ShapeType::Square; break;
2019-04-27 14:27:22 +00:00
default: ret.shape = Shape::ShapeType::Square; break;
}
2019-04-27 14:27:22 +00:00
ret.cols = vm.count("cols") ? vm["cols"].as<int>() : 0;
ret.rows = vm.count("rows") ? vm["rows"].as<int>() : 1;
ret.submethod = vm.count("submethod") ? vm["submethod"].as<int>() : 1;
ret.controlled_size = vm.count("size");
ret.verbose = vm.count("verbose");
return ret;
2019-03-19 12:49:37 +00:00
}