Add command line arguments to headless samples
Those samples can now toggle between glsl and hlsl shaders Moved command line parster to a separate header
This commit is contained in:
parent
484d16d394
commit
e52a9342f4
5 changed files with 172 additions and 137 deletions
117
base/CommandLineParser.hpp
Normal file
117
base/CommandLineParser.hpp
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Simple command line parse
|
||||
*
|
||||
* Copyright (C) 2016-2022 by Sascha Willems - www.saschawillems.de
|
||||
*
|
||||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <string>
|
||||
|
||||
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(std::vector<const char*> arguments)
|
||||
{
|
||||
bool printHelp = false;
|
||||
// Known arguments
|
||||
for (auto& option : options) {
|
||||
for (auto& command : option.second.commands) {
|
||||
for (size_t i = 0; i < arguments.size(); i++) {
|
||||
if (strcmp(arguments[i], command.c_str()) == 0) {
|
||||
option.second.set = true;
|
||||
// Get value
|
||||
if (option.second.hasValue) {
|
||||
if (arguments.size() > i + 1) {
|
||||
option.second.value = arguments[i + 1];
|
||||
}
|
||||
if (option.second.value == "") {
|
||||
printHelp = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Print help for unknown arguments or missing argument values
|
||||
if (printHelp) {
|
||||
options["help"].set = true;
|
||||
}
|
||||
}
|
||||
|
||||
void parse(int argc, char* argv[])
|
||||
{
|
||||
std::vector<const char*> args;
|
||||
for (int i = 0; i < __argc; i++) {
|
||||
args.push_back(__argv[i]);
|
||||
};
|
||||
parse(args);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue