Implement basic terminal output parsing
parent
f76da9f5bc
commit
ea4aa3c36c
@ -1,7 +1,70 @@
|
||||
#include "application.h"
|
||||
#include "output_util.h"
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cctype>
|
||||
|
||||
int main()
|
||||
constexpr static int MIN_ARGC = 2;
|
||||
constexpr static int MAX_ARGC = 4;
|
||||
|
||||
static std::tuple<int, int, std::string> error(const char* msg)
|
||||
{
|
||||
std::cout << msg;
|
||||
return {EXIT_FAILURE, 0, {}};
|
||||
}
|
||||
|
||||
static std::tuple<int, int, std::string> parseInput(int argc, char **argv)
|
||||
{
|
||||
int splitting = -1;
|
||||
std::string path;
|
||||
|
||||
switch (argc)
|
||||
{
|
||||
default:
|
||||
return error(output::NO_ARG_MSG);
|
||||
|
||||
case MIN_ARGC:
|
||||
// maybe --help
|
||||
if (strcmp(argv[1], output::HELP_FLAG) == 0)
|
||||
return error(output::HELP_MSG);
|
||||
|
||||
/* HERE PARSE FILEPATH INSTEAD */ return error(output::NO_ARG_MSG);
|
||||
break;
|
||||
|
||||
case (MAX_ARGC - 1):
|
||||
case MAX_ARGC:
|
||||
// full stack
|
||||
if (strcmp(argv[1], output::SPLITTING_FLAG) == 0)
|
||||
{
|
||||
if (std::isdigit(*argv[2]))
|
||||
splitting = std::stoi(argv[2]);
|
||||
|
||||
if (splitting < 1)
|
||||
return error(output::SPLITTING_MSG);
|
||||
|
||||
/* HERE PARSE FILEPATH INSTEAD */ return error(output::NO_ARG_MSG);
|
||||
}
|
||||
|
||||
return error(output::NO_ARG_MSG);
|
||||
break;
|
||||
}
|
||||
|
||||
return {EXIT_SUCCESS, splitting, path};
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
Application game;
|
||||
game.run();
|
||||
auto[ret_code, splitting, path] = parseInput(argc, argv);
|
||||
|
||||
if (ret_code) // Error code is EXIT_FAILURE
|
||||
return ret_code;
|
||||
|
||||
Application app;
|
||||
if (app.init(splitting, path))
|
||||
{
|
||||
app.run();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
namespace output {
|
||||
const char* HELP_FLAG = "--help";
|
||||
const char* SPLITTING_FLAG = "-s";
|
||||
|
||||
const char* NO_ARG_MSG = "Please at least provide a path to a target image. --help for more information.\n";
|
||||
const char* HELP_MSG = "usage: sliding-puzzle [-OPTIONS...] FILE-OR-DIRECTORY\n\n"
|
||||
" Necessarily provide the latter variable as path to either a directory\n"
|
||||
" which contains arts (one will be picked randomly) or a specific\n"
|
||||
" image file of .bmp, .dds, .jpg, .png, .tga, or .psd format.\n\n"
|
||||
" Options:\n"
|
||||
" [-s num] Provide it if you want to explicitly define\n"
|
||||
" qualifier for image slicing, it's counted\n"
|
||||
" by the smallest side of given source texture.\n"
|
||||
" Hence, if your image is square, the amount of tiles\n"
|
||||
" will be num * num.\n";
|
||||
const char* SPLITTING_MSG = "-s should be given with a positive num as in [-s num].\n";
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue