-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlagParser.cpp
More file actions
175 lines (159 loc) · 5.47 KB
/
FlagParser.cpp
File metadata and controls
175 lines (159 loc) · 5.47 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
#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++)
{
if (cleanName == array[j].longName)
{
executeCommand(
i,
argc,
argv,
array[j],
assignmentIndex,
arrayDelimiter,
bToggleBooleans,
flagPrefix,
false,
bProbingFlags,
depth,
callbacks,
*this
);
return true;
}
}
if (command != nullptr && command->_internal_parent != 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
{
for (size_t j = 0; j < command->flagsCount; j++)
{
if (command->flags[j].shortName == shortName)
{
executeCommand(
i,
argc,
argv,
command->flags[j],
-1,
arrayDelimiter,
bToggleBooleans,
flagPrefix,
bBatched,
bProbingFlags,
depth,
callbacks,
*this
);
return true;
}
}
if (command->_internal_parent != 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.defaultFlag != nullptr)
{
// 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.defaultFlag != nullptr)
{
// 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://...
bool UCLI::Internal::probeFlags(UCLI_Command& command, int& i, const int argc, char** argv, UCLI::Parser& p) noexcept
{
p.bProbingFlags = true;
for (; i < argc; i++)
{
// Is a flag and errors when false
if (argv[i][0] == p.flagPrefix && parseFlag(i, argc, argv, p) == 2)
return false;
// A value
if (argv[i][0] != p.flagPrefix)
return true;
}
if (command.defaultValues != nullptr && command.defaultValuesCount > 0)
useDefaultArguments(command);
else
useNullArguments(command);
p.bProbingFlags = false;
return true;
}