Skip to content

Commit

Permalink
Merge pull request #97 from Laupetin/feature/version-cmd-arg
Browse files Browse the repository at this point in the history
feat: add version command line arg
  • Loading branch information
Laupetin authored Jan 23, 2024
2 parents 398edc5 + 0b0b888 commit 87917ca
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 21 deletions.
8 changes: 8 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include "tools/scripts/including.lua"
include "tools/scripts/linking.lua"
include "tools/scripts/options.lua"
include "tools/scripts/platform.lua"
include "tools/scripts/version.lua"

-- ==================
-- Workspace
Expand Down Expand Up @@ -63,6 +64,13 @@ workspace "OpenAssetTools"
"__STDC_WANT_LIB_EXT1__=1",
"_CRT_SECURE_NO_WARNINGS"
}

-- Write the current version to a header
-- This is better than adding it as macro here since changing a global macro would cause a full rebuild
WriteVersionHeader()
includedirs {
GetVersionHeaderFolder()
}

filter "options:debug-structureddatadef"
defines { "STRUCTUREDDATADEF_DEBUG" }
Expand Down
6 changes: 5 additions & 1 deletion src/Linker/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,13 @@ class LinkerImpl final : public Linker

bool Start(const int argc, const char** argv) override
{
if (!m_args.ParseArgs(argc, argv))
auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false;

if (!shouldContinue)
return true;

if (!m_search_paths.BuildProjectIndependentSearchPaths())
return false;

Expand Down
30 changes: 27 additions & 3 deletions src/Linker/LinkerArgs.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "LinkerArgs.h"

#include "GitVersion.h"
#include "ObjLoading.h"
#include "ObjWriting.h"
#include "Utils/Arguments/UsageInformation.h"
#include "Utils/FileUtils.h"

#include <filesystem>
#include <iostream>
#include <regex>
#include <type_traits>

Expand All @@ -19,6 +21,12 @@ const CommandLineOption* const OPTION_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")
Expand Down Expand Up @@ -88,6 +96,7 @@ const CommandLineOption* const OPTION_MENU_NO_OPTIMIZATION =

const CommandLineOption* const COMMAND_LINE_OPTIONS[]{
OPTION_HELP,
OPTION_VERSION,
OPTION_VERBOSE,
OPTION_BASE_FOLDER,
OPTION_OUTPUT_FOLDER,
Expand All @@ -100,7 +109,7 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[]{
};

LinkerArgs::LinkerArgs()
: m_argument_parser(COMMAND_LINE_OPTIONS, std::extent<decltype(COMMAND_LINE_OPTIONS)>::value),
: m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v<decltype(COMMAND_LINE_OPTIONS)>),
m_base_pattern(R"(\?base\?)"),
m_game_pattern(R"(\?game\?)"),
m_project_pattern(R"(\?project\?)"),
Expand All @@ -125,6 +134,11 @@ void LinkerArgs::PrintUsage()
usage.Print();
}

void LinkerArgs::PrintVersion()
{
std::cout << "OpenAssetTools Linker " << std::string(GIT_VERSION) << "\n";
}

void LinkerArgs::SetVerbose(const bool isVerbose)
{
m_verbose = isVerbose;
Expand Down Expand Up @@ -198,8 +212,9 @@ std::set<std::string> LinkerArgs::GetSearchPathsForProject(const std::set<std::s
return out;
}

bool LinkerArgs::ParseArgs(const int argc, const char** argv)
bool LinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContinue)
{
shouldContinue = true;
if (!m_argument_parser.ParseArguments(argc - 1, &argv[1]))
{
PrintUsage();
Expand All @@ -210,7 +225,16 @@ bool LinkerArgs::ParseArgs(const int argc, const char** argv)
if (m_argument_parser.IsOptionSpecified(OPTION_HELP))
{
PrintUsage();
return false;
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;
}

m_project_specifiers_to_build = m_argument_parser.GetArguments();
Expand Down
3 changes: 2 additions & 1 deletion src/Linker/LinkerArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LinkerArgs
* \brief Prints a command line usage help text for the Linker tool to stdout.
*/
static void PrintUsage();
static void PrintVersion();

void SetVerbose(bool isVerbose);

Expand All @@ -56,7 +57,7 @@ class LinkerArgs
bool m_verbose;

LinkerArgs();
bool ParseArgs(int argc, const char** argv);
bool ParseArgs(int argc, const char** argv, bool& shouldContinue);

/**
* \brief Converts the output path specified by command line arguments to a path applies for the specified project.
Expand Down
6 changes: 5 additions & 1 deletion src/RawTemplater/RawTemplater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ class RawTemplater::Impl

int Run(const int argc, const char** argv)
{
if (!m_args.Parse(argc, argv))
auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue))
return 1;

if (!shouldContinue)
return 0;

if (!m_args.m_build_log_file.empty())
{
fs::path p = fs::path(m_args.m_build_log_file).parent_path();
Expand Down
35 changes: 32 additions & 3 deletions src/RawTemplater/RawTemplaterArguments.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include "RawTemplaterArguments.h"

#include "GitVersion.h"
#include "Utils/Arguments/CommandLineOption.h"
#include "Utils/Arguments/UsageInformation.h"

#include <iostream>
#include <type_traits>

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();

Expand All @@ -30,7 +37,14 @@ const CommandLineOption* const OPTION_DEFINE = CommandLineOption::Builder::Creat
.Reusable()
.Build();

const CommandLineOption* const COMMAND_LINE_OPTIONS[]{OPTION_HELP, OPTION_VERBOSE, OPTION_OUTPUT_FOLDER, OPTION_BUILD_LOG, OPTION_DEFINE};
const CommandLineOption* const COMMAND_LINE_OPTIONS[]{
OPTION_HELP,
OPTION_VERSION,
OPTION_VERBOSE,
OPTION_OUTPUT_FOLDER,
OPTION_BUILD_LOG,
OPTION_DEFINE,
};

RawTemplaterArguments::RawTemplaterArguments()
: m_argument_parser(COMMAND_LINE_OPTIONS, std::extent_v<decltype(COMMAND_LINE_OPTIONS)>),
Expand All @@ -50,8 +64,14 @@ void RawTemplaterArguments::PrintUsage()
usage.Print();
}

bool RawTemplaterArguments::Parse(const int argc, const char** argv)
void RawTemplaterArguments::PrintVersion()
{
std::cout << "OpenAssetTools RawTemplater " << std::string(GIT_VERSION) << "\n";
}

bool RawTemplaterArguments::ParseArgs(const int argc, const char** argv, bool& shouldContinue)
{
shouldContinue = true;
if (!m_argument_parser.ParseArguments(argc - 1, &argv[1]))
{
PrintUsage();
Expand All @@ -62,7 +82,16 @@ bool RawTemplaterArguments::Parse(const int argc, const char** argv)
if (m_argument_parser.IsOptionSpecified(OPTION_HELP))
{
PrintUsage();
return false;
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;
}

m_input_files = m_argument_parser.GetArguments();
Expand Down
3 changes: 2 additions & 1 deletion src/RawTemplater/RawTemplaterArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RawTemplaterArguments
* \brief Prints a command line usage help text for the RawTemplater tool to stdout.
*/
static void PrintUsage();
static void PrintVersion();

public:
bool m_verbose;
Expand All @@ -27,5 +28,5 @@ class RawTemplaterArguments

RawTemplaterArguments();

bool Parse(int argc, const char** argv);
bool ParseArgs(int argc, const char** argv, bool& shouldContinue);
};
6 changes: 5 additions & 1 deletion src/Unlinker/Unlinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,13 @@ class Unlinker::Impl
*/
bool Start(const int argc, const char** argv)
{
if (!m_args.ParseArgs(argc, argv))
auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue))
return false;

if (!shouldContinue)
return true;

if (!BuildSearchPaths())
return false;

Expand Down
28 changes: 26 additions & 2 deletions src/Unlinker/UnlinkerArgs.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "UnlinkerArgs.h"

#include "GitVersion.h"
#include "ObjLoading.h"
#include "ObjWriting.h"
#include "Utils/Arguments/UsageInformation.h"
#include "Utils/FileUtils.h"

#include <iostream>
#include <regex>
#include <type_traits>

Expand All @@ -16,6 +18,12 @@ const CommandLineOption* const OPTION_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")
Expand Down Expand Up @@ -113,6 +121,7 @@ const CommandLineOption* const OPTION_LEGACY_MENUS =

const CommandLineOption* const COMMAND_LINE_OPTIONS[]{
OPTION_HELP,
OPTION_VERSION,
OPTION_VERBOSE,
OPTION_MINIMAL_ZONE_FILE,
OPTION_LOAD,
Expand Down Expand Up @@ -155,6 +164,11 @@ void UnlinkerArgs::PrintUsage()
usage.Print();
}

void UnlinkerArgs::PrintVersion()
{
std::cout << "OpenAssetTools Unlinker " << std::string(GIT_VERSION) << "\n";
}

void UnlinkerArgs::SetVerbose(const bool isVerbose)
{
m_verbose = isVerbose;
Expand Down Expand Up @@ -237,8 +251,9 @@ void UnlinkerArgs::ParseCommaSeparatedAssetTypeString(const std::string& input)
AddSpecifiedAssetType(std::string(lowerInput, currentPos, lowerInput.size() - currentPos));
}

bool UnlinkerArgs::ParseArgs(const int argc, const char** argv)
bool UnlinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContinue)
{
shouldContinue = true;
if (!m_argument_parser.ParseArguments(argc - 1, &argv[1]))
{
PrintUsage();
Expand All @@ -249,7 +264,16 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv)
if (m_argument_parser.IsOptionSpecified(OPTION_HELP))
{
PrintUsage();
return false;
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;
}

m_zones_to_unlink = m_argument_parser.GetArguments();
Expand Down
3 changes: 2 additions & 1 deletion src/Unlinker/UnlinkerArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class UnlinkerArgs
* \brief Prints a command line usage help text for the Unlinker tool to stdout.
*/
static void PrintUsage();
static void PrintVersion();

void SetVerbose(bool isVerbose);
bool SetImageDumpingMode();
Expand Down Expand Up @@ -60,7 +61,7 @@ class UnlinkerArgs
bool m_verbose;

UnlinkerArgs();
bool ParseArgs(int argc, const char** argv);
bool ParseArgs(int argc, const char** argv, bool& shouldContinue);

/**
* \brief Converts the output path specified by command line arguments to a path applies for the specified zone.
Expand Down
6 changes: 5 additions & 1 deletion src/ZoneCodeGeneratorLib/ZoneCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ class ZoneCodeGenerator::Impl

int Run(const int argc, const char** argv)
{
if (!m_args.Parse(argc, argv))
auto shouldContinue = true;
if (!m_args.ParseArgs(argc, argv, shouldContinue))
return 1;

if (!shouldContinue)
return 0;

if (!ReadHeaderData() || !ReadCommandsData())
return 1;

Expand Down
Loading

0 comments on commit 87917ca

Please sign in to comment.