Skip to content

Commit

Permalink
UNRIM: Use CLI class
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmo-ray authored and DrMcCoy committed Apr 13, 2017
1 parent f7e8fb2 commit d51ce21
Showing 1 changed file with 41 additions and 89 deletions.
130 changes: 41 additions & 89 deletions src/unrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "src/common/platform.h"
#include "src/common/readstream.h"
#include "src/common/readfile.h"
#include "src/common/cli.h"

#include "src/aurora/util.h"
#include "src/aurora/rimfile.h"
Expand Down Expand Up @@ -85,99 +86,50 @@ int main(int argc, char **argv) {
return 0;
}

bool parseCommandLine(const std::vector<Common::UString> &argv, int &returnValue,
Command &command, Common::UString &file, Aurora::GameID &game) {

file.clear();
std::vector<Common::UString> args;

bool optionsEnd = false;
for (size_t i = 1; i < argv.size(); i++) {
bool isOption = false;

// A "--" marks an end to all options
if (argv[i] == "--") {
optionsEnd = true;
continue;
}

// We're still handling options
if (!optionsEnd) {
// Help text
if ((argv[i] == "-h") || (argv[i] == "--help")) {
printUsage(stdout, argv[0]);
returnValue = 0;

return false;
}

if (argv[i] == "--version") {
Version::printVersion();
returnValue = 0;

return false;
}

if (argv[i] == "--nwn2") {
isOption = true;
game = Aurora::kGameIDNWN2;
} else if (argv[i] == "--jade") {
isOption = true;
game = Aurora::kGameIDJade;
} else if (argv[i].beginsWith("-") || argv[i].beginsWith("--")) {
// An options, but we already checked for all known ones

printUsage(stderr, argv[0]);
returnValue = 1;

return false;
}
namespace Common {
namespace CLI {
template<>
int ValGetter<Command &>::get(const std::vector<Common::UString> &args, int i, int) {
_val = kCommandNone;
for (int j = 0; j < kCommandMAX; j++) {
if (!strcmp(args[i].c_str(), kCommandChar[j])) {
_val = (Command) j;
return 0;
}

// Was this a valid option? If so, don't try to use it as a file
if (isOption)
continue;

args.push_back(argv[i]);
}

if (args.size() != 2) {
printUsage(stderr, argv[0]);
returnValue = 1;

return false;
}

// Find out what we should do
command = kCommandNone;
for (int i = 0; i < kCommandMAX; i++)
if (!strcmp(args[0].c_str(), kCommandChar[i]))
command = (Command) i;

// Unknown command
if (command == kCommandNone) {
printUsage(stderr, argv[0]);
returnValue = 1;

return false;
}

file = args[1];

return true;
return -1;
}
}
}

bool parseCommandLine(const std::vector<Common::UString> &argv, int &returnValue,
Command &command, Common::UString &file, Aurora::GameID &game) {

void printUsage(FILE *stream, const Common::UString &name) {
std::fprintf(stream, "BioWare RIM archive extractor\n\n");
std::fprintf(stream, "Usage: %s [<options>] <command> <file>\n\n", name.c_str());
std::fprintf(stream, "Options:\n");
std::fprintf(stream, " -h --help This help text\n");
std::fprintf(stream, " --version Display version information\n");
std::fprintf(stream, " --nwn2 Alias file types according to Neverwinter Nights 2 rules\n");
std::fprintf(stream, " --jade Alias file types according to Jade Empire rules\n\n");
std::fprintf(stream, "Commands:\n");
std::fprintf(stream, " l List archive\n");
std::fprintf(stream, " e Extract files to current directory\n");
using Common::CLI::NoOption;
using Common::CLI::kContinueParsing;
using Common::CLI::Parser;
using Common::CLI::ValGetter;
using Common::CLI::ValAssigner;
using Common::CLI::makeEndArgs;
using Common::CLI::makeAssigners;

NoOption cmdOpt(false, new ValGetter<Command &>(command, "command"));
NoOption fileOpt(false, new ValGetter<Common::UString &>(file, "file"));
Parser parser(argv[0], "BioWare RIM archive extractor",
"Commands:\n"
" l List archive\n"
" e Extract files to current directory\n",
returnValue,
makeEndArgs(&cmdOpt, &fileOpt));

parser.addOption("nwn2", "Alias file types according to Neverwinter Nights 2 rules",
kContinueParsing,
makeAssigners(new ValAssigner<Aurora::GameID>(Aurora::kGameIDNWN2, game)));
parser.addOption("jade", "Alias file types according to Jade Empire rules",
kContinueParsing,
makeAssigners(new ValAssigner<Aurora::GameID>(Aurora::kGameIDJade, game)));

return parser.process(argv);
}

void listFiles(Aurora::RIMFile &rim, Aurora::GameID game) {
Expand Down

0 comments on commit d51ce21

Please sign in to comment.