CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBCommandLine.cpp
Go to the documentation of this file.
1 #include "CYB.hpp"
3 
4 CYB::Engine::Helpers::CommandLine::CommandLine(const unsigned int ANumArguments, const oschar_t* const* const AArguments) :
5  FTokens(ParseTokens(ANumArguments, AArguments))
6 {}
7 
8 CYB::API::Container::Vector<CYB::Engine::Helpers::CommandLine::Token> CYB::Engine::Helpers::CommandLine::ParseTokens(const unsigned int ANumArguments, const oschar_t* const* const AArguments) {
10  Result.reserve(ANumArguments);
11 
12  //Make no assumptions about argv[0]
13  for (auto I(0U); I < ANumArguments; ++I) {
15 #ifdef TARGET_OS_WINDOWS //Do this nowhere else in the engine tree
16  API::String::UTF16::ToUTF8(AArguments[I])
17 #else
18  AArguments[I]
19 #endif
20  );
21  TokenType TT;
22  if (AsUTF.RawLength() > 0) {
23  if (AsUTF.CString()[0] == '-') {
24  if (AsUTF.RawLength() > 1 && AsUTF.CString()[1] == '-') {
25  TT = TokenType::EXTENDED_KEY;
26  AsUTF = AsUTF.SubString(2, -1);
27  }
28  else {
29  TT = TokenType::SINGLE_LETTER_KEY;
30  AsUTF = AsUTF.SubString(1, -1);
31  }
32  }
33  else if (AsUTF.CString()[0] == '/') {
34  TT = TokenType::SINGLE_LETTER_KEY;
35  AsUTF = AsUTF.SubString(1, -1);
36  }
37  else
38  TT = TokenType::NORMAL;
39  Result.emplace_back(Token{ std::move(AsUTF), TT });
40  }
41  }
42 
43  return Result;
44 }
45 
46 void CYB::Engine::Helpers::CommandLine::RunHandler(Callback ACallback, const int AFullNameKey, const int ADescriptionKey, const API::String::CStyle& AShortFlag, const API::String::CStyle& ALongFlag, const unsigned int ANumExpectedTokens, const unsigned int ANumOptionalTokens, unsigned long long AMaxInvocations) const {
48  API::String::Dynamic NotEnoughParams(u8"Not enough parameters for command: ");
49  for (auto I(FTokens.begin()); AMaxInvocations > 0 && I != FTokens.end(); ++I) {
50  if ((I->FType == TokenType::SINGLE_LETTER_KEY && I->FEntry == AShortFlag)
51  || (I->FType == TokenType::EXTENDED_KEY && I->FEntry == ALongFlag)) {
52  auto Remaining(ANumExpectedTokens);
53  //Make sure we have enough normal tokens to fill requirements
54  auto J(I + 1);
55  for (; Remaining > 0 && J != FTokens.end() && J->FType == TokenType::NORMAL; ++J, --Remaining)
56  Work.emplace_back(&(J->FEntry));
57  if (Remaining == 0) {
58  //found all required, check for optional
59  Remaining = ANumOptionalTokens;
60  for (; Remaining > 0 && J != FTokens.end() && J->FType == TokenType::NORMAL; ++J, --Remaining)
61  Work.emplace_back(&(J->FEntry));
62  if (!ACallback(Work))
63  Platform::System::Process::GetSelf().Terminate(); //Stop immediately, no consideration
64  --AMaxInvocations;
65  }
66  else
67  API::Context().FLogger.Log(NotEnoughParams + I->FEntry, API::Logger::Level::WARN);
68  Work.clear();
69  }
70  }
71  static_cast<void>(AFullNameKey);
72  static_cast<void>(ADescriptionKey);
73 }
void RunHandler(Callback ACallback, const int AFullNameKey, const int ADescriptionKey, const API::String::CStyle &AShortFlag, const API::String::CStyle &ALongFlag, const unsigned int ANumExpectedTokens, const unsigned int ANumOptionalTokens, unsigned long long AMaxInvocations) const finaloverride
Adds a command line flag handler.
API::Interop::Context & Context(void) noexcept
Get the API's Context.
TokenType
Recognized token variations.
std::add_pointer< bool(const API::Container::Deque< const API::String::Dynamic * > &AParameters)>::type Callback
Called to handle a valid command line parameters.
Definition: CommandLine.hpp:19
std::vector< AType, Interop::STLAllocator< AType >> Vector
std::vector algorithms using the CyberEngine's allocator
Definition: Containers.hpp:12
static Process GetSelf(void) noexcept
Get's the Process representing the current execution.
Definition: CYBProcess.cpp:12
virtual void Log(const String::CStyle &AMessage, const Level ALevel)=0
Log a message. Will be written to a text file on the Path returned by CurrentLog. ...
Recoverable warnings.
int RawLength(void) const noexcept
Get the byte length of the contained CString.
void Terminate(void)
Terminates the Process with exit code 0.
CommandLine(const unsigned int ANumArguments, const oschar_t *const *const AArguments)
Parse the command line into tokens.
Dynamic SubString(const int AIndex, const int ALength) const
Get a substring of the contained string.
Used for string of allocated variable length.
static UTF8 ToUTF8(const wchar_t *AWString)
Create a new UTF8 string given a wide char array.
std::deque< AType, Interop::STLAllocator< AType >> Deque
std::deque algorithms using the CyberEngine's allocator
Definition: Containers.hpp:10
A basic char contained string.
Definition: CStyleString.hpp:7
Precompiled header for inter-engine operations.
static API::Container::Vector< Token > ParseTokens(const unsigned int ANumArguments, const oschar_t *const *const AArguments)
Parse the command line into tokens.
const char * CString(void) const noexcept
Get the contained const CString.
Logger & FLogger
The Logger.
Definition: Context.hpp:12
A parsed command line token.