added: cmdline args handling

This commit is contained in:
2024-07-06 01:39:39 -04:00
parent 116d068765
commit eb773405a6
11 changed files with 206 additions and 21 deletions

View File

@ -1,2 +1,159 @@
#include "arguments.h"
#include <iostream>
#include <utility>
#include <fstream>
#include <string>
#include "file_input.h"
#include "file_output.h"
#include "console_input.h"
#include "console_output.h"
#include "curses_input.h"
#include "curses_output.h"
feature proc_args(int argc, char **argv,
std::unique_ptr<cursor> &curse,
std::unique_ptr<input> &in,
std::unique_ptr<display> &out,
std::unique_ptr<logger> &log) {
feature result = 0;
std::string str;
std::string fn_fin;
std::string fn_fout;
std::string fn_lout;
std::string fn_save;
std::string seed;
for (int i = 1; i < argc; ++i) {
str = argv[i];
if (str == "-n") {
if (result & (FEATURE_IN_FILE | FEATURE_OUT_FILE))
return FEATURE_CONFLICT | i;
result |= FEATURE_NCURSES;
} else if (str == "-r") {
result |= FEATURE_RAND_MAP;
} else if (str == "-m") {
result |= FEATURE_MENU;
} else if (str == "-c") {
result |= FEATURE_ENEMIES_CHASE;
} else if (str == "-i") {
result |= FEATURE_INVENTORY;
} else if (str == "-t") {
result |= FEATURE_THROW;
} else if (str == "-R") {
result |= FEATURE_REVISIT;
} else if (str == "-s") {
++i;
str = argv[i];
if (!seed.empty() && seed != str)
return FEATURE_CONFLICT | i;
seed = str;
result |= FEATURE_SEED;
} else if (str == "-L") {
++i;
str = argv[i];
if (!fn_lout.empty() && fn_lout != str)
return FEATURE_CONFLICT | i;
fn_lout = str;
result |= FEATURE_LOG;
} else if (str == "-I") {
++i;
str = argv[i];
if (!fn_fin.empty() && fn_fin != str)
return FEATURE_CONFLICT | i;
fn_fin = str;
result |= FEATURE_IN_FILE;
} else if (str == "-O") {
++i;
str = argv[i];
if (!fn_fout.empty() && fn_fout != str)
return FEATURE_CONFLICT | i;
fn_fout = str;
result |= FEATURE_OUT_FILE;
} else if (str == "-h" || str == "--help") {
return FEATURE_LIST_ARGS;
} else {
return FEATURE_PANIC | i;
}
}
if (result & FEATURE_IN_FILE) {
std::ifstream fin(fn_fin);
if (!fin.good())
return FEATURE_PANIC_FILE | FEATURE_IN_FILE;
in = std::make_unique<file_input>(std::move(fin));
} else if (!(result & FEATURE_NCURSES))
in = std::make_unique<console_input>(std::cin);
if (result & FEATURE_OUT_FILE) {
std::ofstream fout(fn_fout);
if (!fout.good())
return FEATURE_PANIC_FILE | FEATURE_OUT_FILE;
out = std::make_unique<file_output>(std::move(fout));
} else if (!(result & FEATURE_NCURSES))
out = std::make_unique<console_output>(std::cout);
if (result & FEATURE_LOG) {
std::ofstream lout(fn_lout);
if (!lout.good())
return FEATURE_PANIC_FILE | FEATURE_LOG;
log = std::make_unique<logger>(std::move(lout));
}
if (result & FEATURE_NCURSES) {
curse = std::make_unique<cursor>();
in = std::make_unique<curses_input>(curse);
out = std::make_unique<curses_output>(curse);
}
return result;
}
// IMPORTANT: This is meant for already panicking
void panic_args(feature panic) {
using namespace std;
if (panic & FEATURE_PANIC)
cerr << "Invalid argument No. " << (panic ^ FEATURE_PANIC)
<< " !" << endl;
else if (panic & FEATURE_CONFLICT)
cerr << "Argument No. " << (panic ^ FEATURE_CONFLICT)
<< " conflicts with a previous argument!" << endl;
else if (panic & FEATURE_PANIC_FILE) {
switch (panic ^ FEATURE_PANIC_FILE) {
case FEATURE_IN_FILE:
cerr << "Cannot open specified input file!" << endl;
break;
case FEATURE_OUT_FILE:
cerr << "Cannot open specified output file!" << endl;
break;
case FEATURE_LOG:
cerr << "Cannot open specified log file!" << endl;
break;
default:
cerr << "Something must have went really, really, wrong..."
<< endl;
}
} else
cerr << "Something must have went really, really, wrong..."
<< endl;
}