1 /***************************************************************************** 2 * Copyright (c) 2014-2020 OpenRCT2 developers 3 * 4 * For a complete list of all authors, please refer to contributors.md 5 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 6 * 7 * OpenRCT2 is licensed under the GNU General Public License version 3. 8 *****************************************************************************/ 9 10 #pragma once 11 12 #include "../common.h" 13 14 /** 15 * Class for enumerating and retrieving values for a set of command line arguments. 16 */ 17 class CommandLineArgEnumerator final 18 { 19 private: 20 const char* const* _arguments; 21 uint16_t _count; 22 uint16_t _index; 23 24 public: GetArguments() const25 const char* const* GetArguments() const 26 { 27 return _arguments; 28 } GetCount() const29 uint16_t GetCount() const 30 { 31 return _count; 32 } GetIndex() const33 uint16_t GetIndex() const 34 { 35 return _index; 36 } 37 38 CommandLineArgEnumerator(const char* const* arguments, int32_t count); 39 40 void Reset(); 41 bool Backtrack(); 42 bool TryPop(); 43 bool TryPopInteger(int32_t* result); 44 bool TryPopReal(float* result); 45 bool TryPopString(const char** result); 46 }; 47 48 using exitcode_t = int32_t; 49 using CommandLineFunc = exitcode_t (*)(CommandLineArgEnumerator*); 50 51 enum 52 { 53 EXITCODE_FAIL = -1, 54 EXITCODE_OK = 0, 55 EXITCODE_CONTINUE = 1, 56 }; 57 58 struct CommandLineExample 59 { 60 const char* Arguments; 61 const char* Description; 62 }; 63 64 struct CommandLineOptionDefinition 65 { 66 uint8_t Type; 67 void* OutAddress; 68 char ShortName; 69 const char* LongName; 70 const char* Description; 71 }; 72 73 struct CommandLineCommand 74 { 75 const char* Name; 76 const char* Parameters; 77 const CommandLineOptionDefinition* Options; 78 const CommandLineCommand* SubCommands; 79 CommandLineFunc Func; 80 }; 81 82 enum 83 { 84 CMDLINE_TYPE_SWITCH, 85 CMDLINE_TYPE_INTEGER, 86 CMDLINE_TYPE_REAL, 87 CMDLINE_TYPE_STRING, 88 }; 89 90 constexpr char NAC = '\0'; 91 92 #define ExampleTableEnd \ 93 { \ 94 nullptr, nullptr \ 95 } 96 #define OptionTableEnd \ 97 { \ 98 UINT8_MAX, nullptr, NAC, nullptr, nullptr \ 99 } 100 #define CommandTableEnd \ 101 { \ 102 nullptr, nullptr, nullptr, nullptr, nullptr \ 103 } 104 105 #define DefineCommand(name, params, options, func) \ 106 { \ 107 name, params, options, nullptr, func \ 108 } 109 #define DefineSubCommand(name, subcommandtable) \ 110 { \ 111 name, "", nullptr, subcommandtable, nullptr \ 112 } 113 114 namespace CommandLine 115 { 116 extern const CommandLineCommand RootCommands[]; 117 extern const CommandLineCommand ScreenshotCommands[]; 118 extern const CommandLineCommand SpriteCommands[]; 119 extern const CommandLineCommand BenchGfxCommands[]; 120 extern const CommandLineCommand BenchSpriteSortCommands[]; 121 extern const CommandLineCommand BenchUpdateCommands[]; 122 extern const CommandLineCommand SimulateCommands[]; 123 124 extern const CommandLineExample RootExamples[]; 125 126 void PrintHelp(bool allCommands = false); 127 exitcode_t HandleCommandDefault(); 128 129 exitcode_t HandleCommandConvert(CommandLineArgEnumerator* enumerator); 130 exitcode_t HandleCommandUri(CommandLineArgEnumerator* enumerator); 131 } // namespace CommandLine 132