genetic-images/src/parseargs.cc

91 lines
3.4 KiB
C++
Raw Normal View History

2019-03-19 12:49:37 +00:00
#include "parseargs.hh"
#include <boost/program_options.hpp>
#include <iostream>
constexpr int DEFAULT_ITERATIONS = 2000;
2019-03-20 11:34:46 +00:00
using path = std::filesystem::path;
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
*/
void processFilenames(po::variables_map const& t_vm,
2019-03-28 11:26:05 +00:00
path const& t_input,
path& t_output)
{
2019-04-14 01:58:49 +00:00
if (!t_vm.count("output")) {
2019-03-28 11:26:05 +00:00
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-01 14:55:02 +00:00
[[nodiscard]] auto parse_args(int t_ac, char** t_av)
-> std::tuple<path, path, int, int, int, int, int, bool, bool>
2019-03-28 11:26:05 +00:00
{
po::options_description desc("Allowed options");
desc.add_options()
2019-04-13 13:09:07 +00:00
("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)")
("cols,c", po::value<int>(),
2019-04-13 13:09:07 +00:00
"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)")
2019-04-13 13:09:07 +00:00
("size,s", "Enables controlled size of the random shapes")
("verbose,v", "Enables verbosity");
2019-03-28 11:26:05 +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-28 11:26:05 +00:00
}
2019-03-20 11:34:46 +00:00
2019-03-28 11:26:05 +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-03-28 11:26:05 +00:00
return std::make_tuple(
input_path, output_path,
vm.count("iterations") ? vm["iterations"].as<int>() : DEFAULT_ITERATIONS,
vm.count("method") ? vm["method"].as<int>() : 1,
vm.count("cols") ? vm["cols"].as<int>() : 0,
vm.count("rows") ? vm["rows"].as<int>() : 1,
vm.count("submethod") ? vm["submethod"].as<int>() : 1,
vm.count("size"),
2019-04-13 12:50:45 +00:00
vm.count("verbose"));
2019-03-19 12:49:37 +00:00
}