Skip to content

Commit

Permalink
SporeModManager: improve argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosalie241 committed Nov 9, 2023
1 parent 246371f commit 85a65ff
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions SporeModManager/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "SporeModManager.hpp"
#include "SporeModManagerHelpers.hpp"

#include <algorithm>
#include <filesystem>
#include <iostream>
#include <cstring>
Expand All @@ -23,9 +22,11 @@ using namespace SporeModManagerHelpers;

#ifdef _WIN32
#define arg_str(str) L##str
#define arg_char(c) L##c
#define arg_str_type std::wstring
#else
#define arg_str(str) str
#define arg_char(c) c
#define arg_str_type std::string
#endif // _WIN32

Expand Down Expand Up @@ -98,54 +99,80 @@ int main(int argc, char** argv)

struct option_argument optionArgs[] =
{
{ arg_str("-n"), arg_str("--no-input"), hasNoInputOption },
{ arg_str("-u"), arg_str("--update-needed"), hasUpdateOption },
{ arg_str("-s"), arg_str("--save-paths"), hasSavePathsOption },
{ arg_str("n"), arg_str("no-input"), hasNoInputOption },
{ arg_str("u"), arg_str("update-needed"), hasUpdateOption },
{ arg_str("s"), arg_str("save-paths"), hasSavePathsOption },
};

struct path_argument pathArgs[] =
{
{ arg_str("--corelibs-path"), coreLibsPath },
{ arg_str("--modlibs-path"), modLibsPath },
{ arg_str("--data-path"), dataPath },
{ arg_str("--ep1-path"), ep1Path },
{ arg_str("corelibs-path"), coreLibsPath },
{ arg_str("modlibs-path"), modLibsPath },
{ arg_str("data-path"), dataPath },
{ arg_str("ep1-path"), ep1Path },
};

for (size_t i = 0; i < args.size(); i++)
{
arg_str_type arg = args.at(i);

for (const auto& optionArg : optionArgs)
if (arg.at(0) == arg_char('-'))
{
if (arg == optionArg.shortArgument ||
arg == optionArg.longArgument)
{
optionArg.value = true;
args.erase(args.begin() + i);
i -= 1;
if (arg.at(1) != arg_char('-'))
{ // short option
// strip '-'
arg.erase(0, 1);
for (const auto& optionArg : optionArgs)
{
auto pos = arg.find(optionArg.shortArgument);
if (pos != arg_str_type::npos)
{
optionArg.value = true;
arg.erase(pos, 1);
}
}
}
}

for (const auto& pathArg : pathArgs)
{
if (arg == pathArg.argument)
{
if (i == (args.size() - 1))
else
{ // long option
// strip '--'
arg.erase(0, 2);
for (const auto& optionArg : optionArgs)
{
ShowUsage();
return 1;
if (arg == optionArg.longArgument)
{
optionArg.value = true;
arg.clear();
}
}
pathArg.path = args.at(i + 1);
if (!std::filesystem::is_directory(pathArg.path))
for (const auto& pathArg : pathArgs)
{
ShowUsage();
return 1;
if (arg == pathArg.argument)
{
if (i == (args.size() - 1))
{
ShowUsage();
return 1;
}
pathArg.path = args.at(i + 1);
if (!std::filesystem::is_directory(pathArg.path))
{
ShowUsage();
return 1;
}
args.erase(args.begin() + i + 1);
arg.clear();
}
}
args.erase(args.begin() + i + 1);
args.erase(args.begin() + i);
i -= 1;
}
}

// when all options have been parsed,
// strip the arg and continue
if (arg.empty())
{
args.erase(args.begin() + i);
i -= 1;
}
}

// apply options
Expand Down

0 comments on commit 85a65ff

Please sign in to comment.