summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAECX <aecx@aecx.cc>2019-09-03 01:44:44 +0200
committerAECX <aecx@aecx.cc>2019-09-03 01:44:44 +0200
commitfc82b571084ddd724a5bb05c86b638442f7c2ede (patch)
tree6a78f59d7239b6e3108d7aa7638d7d8e33e60db9
parent08b1e6aa2f0c20266106b57c0a71e7f38bd49c93 (diff)
Argument parsing into Settings struct
-rw-r--r--CMakeLists.txt8
-rw-r--r--include/cxxopts.h4
-rw-r--r--include/settings.h27
-rw-r--r--source/Twilight Editor.cpp115
4 files changed, 135 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ae85c69..91c1933 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,7 @@
´╗┐cmake_minimum_required (VERSION 3.8)
+set (CMAKE_CXX_STANDARD 17)
+
project (TwilightEditor)
set (TE_VERSION_MAIN 3)
@@ -25,4 +27,8 @@ list(REMOVE_DUPLICATES TE_INCLUDE_DIRS)
add_executable (TwilightEditor ${TE_SOURCES})
-target_include_directories(TwilightEditor PRIVATE ${TE_INCLUDE_DIRS}) \ No newline at end of file
+target_include_directories(TwilightEditor PRIVATE ${TE_INCLUDE_DIRS})
+
+if (CMAKE_CXX_COMPILER_ID STREQUAL GNU)
+ target_link_libraries(TwilightEditor stdc++fs)
+endif() \ No newline at end of file
diff --git a/include/cxxopts.h b/include/cxxopts.h
index 7536cea..89354c1 100644
--- a/include/cxxopts.h
+++ b/include/cxxopts.h
@@ -1293,7 +1293,7 @@ namespace cxxopts
Options(std::string program, std::string help_string = "")
: m_program(std::move(program))
, m_help_string(toLocalString(std::move(help_string)))
- , m_custom_help("[OPTION...]")
+ , m_custom_help("--fix -i GZ2E01.gci --offset 0xB --length 0x1 --type int --value \"0xFF\"")
, m_positional_help("positional parameters")
, m_show_positional(false)
, m_allow_unrecognised(false)
@@ -2156,7 +2156,7 @@ namespace cxxopts
std::string
Options::help(const std::vector<std::string>& help_groups) const
{
- String result = m_help_string + "\nUsage:\n " +
+ String result = m_help_string + "\nExample Usage:\n " +
toLocalString(m_program) + " " + toLocalString(m_custom_help);
if (m_positional.size() > 0 && m_positional_help.size() > 0) {
diff --git a/include/settings.h b/include/settings.h
index ac2878b..43d2e5b 100644
--- a/include/settings.h
+++ b/include/settings.h
@@ -5,9 +5,36 @@
namespace TwilightEditor
{
+ enum mode
+ {
+ GET,
+ SET
+ };
+
+ enum offsetType
+ {
+ TPINT,
+ TPFLAG,
+ TPSTRING
+ };
+
struct settings
{
+ // General
std::string INPUT_FILE;
std::string OUTPUT_FILE;
+ mode MODE;
+ uint16_t QUESTLOG;
+ bool FIX;
+
+ // Offset
+ uint16_t OFFSET;
+ uint16_t LENGTH;
+ offsetType TYPE;
+ std::string VALUE;
+
+ // Additional
+ bool VERBOSE;
+ bool FORCE;
};
} \ No newline at end of file
diff --git a/source/Twilight Editor.cpp b/source/Twilight Editor.cpp
index 1ee179c..ac072e9 100644
--- a/source/Twilight Editor.cpp
+++ b/source/Twilight Editor.cpp
@@ -3,6 +3,11 @@
#include "cxxopts.h"
#include "config.h"
+#include <filesystem>
+
+namespace fs = std::filesystem;
+namespace TE = TwilightEditor;
+
namespace TwilightEditor
{
settings Settings;
@@ -12,23 +17,25 @@ cxxopts::ParseResult parse(int argc, char* argv[])
{
try
{
- cxxopts::Options options(argv[0], "Twilight Editor | (C) @theAECX");
+ cxxopts::Options options(argv[0], "Twilight Editor | (C) @theAECX | *required");
options.add_options("General")
- ("h,help", "Prints this help")
- ("i,input", "Path to input file", cxxopts::value<std::string>(), "[path/to/file]")
- ("o,output", "If empty: Override input file", cxxopts::value<std::string>(), "[path/to/file]")
- ("q,quest-log", "QuestLog Index (0 based)", cxxopts::value<std::uint8_t>()->default_value("0"), "[n]")
- ("m,mode", "Specifies whether to Read or Write", cxxopts::value<std::string>(), "[GET|SET]")
- ("f,fix", "Correct checksums?", cxxopts::value<bool>()->default_value("true"));
+ ("h,help", " Prints this help")
+ ("i,input", "*(string) Path to input file", cxxopts::value<std::string>(), "<path/to/file>")
+ ("m,mode", " (string) <GET> or <SET>", cxxopts::value<std::string>()->default_value("GET"), "<GET|SET>")
+ ("o,output", " (string) If empty: Override input file", cxxopts::value<std::string>(), "<path/to/file>")
+ ("q,questlog", " (int) QuestLog Index (0 based)", cxxopts::value<std::uint16_t>()->default_value("0"), "<n>")
+ ("fix", " (bool) Correct checksum?", cxxopts::value<bool>()->default_value("false"));
options.add_options("Offset")
- ("offset", "Hexadecimal QuestLog offset", cxxopts::value<std::string>(), "[0x000]")
- ("length", "Hexadecimal Length", cxxopts::value<std::string>(), "[0x00]")
- ("type", "Value type ('tpflag', 'tpint', 'tpstring')", cxxopts::value<std::string>(), "[tpflag|tpint|tpstring]");
+ ("offset", "*(hex/dec) Hexadecimal QuestLog offset", cxxopts::value<uint16_t>(), "<0x000>")
+ ("length", "*(hex/dec) Hexadecimal Length", cxxopts::value<uint16_t>(), "<0x00>")
+ ("type", "*(string) Value type/interpretation", cxxopts::value<std::string>(), "<int|flg|str>")
+ ("value", "*(string) The value you want to apply", cxxopts::value<std::string>(), "<\"Link\">");
options.add_options("Additional")
- ("v,verbose", "Print additional output information", cxxopts::value<bool>());
+ ("v,verbose", " Print additional output information", cxxopts::value<bool>()->default_value("false"))
+ ("f,force", " Don't ask for file overrides", cxxopts::value<bool>()->default_value("false"));
auto result = options.parse(argc, argv);
@@ -38,17 +45,79 @@ cxxopts::ParseResult parse(int argc, char* argv[])
exit(0);
}
+ // Set values to global settings struct
if (result.count("input"))
{
- TwilightEditor::Settings.INPUT_FILE = result["input"].as<std::string>();
+ TE::Settings.INPUT_FILE = result["input"].as<std::string>();
+ }
+ else
+ {
+ throw cxxopts::OptionException("Missing parameter --input");
}
- return result;
+ TE::Settings.MODE = (result["mode"].as<std::string>() == "GET" ? TE::mode::GET : TE::mode::SET);
+
+ if (result.count("output"))
+ {
+ TE::Settings.OUTPUT_FILE = result["output"].as<std::string>();
+ }
+ else
+ {
+ // Overwrite original
+ TE::Settings.OUTPUT_FILE = TE::Settings.INPUT_FILE;
+ }
+ TE::Settings.QUESTLOG = result["questlog"].as<std::uint16_t>();
+ if (TE::Settings.QUESTLOG > 2)
+ {
+ throw cxxopts::OptionException("Invalid QuestLog Index");
+ }
+
+ TE::Settings.FIX = result["fix"].as<bool>();
+
+ if (result.count("offset"))
+ {
+ TE::Settings.OFFSET = result["offset"].as<uint16_t>();
+ }
+ else
+ {
+ throw cxxopts::OptionException("Missing parameter --offset");
+ }
+
+ if (result.count("length"))
+ {
+ TE::Settings.LENGTH = result["length"].as<uint16_t>();
+ }
+ else
+ {
+ throw cxxopts::OptionException("Missing parameter --length");
+ }
+
+ if (result.count("type"))
+ {
+ TE::Settings.TYPE = (result["type"].as<std::string>() == "int" ? TE::offsetType::TPINT : result["type"].as<std::string>() == "flg" ? TE::offsetType::TPFLAG : TE::offsetType::TPSTRING);
+ }
+
+ if (result.count("value"))
+ {
+ TE::Settings.VALUE = result["value"].as<std::string>();
+ }
+ else
+ {
+ throw cxxopts::OptionException("Missing parameter --value");
+ }
+
+ // -v, --verbose
+ TE::Settings.VERBOSE = result["verbose"].as<bool>();
+
+ // -f, --force
+ TE::Settings.FORCE = result["force"].as<bool>();
+
+ return result;
}
catch (const cxxopts::OptionException& e)
{
- std::cout << "error parsing options: " << e.what() << std::endl;
+ std::cout << "error parsing options: " << e.what() << std::endl << "Maybe try --help" << std::endl;
exit(1);
}
}
@@ -57,8 +126,22 @@ int main(int argc, char* argv[])
{
auto result = parse(argc, argv);
auto arguments = result.arguments();
-
- std::cout << TwilightEditor::Settings.INPUT_FILE << std::endl;
+
+ std::cout << "Results:" << std::endl;
+ std::cout << "Input File: " << TE::Settings.INPUT_FILE << std::endl;
+ std::cout << "Output File: " << TE::Settings.OUTPUT_FILE << std::endl;
+ std::cout << "Mode: " << TE::Settings.MODE << std::endl;
+ std::cout << "QuestLog: " << TE::Settings.QUESTLOG << std::endl;
+ std::cout << "Fix checksum: " << TE::Settings.FIX << std::endl;
+
+
+ std::cout << "Offset: " << TE::Settings.OFFSET << std::endl;
+ std::cout << "Length: " << TE::Settings.LENGTH << std::endl;
+ std::cout << "Type: " << TE::Settings.TYPE << std::endl;
+ std::cout << "Value: " << TE::Settings.VALUE << std::endl;
+
+ std::cout << "Verbose: " << TE::Settings.VERBOSE << std::endl;
+ std::cout << "Force: " << TE::Settings.FORCE << std::endl;
getchar();