Repository navigation
Expand file tree
/
Copy pathFlagParser.cpp
More file actions
193 lines (176 loc) · 6.58 KB
/
Copy pathFlagParser.cpp
File metadata and controls
193 lines (176 loc) · 6.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "CLIParser.hpp"
#include "Router.hpp"
#include "FlagParser.hpp"
int64_t UCLI::Parser::getAssignmentIndex(const char* str) noexcept
{
if (str[0] == '=')
return -1;
int64_t assignmentIndex = -1;
for (int64_t j = 0; j < INT64_MAX; j++)
{
if (str[j] == '\0')
break;
if (str[j] == '=')
{
assignmentIndex = j;
break;
}
}
return assignmentIndex;
}
bool UCLI::Parser::findFlagsRecursive(int& i, const int argc, char** argv, const int64_t assignmentIndex, const int64_t depth, const Command* command, const std::string& cleanName) noexcept
{
Flag* array = command == nullptr ? flags.data() : command->flags;
const size_t count = command == nullptr ? flags.size() : command->flagsCount;
for (size_t j = 0; j < count; j++)
{
// The long name may be null when the user only gave the flag a short name, so we can't hand it to
// std::string's comparison operator unchecked
if (array[j].longName != nullptr && cleanName == array[j].longName)
{
executeCommand(
i,
argc,
argv,
array[j],
assignmentIndex,
arrayDelimiter,
bToggleBooleans,
flagPrefix,
false,
depth,
callbacks,
*this
);
return true;
}
}
// Walk up to the parent command, and once we run past the root of the chain search the parser's own flags, so
// that a flag declared on an ancestor command - or globally - still resolves from inside a subcommand
if (command != nullptr)
return findFlagsRecursive(i, argc, argv, assignmentIndex, depth + 1, command->_internal_parent, cleanName);
return false;
}
bool UCLI::Parser::findFlagsRecursive(int& i, const int argc, char** argv, const int64_t depth, const Command* command, const char shortName, const bool bBatched) noexcept
{
// The command is null when we haven't entered one yet, and when we've walked past the root of the command chain.
// In both cases we search the parser's own flags
Flag* array = command == nullptr ? flags.data() : command->flags;
const size_t count = command == nullptr ? flags.size() : command->flagsCount;
for (size_t j = 0; j < count; j++)
{
if (array[j].shortName == shortName)
{
executeCommand(
i,
argc,
argv,
array[j],
-1,
arrayDelimiter,
bToggleBooleans,
flagPrefix,
bBatched,
depth,
callbacks,
*this
);
return true;
}
}
// Same as above: walk up the chain of parents and finally fall back to the parser's own flags
if (command != nullptr)
return findFlagsRecursive(i, argc, argv, depth + 1, command->_internal_parent, shortName, bBatched);
return false;
}
// Returns 0 on success, 1 when skipping -- or - and 2 only in strict mode when an unrecognized command is found
int UCLI::Internal::parseFlag(int& i, const int argc, char** argv, UCLI::Parser& p) noexcept
{
const char* current = argv[i];
// Skip -- and -
if (current[1] == '\0' || (current[1] == p.flagPrefix && current[2] == '\0'))
return 1;
// --flag
if (current[1] == p.flagPrefix)
{
// Clean up the command name so that we can search for the command
std::string cleanName = current + 2;
int64_t assignmentIndex = UCLI::Parser::getAssignmentIndex(cleanName.c_str());
if (assignmentIndex != -1)
{
cleanName.erase(assignmentIndex);
assignmentIndex += 2; // + 2 to account for removing the --
}
if (!p.findFlagsRecursive(i, argc, argv, assignmentIndex, 0, p.currentCommand, cleanName))
{
if (p.bHasDefaultFlag)
{
// For diagnostic commands like --help
p.defaultFlag._internal_ctx_ = argv[i];
// Do not run executeCommand, since it contains additional parsing logic. Default commands should not
// take advantage of features such as default arguments and values
pushCallback(p.callbacks.size(), p.defaultFlag, 0, p.callbacks);
}
return p.bStrictMode ? 2 : 0;
}
}
else // -f or -fF
{
for (size_t f = 1; current[f] != '\0'; f++)
{
if (
!p.findFlagsRecursive(
i,
argc,
argv,
0,
p.currentCommand,
current[f],
f > 1 || (f == 1 && current[f + 1] != '\0')
)
)
{
if (p.bHasDefaultFlag)
{
// For diagnostic commands like --help
p.defaultFlag._internal_ctx_ = argv[i];
// Do not run executeCommand, since it contains additional parsing logic. Default commands should not
// take advantage of features such as default arguments and values
pushCallback(p.callbacks.size(), p.defaultFlag, 0, p.callbacks);
}
return p.bStrictMode ? 2 : 0;
}
}
}
return 0;
}
// Add all flags before the argument to a command in syntax such as: git clone --recursive https://...
UCLI::Internal::ProbeResult UCLI::Internal::probeFlags(UCLI_Command& command, int& i, const int argc, char** argv, UCLI::Parser& p) noexcept
{
ProbeResult result = ProbeResult::Exhausted;
for (; i < argc; i++)
{
// Not a flag, so this is a value that belongs to the command. We check this before parsing a flag, since every
// caller of this function increments i past the last argument that a flag consumed
if (argv[i][0] != p.flagPrefix)
{
result = ProbeResult::Value;
break;
}
// Is a flag and errors when false
if (parseFlag(i, argc, argv, p) == 2)
{
result = ProbeResult::Failure;
break;
}
}
// We never found a value for the command, so it only gets whatever the user set as its default
if (result == ProbeResult::Exhausted)
{
if (command.defaultValues != nullptr && command.defaultValuesCount > 0)
useDefaultArguments(command);
else
useNullArguments(command);
}
return result;
}