Clean arguments parsing
parent
5b8b0f8fb8
commit
291c23124f
@ -0,0 +1,136 @@
|
|||||||
|
#include "argsprocessor.h"
|
||||||
|
#include "output_util.h"
|
||||||
|
#include "filepath_util.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
static constexpr int DEFAULT_SCREEN_WIDTH = 1280;
|
||||||
|
static constexpr int DEFAULT_SCREEN_HEIGHT = 720;
|
||||||
|
static constexpr int DEFAULT_SPLITTING = 4;
|
||||||
|
static const std::string DEFAULT_PATH = ".";
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
ArgsProcessor::ArgsProcessor(int argc, char **argv)
|
||||||
|
{
|
||||||
|
parse_result = tryConvertInput(argc, argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::broken() const
|
||||||
|
{
|
||||||
|
return parse_result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<int, sf::Vector2i, std::string> ArgsProcessor::unpack() const
|
||||||
|
{
|
||||||
|
return {image_splitting, game_resolution, image_path};
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::tryConvertInput(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int error = iterateArgc(argc, argv);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
if (image_path == DEFAULT_PATH)
|
||||||
|
{
|
||||||
|
// no path was given, loading random image from '.'
|
||||||
|
const auto &[error, ret_path] = filepath::parsePath(image_path);
|
||||||
|
if (error)
|
||||||
|
return makeError(output::IMG_FAIL_MSG);
|
||||||
|
|
||||||
|
image_path = ret_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ArgsProcessor::isFlag(const char *arg, const char *flag) const
|
||||||
|
{
|
||||||
|
return (strcmp(arg, flag) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::iterateArgc(int argc, char **argv)
|
||||||
|
{
|
||||||
|
for (int current_arg = 1; current_arg < argc; ++current_arg) // current_arg = 0 is executable name
|
||||||
|
{
|
||||||
|
if (isFlag(argv[current_arg], output::HELP_FLAG))
|
||||||
|
return makeError(output::HELP_MSG);
|
||||||
|
|
||||||
|
if (isFlag(argv[current_arg], output::SPLITTING_FLAG))
|
||||||
|
{
|
||||||
|
int error = parseSplitting(current_arg, argc, argv);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
++current_arg;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isFlag(argv[current_arg], output::RESOLUTION_FLAG))
|
||||||
|
{
|
||||||
|
int error = parseResolution(current_arg, argc, argv);
|
||||||
|
if (error)
|
||||||
|
return error;
|
||||||
|
|
||||||
|
++current_arg;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto &[error, ret_path] = filepath::parsePath(argv[current_arg]);
|
||||||
|
|
||||||
|
if (error)
|
||||||
|
return makeError(output::IMG_FAIL_MSG);
|
||||||
|
|
||||||
|
image_path = ret_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::parseSplitting(int curr_arg, int argc, char **argv)
|
||||||
|
{
|
||||||
|
const int value_rg = curr_arg + 1;
|
||||||
|
if (value_rg == argc)
|
||||||
|
return makeError(output::SPLITTING_MSG);
|
||||||
|
|
||||||
|
image_splitting = -1;
|
||||||
|
if (std::isdigit(*argv[value_rg]))
|
||||||
|
image_splitting = std::stoi(argv[value_rg]);
|
||||||
|
|
||||||
|
if (image_splitting < 2)
|
||||||
|
return makeError(output::SPLITTING_MSG);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::parseResolution(int curr_arg, int argc, char **argv)
|
||||||
|
{
|
||||||
|
const int value_rg = curr_arg + 1;
|
||||||
|
if (value_rg == argc)
|
||||||
|
return makeError(output::RESOLUTION_MSG);
|
||||||
|
|
||||||
|
std::vector<std::string> res = filepath::split(argv[value_rg], 'x'); // splitting argument by 'x' as in 600x900
|
||||||
|
|
||||||
|
if (res.size() < 2)
|
||||||
|
return makeError(output::RESOLUTION_MSG);
|
||||||
|
|
||||||
|
game_resolution = {-1, -1};
|
||||||
|
|
||||||
|
if (std::isdigit(*res[0].c_str()) && std::isdigit(*res[1].c_str()))
|
||||||
|
game_resolution = {std::stoi(res[0].c_str()), std::stoi(res[1].c_str())};
|
||||||
|
|
||||||
|
if (game_resolution.x < 1 || game_resolution.y < 1)
|
||||||
|
return makeError(output::RESOLUTION_MSG);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ArgsProcessor::makeError(const char* msg) const
|
||||||
|
{
|
||||||
|
std::cout << msg;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
#ifndef ARGSPROCESSOR_H
|
||||||
|
#define ARGSPROCESSOR_H
|
||||||
|
|
||||||
|
#include <SFML/Graphics/VertexArray.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class ArgsProcessor
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ArgsProcessor(int argc, char **argv);
|
||||||
|
|
||||||
|
int broken() const;
|
||||||
|
std::tuple<int, sf::Vector2i, std::string> unpack() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool isFlag(const char* arg, const char* flag) const;
|
||||||
|
int tryConvertInput(int argc, char **argv);
|
||||||
|
int iterateArgc(int argc, char **argv);
|
||||||
|
int makeError(const char* msg) const;
|
||||||
|
int parseSplitting(int curr_arg, int argc, char **argv);
|
||||||
|
int parseResolution(int curr_arg, int argc, char **argv);
|
||||||
|
|
||||||
|
int parse_result;
|
||||||
|
int image_splitting;
|
||||||
|
sf::Vector2i game_resolution;
|
||||||
|
std::string image_path;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ARGSPROCESSOR_H
|
Loading…
Reference in New Issue