-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleanParser.hpp
More file actions
50 lines (46 loc) · 1.68 KB
/
BooleanParser.hpp
File metadata and controls
50 lines (46 loc) · 1.68 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
#pragma once
#include <string>
#include <algorithm>
static void toLower(std::string& str)
{
std::ranges::transform(str, str.begin(), tolower);
}
/**
* This is a parser for arguments of type UCLI_COMMAND_TYPE_BOOL. The syntax is as follows:
* - `command` or short `c` - the boolean is toggled or set to true
* - `command=value` - the boolean is set to the value
* - `command=` - same as 1
* - `command` or `command=value` where `boolValue` is nullptr - no additional parsing, we simply push the callback
* - `-cC` batched short arguments - the same as cases 1 or 3
* - `command value` - sets the command to the value
* - `command --flag` - the boolean is toggled or set to true
*/
template<typename T>
static void loadBooleanCommand(int& i, const int argc, char** argv, T& command, const int64_t assignmentIndex, const bool bToggle, const char flagPrefix, const bool bForcedDefault) noexcept
{
// This is completely fine, since we have to support some way of accepting void without a distinct type
if (command.boolValue == nullptr)
return;
if (assignmentIndex >= 0)
{
std::string str = argv[i] + assignmentIndex + 1;
toLower(str);
if (str == "false" || str == "0" || str == "no" || str == "n")
{
*command.boolValue = false;
return;
}
}
else if (!bForcedDefault && argc > (i + 1) && argv[i + 1][0] != flagPrefix)
{
std::string str = argv[i + 1];
toLower(str);
i++;
if (str == "false" || str == "0" || str == "no" || str == "n")
{
*command.boolValue = false;
return;
}
}
*command.boolValue = bToggle ? !*command.boolValue : true;
}