-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIParser.cpp
More file actions
117 lines (98 loc) · 3.37 KB
/
CLIParser.cpp
File metadata and controls
117 lines (98 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "CLIParser.hpp"
#include "ParserUtils.hpp"
UCLI::Parser& UCLI::Parser::setArrayDelimiter(const char delimiter) noexcept
{
arrayDelimiter = delimiter;
return *this;
}
UCLI::Parser& UCLI::Parser::useLenientMode(const bool bUseLenientMode) noexcept
{
bStrictMode = !bUseLenientMode;
return *this;
}
UCLI::Parser& UCLI::Parser::setBoolToggle(const bool bToggle) noexcept
{
bToggleBooleans = bToggle;
return *this;
}
UCLI::Parser& UCLI::Parser::setFlagPrefix(const char prefix) noexcept
{
flagPrefix = prefix;
return *this;
}
UCLI::Parser& UCLI::Parser::pushCommand(const Command& command) noexcept
{
commands.push_back(command);
return *this;
}
UCLI::Parser& UCLI::Parser::pushFlag(const Flag& flag) noexcept
{
flags.push_back(flag);
return *this;
}
UCLI::Parser& UCLI::Parser::pushDefaultCommand(const Command& command) noexcept
{
commands.push_back(command);
defaultCommand = &commands.back();
return *this;
}
UCLI::Parser& UCLI::Parser::pushDefaultFlag(const Flag& flag) noexcept
{
flags.push_back(flag);
defaultFlag = &flags.back();
return *this;
}
void UCLI::Parser::freeCommands(Command& command) noexcept
{
if ((command.type == UCLI_COMMAND_TYPE_ARRAY || command.type == UCLI_COMMAND_TYPE_STRING) && !(command.stringValues.stringValues == nullptr || command.stringValues.stringValuesCount == 0))
{
if (command.stringValues._internal_._bFreeInnerStringValues)
for (size_t i = 0; i < command.stringValues.stringValuesCount; i++)
free(UCLI_VOID_CAST(command.stringValues.stringValues[i]));
if (command.stringValues._internal_._bFreeStringValues)
free(command.stringValues.stringValues);
memset(&command.stringValues, 0, sizeof(command.stringValues));
command.stringValues._internal_._bFreeInnerStringValues = false;
command.stringValues._internal_._bFreeStringValues = false;
}
if (command.subcommands != nullptr)
for (size_t i = 0; i < command.subcommandsCount; i++)
freeCommands(command.subcommands[i]);
if (command.flags != nullptr)
for (size_t i = 0; i < command.flagsCount; i++)
freeFlags(command.flags[i]);
}
void UCLI::Parser::freeFlags(Flag& command) noexcept
{
if ((command.type == UCLI_COMMAND_TYPE_ARRAY || command.type == UCLI_COMMAND_TYPE_STRING) && !(command.stringValues.stringValues == nullptr || command.stringValues.stringValuesCount == 0))
{
if (command.stringValues._internal_._bFreeInnerStringValues)
for (size_t i = 0; i < command.stringValues.stringValuesCount; i++)
free(UCLI_VOID_CAST(command.stringValues.stringValues[i]));
if (command.stringValues._internal_._bFreeStringValues)
free(command.stringValues.stringValues);
memset(&command.stringValues, 0, sizeof(command.stringValues));
}
}
UCLI::Parser& UCLI::Parser::release() noexcept
{
for (auto& command : commands)
freeCommands(command);
for (auto& flag : flags)
freeFlags(flag);
defaultCommand = nullptr;
defaultFlag = nullptr;
return *this;
}
UCLI::Parser::~Parser() noexcept
{
release();
}
UCLI_CallbackResult UCLI_EMPTY_FLAG_CALLBACK(const UCLI_Flag* flag)
{
return UCLI_CALLBACK_RESULT_OK;
}
UCLI_CallbackResult UCLI_EMPTY_COMMAND_CALLBACK(const UCLI_Command* flag)
{
return UCLI_CALLBACK_RESULT_OK;
}