Files
OpenRCT2-Web/src/openrct2/command_line/CommandLine.cpp
T

556 lines
17 KiB
C++
Raw Normal View History

/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 22:57:42 +02:00
#include "CommandLine.hpp"
2018-03-14 12:44:16 +01:00
2018-06-22 22:57:42 +02:00
#include "../OpenRCT2.h"
#include "../core/Console.hpp"
#include "../core/String.hpp"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
2018-06-22 22:57:42 +02:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-06-22 22:57:42 +02:00
#include <cstring>
#pragma region CommandLineArgEnumerator
2018-06-22 22:57:42 +02:00
CommandLineArgEnumerator::CommandLineArgEnumerator(const char* const* arguments, int32_t count)
{
_arguments = arguments;
_count = count;
_index = 0;
}
void CommandLineArgEnumerator::Reset()
{
_index = 0;
}
bool CommandLineArgEnumerator::Backtrack()
{
if (_index > 0)
{
_index--;
return true;
}
2021-09-15 22:22:15 +02:00
return false;
}
bool CommandLineArgEnumerator::TryPop()
{
if (_index < _count)
{
_index++;
return true;
}
2021-09-15 22:22:15 +02:00
return false;
}
2018-06-22 22:57:42 +02:00
bool CommandLineArgEnumerator::TryPopInteger(int32_t* result)
{
2018-06-22 22:57:42 +02:00
char const* arg;
if (TryPopString(&arg))
{
2020-04-22 17:09:29 +02:00
*result = static_cast<int32_t>(atol(arg));
return true;
}
2016-11-13 20:17:49 +01:00
return false;
}
2018-06-22 22:57:42 +02:00
bool CommandLineArgEnumerator::TryPopReal(float* result)
{
2018-06-22 22:57:42 +02:00
char const* arg;
if (TryPopString(&arg))
{
2020-04-22 17:09:29 +02:00
*result = static_cast<float>(atof(arg));
return true;
}
2016-11-13 20:17:49 +01:00
return false;
}
2018-06-22 22:57:42 +02:00
bool CommandLineArgEnumerator::TryPopString(const char** result)
{
if (_index < _count)
{
*result = _arguments[_index];
_index++;
return true;
}
2021-09-15 22:22:15 +02:00
return false;
}
#pragma endregion
namespace CommandLine
{
2018-06-22 22:57:42 +02:00
constexpr const char* HelpText = "openrct2 -ha shows help for all commands. "
"openrct2 <command> -h will show help and details for a given command.";
2018-06-22 22:57:42 +02:00
static void PrintHelpFor(const CommandLineCommand* commands);
static void PrintOptions(const CommandLineOptionDefinition* options);
static void PrintExamples(const CommandLineExample* examples);
static utf8* GetOptionCaption(utf8* buffer, size_t bufferSize, const CommandLineOptionDefinition* option);
2018-06-22 22:57:42 +02:00
static const CommandLineOptionDefinition* FindOption(const CommandLineOptionDefinition* options, char shortName);
static const CommandLineOptionDefinition* FindOption(const CommandLineOptionDefinition* options, const char* longName);
2018-06-22 22:57:42 +02:00
static bool ParseShortOption(
const CommandLineOptionDefinition* options, CommandLineArgEnumerator* argEnumerator, const char* argument);
static bool ParseLongOption(
const CommandLineOptionDefinition* options, CommandLineArgEnumerator* argEnumerator, const char* argument);
static bool ParseOptionValue(const CommandLineOptionDefinition* option, const char* valueString);
2018-06-22 22:57:42 +02:00
static bool HandleSpecialArgument(const char* argument);
void PrintHelp(bool allCommands)
{
PrintHelpFor(RootCommands);
PrintExamples(RootExamples);
if (allCommands)
{
2018-06-22 22:57:42 +02:00
for (const CommandLineCommand* command = RootCommands; command->Name != nullptr; command++)
{
if (command->SubCommands != nullptr)
{
size_t commandNameLength = String::LengthOf(command->Name);
for (size_t i = 0; i < commandNameLength; i++)
{
Console::Write("-");
}
Console::WriteLine();
Console::WriteLine(command->Name);
for (size_t i = 0; i < commandNameLength; i++)
{
Console::Write("-");
}
Console::WriteLine();
PrintHelpFor(command->SubCommands);
}
}
}
else
{
Console::WriteLine(HelpText);
}
}
2018-06-22 22:57:42 +02:00
static void PrintHelpFor(const CommandLineCommand* commands)
{
// Print usage
2018-06-22 22:57:42 +02:00
const char* usageString = "usage: openrct2 ";
const size_t usageStringLength = String::LengthOf(usageString);
Console::Write(usageString);
// Get the largest command name length and parameter length
size_t maxNameLength = 0;
size_t maxParamsLength = 0;
2018-06-22 22:57:42 +02:00
const CommandLineCommand* command;
for (command = commands; command->Name != nullptr; command++)
{
2018-06-20 17:11:35 +02:00
maxNameLength = std::max(maxNameLength, String::LengthOf(command->Name));
maxParamsLength = std::max(maxParamsLength, String::LengthOf(command->Parameters));
}
for (command = commands; command->Name != nullptr; command++)
{
if (command != commands)
{
Console::WriteSpace(usageStringLength);
}
Console::Write(command->Name);
Console::WriteSpace(maxNameLength - String::LengthOf(command->Name) + 1);
if (command->SubCommands == nullptr)
{
Console::Write(command->Parameters);
Console::WriteSpace(maxParamsLength - String::LengthOf(command->Parameters));
if (command->Options != nullptr)
{
Console::Write(" [options]");
}
}
else
{
Console::Write("...");
}
Console::WriteLine();
}
Console::WriteLine();
if (commands->Options != nullptr)
{
PrintOptions(commands->Options);
}
}
2018-06-22 22:57:42 +02:00
static void PrintOptions(const CommandLineOptionDefinition* options)
{
// Print options for main commands
size_t maxOptionLength = 0;
2018-06-22 22:57:42 +02:00
const CommandLineOptionDefinition* option = options;
for (; option->Type != 255; option++)
{
char buffer[128];
GetOptionCaption(buffer, sizeof(buffer), option);
size_t optionCaptionLength = String::LengthOf(buffer);
2018-06-20 17:11:35 +02:00
maxOptionLength = std::max(maxOptionLength, optionCaptionLength);
}
option = options;
for (; option->Type != 255; option++)
{
Console::WriteSpace(4);
char buffer[128];
GetOptionCaption(buffer, sizeof(buffer), option);
size_t optionCaptionLength = String::LengthOf(buffer);
Console::Write(buffer);
Console::WriteSpace(maxOptionLength - optionCaptionLength + 4);
Console::Write(option->Description);
Console::WriteLine();
}
Console::WriteLine();
}
2018-06-22 22:57:42 +02:00
static void PrintExamples(const CommandLineExample* examples)
{
size_t maxArgumentsLength = 0;
2018-06-22 22:57:42 +02:00
const CommandLineExample* example;
for (example = examples; example->Arguments != nullptr; example++)
{
size_t argumentsLength = String::LengthOf(example->Arguments);
2018-06-20 17:11:35 +02:00
maxArgumentsLength = std::max(maxArgumentsLength, argumentsLength);
}
Console::WriteLine("examples:");
for (example = examples; example->Arguments != nullptr; example++)
{
Console::Write(" openrct2 ");
Console::Write(example->Arguments);
size_t argumentsLength = String::LengthOf(example->Arguments);
Console::WriteSpace(maxArgumentsLength - argumentsLength + 4);
Console::Write(example->Description);
Console::WriteLine();
}
Console::WriteLine();
}
2018-06-22 22:57:42 +02:00
static utf8* GetOptionCaption(utf8* buffer, size_t bufferSize, const CommandLineOptionDefinition* option)
{
buffer[0] = 0;
if (option->ShortName != '\0')
{
String::AppendFormat(buffer, bufferSize, "-%c, ", option->ShortName);
}
String::Append(buffer, bufferSize, "--");
String::Append(buffer, bufferSize, option->LongName);
2018-06-22 22:57:42 +02:00
switch (option->Type)
{
case CMDLINE_TYPE_INTEGER:
String::Append(buffer, bufferSize, "=<int>");
break;
case CMDLINE_TYPE_REAL:
String::Append(buffer, bufferSize, "=<real>");
break;
case CMDLINE_TYPE_STRING:
String::Append(buffer, bufferSize, "=<str>");
break;
}
return buffer;
}
2018-06-22 22:57:42 +02:00
static const CommandLineCommand* FindCommandFor(const CommandLineCommand* commands, CommandLineArgEnumerator* argEnumerator)
{
// Check if end of arguments or options have started
2018-06-22 22:57:42 +02:00
const char* firstArgument;
if (!argEnumerator->TryPopString(&firstArgument))
{
return commands;
}
if (firstArgument[0] == '-')
{
argEnumerator->Backtrack();
return commands;
}
// Search through defined commands for one that matches
2018-06-22 22:57:42 +02:00
const CommandLineCommand* fallback = nullptr;
const CommandLineCommand* command = commands;
for (; command->Name != nullptr; command++)
{
if (command->Name[0] == '\0')
{
// If we don't find a command, this should be used
fallback = command;
}
else if (String::Equals(command->Name, firstArgument))
{
if (command->SubCommands == nullptr)
{
// Found matching command
return command;
}
2021-09-15 22:22:15 +02:00
// Recurse for the sub command table
return FindCommandFor(command->SubCommands, argEnumerator);
}
}
argEnumerator->Backtrack();
return fallback;
}
2018-06-22 22:57:42 +02:00
static bool ParseOptions(const CommandLineOptionDefinition* options, CommandLineArgEnumerator* argEnumerator)
{
bool firstOption = true;
2018-06-22 22:57:42 +02:00
const char* argument;
while (argEnumerator->TryPopString(&argument))
{
if (HandleSpecialArgument(argument))
{
continue;
}
if (argument[0] == '-')
{
if (argument[1] == '-')
{
if (!ParseLongOption(options, argEnumerator, &argument[2]))
{
return false;
}
}
else
{
if (!ParseShortOption(options, argEnumerator, argument))
{
return false;
}
}
2016-01-21 18:18:44 +00:00
firstOption = false;
}
else if (!firstOption)
{
2016-01-11 21:49:13 +00:00
Console::Error::WriteLine("All options must be passed at the end of the command line.");
return false;
}
2021-09-15 22:22:15 +02:00
continue;
}
return true;
}
2018-06-22 22:57:42 +02:00
static bool ParseLongOption(
const CommandLineOptionDefinition* options, CommandLineArgEnumerator* argEnumerator, const char* argument)
{
// Get just the option name
char optionName[64];
2018-06-22 22:57:42 +02:00
const char* equalsCh = strchr(argument, '=');
if (equalsCh != nullptr)
{
String::Set(optionName, sizeof(optionName), argument, equalsCh - argument);
}
else
{
String::Set(optionName, sizeof(optionName), argument);
}
// Find a matching option definition
2018-06-22 22:57:42 +02:00
const CommandLineOptionDefinition* option = FindOption(options, optionName);
if (option == nullptr)
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("Unknown option: --%s", optionName);
return false;
}
if (equalsCh == nullptr)
{
if (option->Type == CMDLINE_TYPE_SWITCH)
{
ParseOptionValue(option, nullptr);
}
else
{
2018-06-22 22:57:42 +02:00
const char* valueString = nullptr;
if (!argEnumerator->TryPopString(&valueString))
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("Expected value for option: %s", optionName);
return false;
}
2016-11-13 20:17:49 +01:00
if (!ParseOptionValue(option, valueString))
{
return false;
}
}
}
else
{
if (option->Type == CMDLINE_TYPE_SWITCH)
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("Option is a switch: %s", optionName);
return false;
}
2021-09-15 22:22:15 +02:00
if (!ParseOptionValue(option, equalsCh + 1))
{
2021-09-15 22:22:15 +02:00
return false;
}
}
return true;
}
2018-06-22 22:57:42 +02:00
static bool ParseShortOption(
const CommandLineOptionDefinition* options, CommandLineArgEnumerator* argEnumerator, const char* argument)
{
2018-06-22 22:57:42 +02:00
const CommandLineOptionDefinition* option = nullptr;
2018-06-22 22:57:42 +02:00
const char* shortOption = &argument[1];
for (; *shortOption != '\0'; shortOption++)
{
option = FindOption(options, shortOption[0]);
if (option == nullptr)
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("Unknown option: -%c", shortOption[0]);
return false;
}
if (option->Type == CMDLINE_TYPE_SWITCH)
{
if (!ParseOptionValue(option, nullptr))
{
return false;
}
}
else if (shortOption[1] != '\0')
{
2017-12-03 23:45:43 +01:00
return ParseOptionValue(option, &shortOption[1]);
}
}
if (option != nullptr && option->Type != CMDLINE_TYPE_SWITCH)
{
2018-06-22 22:57:42 +02:00
const char* valueString = nullptr;
if (!argEnumerator->TryPopString(&valueString))
{
2016-07-09 15:02:05 +01:00
Console::Error::WriteLine("Expected value for option: %c", option->ShortName);
return false;
}
2016-11-13 20:17:49 +01:00
if (!ParseOptionValue(option, valueString))
{
return false;
}
}
return true;
}
2018-06-22 22:57:42 +02:00
static bool ParseOptionValue(const CommandLineOptionDefinition* option, const char* valueString)
{
2018-06-22 22:57:42 +02:00
if (option->OutAddress == nullptr)
return true;
2018-06-22 22:57:42 +02:00
switch (option->Type)
{
case CMDLINE_TYPE_SWITCH:
2020-04-22 17:09:29 +02:00
*(static_cast<bool*>(option->OutAddress)) = true;
2018-06-22 22:57:42 +02:00
return true;
case CMDLINE_TYPE_INTEGER:
2020-04-22 17:09:29 +02:00
*(static_cast<int32_t*>(option->OutAddress)) = static_cast<int32_t>(atol(valueString));
2018-06-22 22:57:42 +02:00
return true;
case CMDLINE_TYPE_REAL:
2020-04-22 17:09:29 +02:00
*(static_cast<float*>(option->OutAddress)) = static_cast<float>(atof(valueString));
2018-06-22 22:57:42 +02:00
return true;
case CMDLINE_TYPE_STRING:
2022-01-27 14:21:46 +01:00
*(static_cast<u8string*>(option->OutAddress)) = u8string(valueString);
2018-06-22 22:57:42 +02:00
return true;
default:
Console::Error::WriteLine("Unknown CMDLINE_TYPE for: %s", option->LongName);
return false;
}
}
2018-06-22 22:57:42 +02:00
static bool HandleSpecialArgument([[maybe_unused]] const char* argument)
{
return Platform::HandleSpecialCommandLineArgument(argument);
}
2018-06-22 22:57:42 +02:00
const CommandLineOptionDefinition* FindOption(const CommandLineOptionDefinition* options, char shortName)
{
2018-06-22 22:57:42 +02:00
for (const CommandLineOptionDefinition* option = options; option->Type != 255; option++)
{
if (option->ShortName == shortName)
{
return option;
}
}
return nullptr;
}
2018-06-22 22:57:42 +02:00
const CommandLineOptionDefinition* FindOption(const CommandLineOptionDefinition* options, const char* longName)
{
2018-06-22 22:57:42 +02:00
for (const CommandLineOptionDefinition* option = options; option->Type != 255; option++)
{
if (String::Equals(option->LongName, longName))
{
return option;
}
}
return nullptr;
}
2018-05-04 22:40:09 +02:00
} // namespace CommandLine
int32_t CommandLineRun(const char** argv, int32_t argc)
{
2018-02-01 18:49:14 +01:00
auto argEnumerator = CommandLineArgEnumerator(argv, argc);
// Pop process path
argEnumerator.TryPop();
2018-06-22 22:57:42 +02:00
const CommandLineCommand* command = CommandLine::FindCommandFor(CommandLine::RootCommands, &argEnumerator);
2018-02-01 18:49:14 +01:00
if (command == nullptr)
{
2018-02-01 18:49:14 +01:00
return EXITCODE_FAIL;
}
2018-02-01 18:49:14 +01:00
if (command->Options != nullptr)
{
auto argEnumeratorForOptions = CommandLineArgEnumerator(argEnumerator);
if (!CommandLine::ParseOptions(command->Options, &argEnumeratorForOptions))
2017-01-14 14:57:44 +00:00
{
2017-01-25 17:48:00 +01:00
return EXITCODE_FAIL;
2017-01-14 14:57:44 +00:00
}
2018-02-01 18:49:14 +01:00
}
2018-02-01 18:49:14 +01:00
if (command == CommandLine::RootCommands && command->Func == nullptr)
{
return CommandLine::HandleCommandDefault();
}
2021-09-15 22:22:15 +02:00
return command->Func(&argEnumerator);
}