-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLIParser.hpp
More file actions
103 lines (77 loc) · 3.59 KB
/
CLIParser.hpp
File metadata and controls
103 lines (77 loc) · 3.59 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
#pragma once
#include <vector>
#include <deque>
#include <string>
#include <cstring>
#include "Common.h"
#include "FlagParser.hpp"
namespace UCLI
{
typedef UCLI_Command Command;
typedef UCLI_Flag Flag;
typedef UCLI_CommandType CommandType;
typedef UCLI_CallbackResult CallbackResult;
class MLS_PUBLIC_API Parser
{
public:
Parser() noexcept = default;
Parser& setHelpHeader(const char* header) noexcept;
Parser& setHelpFooter(const char* footer) noexcept;
// Set to true by default
Parser& setUseGeneratedHelp(bool bUseGeneratedHelp) noexcept;
// Set to 2 by default
Parser& setHelpSubcommandIndentationSpaces(size_t indentSpaces) noexcept;
// The default is `-`
Parser& setFlagPrefix(char prefix) noexcept;
// The default is `,`
Parser& setArrayDelimiter(char delimiter) noexcept;
// By default, we use strict mode where the default argument/command is called and directly exits. Lenient mode
// replaces calls to invalid commands/flags with the default argument/flag command instead without exiting.
Parser& useLenientMode(bool bUseLenientMode) noexcept;
// Whether to toggle boolean arguments or to set them to true. The default behaviour is to set them to true
Parser& setBoolToggle(bool bToggle) noexcept;
Parser& pushCommand(const Command& command) noexcept;
Parser& pushFlag(const Flag& flag) noexcept;
Parser& pushDefaultCommand(const Command& command) noexcept;
Parser& pushDefaultFlag(const Flag& flag) noexcept;
Parser& parse(int argc, char** argv) noexcept;
Parser& release() noexcept;
~Parser() noexcept;
private:
std::string helpHeader;
std::string helpFooter;
std::string indentationString = " ";
char arrayDelimiter = ',';
char flagPrefix = '-';
bool bToggleBooleans = false;
bool bUseHelp = true;
bool bShowHelp = false;
bool bStrictMode = true;
bool bProbingFlags = false;
std::vector<Command> commands{};
std::vector<Flag> flags{};
Command* currentCommand = nullptr;
Command* defaultCommand = nullptr;
Flag* defaultFlag = nullptr;
void pushHelp() noexcept;
static void printCommands(const Command* commands, size_t size, const std::string& indentationString, size_t indentation) noexcept;
static void printFlags(const Flag* flags, size_t size, const std::string& indentationString, size_t indentation) noexcept;
static int64_t getAssignmentIndex(const char* str) noexcept;
bool findFlagsRecursive(int& i, int argc, char** argv, int64_t assignmentIndex, int64_t depth, const Command* command, const std::string& cleanName) noexcept;
bool findFlagsRecursive(int& i, int argc, char** argv, int64_t depth, const Command* command, char shortName, bool bBatched) noexcept;
static void freeCommands(Command& command) noexcept;
static void freeFlags(Flag& command) noexcept;
friend int UCLI::Internal::parseFlag(int& i, int argc, char** argv, Parser& p) noexcept;
friend bool UCLI::Internal::probeFlags(UCLI_Command& command, int& i, int argc, char** argv, Parser& p) noexcept;
public:
struct CallbackObject
{
void* ptr = nullptr;
bool bCommand = false;
};
static CallbackResult helpCommand(const Flag* command) noexcept;
static CallbackResult helpCommand(const Command* command) noexcept;
private:
std::deque<CallbackObject> callbacks;
};
}