diff --git a/src/ModMan/ModManArgs.cpp b/src/ModMan/ModManArgs.cpp new file mode 100644 index 00000000..b5b1f88c --- /dev/null +++ b/src/ModMan/ModManArgs.cpp @@ -0,0 +1,105 @@ +#include "ModManArgs.h" + +#include "GitVersion.h" +#include "Utils/Arguments/UsageInformation.h" +#include "Utils/Logging/Log.h" + +#include +#include + +namespace +{ + // clang-format off + const CommandLineOption* const OPTION_HELP = + CommandLineOption::Builder::Create() + .WithShortName("?") + .WithLongName("help") + .WithDescription("Displays usage information.") + .Build(); + + const CommandLineOption* const OPTION_VERSION = + CommandLineOption::Builder::Create() + .WithLongName("version") + .WithDescription("Prints the application version.") + .Build(); + + const CommandLineOption* const OPTION_VERBOSE = + CommandLineOption::Builder::Create() + .WithShortName("v") + .WithLongName("verbose") + .WithDescription("Outputs a lot more and more detailed messages.") + .Build(); + + const CommandLineOption* const OPTION_NO_COLOR = + CommandLineOption::Builder::Create() + .WithLongName("no-color") + .WithDescription("Disables colored terminal output.") + .Build(); + // clang-format on + + const CommandLineOption* const COMMAND_LINE_OPTIONS[]{ + OPTION_HELP, + OPTION_VERSION, + OPTION_VERBOSE, + OPTION_NO_COLOR, + }; +} // namespace + +ModManArgs::ModManArgs() + : m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v) +{ +} + +void ModManArgs::PrintUsage() const +{ + UsageInformation usage(m_argument_parser.GetExecutableName()); + + for (const auto* commandLineOption : COMMAND_LINE_OPTIONS) + { + usage.AddCommandLineOption(commandLineOption); + } + + usage.Print(); +} + +void ModManArgs::PrintVersion() +{ + con::info("OpenAssetTools ModMan {}", GIT_VERSION); +} + +bool ModManArgs::ParseArgs(const int argc, const char** argv, bool& shouldContinue) +{ + shouldContinue = true; + if (!m_argument_parser.ParseArguments(argc, argv)) + { + PrintUsage(); + return false; + } + + // Check if the user requested help + if (m_argument_parser.IsOptionSpecified(OPTION_HELP)) + { + PrintUsage(); + shouldContinue = false; + return true; + } + + // Check if the user wants to see the version + if (m_argument_parser.IsOptionSpecified(OPTION_VERSION)) + { + PrintVersion(); + shouldContinue = false; + return true; + } + + // -v; --verbose + if (m_argument_parser.IsOptionSpecified(OPTION_VERBOSE)) + con::globalLogLevel = con::LogLevel::DEBUG; + else + con::globalLogLevel = con::LogLevel::INFO; + + // --no-color + con::globalUseColor = !m_argument_parser.IsOptionSpecified(OPTION_NO_COLOR); + + return true; +} diff --git a/src/ModMan/ModManArgs.h b/src/ModMan/ModManArgs.h new file mode 100644 index 00000000..feb19628 --- /dev/null +++ b/src/ModMan/ModManArgs.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Utils/Arguments/ArgumentParser.h" + +class ModManArgs +{ +public: + ModManArgs(); + bool ParseArgs(int argc, const char** argv, bool& shouldContinue); + +private: + /** + * \brief Prints a command line usage help text for the ModMan tool to stdout. + */ + void PrintUsage() const; + static void PrintVersion(); + + ArgumentParser m_argument_parser; +}; diff --git a/src/ModMan/main.cpp b/src/ModMan/main.cpp index 5c34be42..4b4f69ae 100644 --- a/src/ModMan/main.cpp +++ b/src/ModMan/main.cpp @@ -1,5 +1,6 @@ #include "Context/ModManContext.h" #include "GitVersion.h" +#include "ModManArgs.h" #include "Web/Binds/Binds.h" #include "Web/Platform/AssetHandler.h" #include "Web/UiCommunication.h" @@ -94,9 +95,13 @@ namespace } // namespace #ifdef _WIN32 +#define MODMAN_ARGC __argc +#define MODMAN_ARGV const_cast(__argv) int WINAPI WinMain(HINSTANCE /*hInst*/, HINSTANCE /*hPrevInst*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/) #else -int main() +#define MODMAN_ARGC argc +#define MODMAN_ARGV argv +int main(int argc, const char** argv) #endif { #ifdef _WIN32 @@ -114,6 +119,14 @@ int main() } #endif + ModManArgs args; + auto shouldContinue = true; + if (!args.ParseArgs(MODMAN_ARGC, MODMAN_ARGV, shouldContinue)) + return false; + + if (!shouldContinue) + return true; + con::info("Starting ModMan " GIT_VERSION); ModManContext::Get().Startup();