-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommandLineParser.hpp
109 lines (100 loc) · 2.91 KB
/
CommandLineParser.hpp
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
/*
* Vulkan hardware capability viewer command line version
*
* Copyright (C) 2022 by Sascha Willems (www.saschawillems.de)
*
* This code is free software, you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 3 as published by the Free Software Foundation.
*
* Please review the following information to ensure the GNU Lesser
* General Public License version 3 requirements will be met:
* http://opensource.org/licenses/lgpl-3.0.html
*
* The code is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU LGPL 3.0 for more details.
*
*/
#include <vector>
#include <string>
#include <unordered_map>
#include <iostream>
#include <assert.h>
class CommandLineParser
{
public:
struct CommandLineOption {
std::vector<std::string> commands;
std::string value;
bool hasValue = false;
std::string help;
bool set = false;
};
std::unordered_map<std::string, CommandLineOption> options;
void add(std::string name, std::vector<std::string> commands, bool hasValue, std::string help) {
options[name].commands = commands;
options[name].help = help;
options[name].set = false;
options[name].hasValue = hasValue;
options[name].value = "";
}
void printHelp() {
std::cout << "Available command line options:\n";
for (auto option : options) {
std::cout << " ";
for (size_t i = 0; i < option.second.commands.size(); i++) {
std::cout << option.second.commands[i];
if (i < option.second.commands.size() - 1) {
std::cout << ", ";
}
}
std::cout << ": " << option.second.help << "\n";
}
std::cout << "Press any key to close...";
}
void parse(int argc, char* argv[]) {
bool printHelp = false;
// Known arguments
for (auto& option : options) {
for (auto& command : option.second.commands) {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], command.c_str()) == 0) {
option.second.set = true;
// Get value
if (option.second.hasValue) {
if (argc > i + 1) {
option.second.value = argv[i + 1];
}
if (option.second.value == "") {
printHelp = true;
break;
}
}
}
}
}
}
}
bool isSet(std::string name) {
return ((options.find(name) != options.end()) && options[name].set);
}
std::string getValueAsString(std::string name, std::string defaultValue) {
assert(options.find(name) != options.end());
std::string value = options[name].value;
return (value != "") ? value : defaultValue;
}
int32_t getValueAsInt(std::string name, int32_t defaultValue) {
assert(options.find(name) != options.end());
std::string value = options[name].value;
if (value != "") {
char* numConvPtr;
int32_t intVal = strtol(value.c_str(), &numConvPtr, 10);
return (intVal > 0) ? intVal : defaultValue;
}
else {
return defaultValue;
}
return int32_t();
}
};