28#define QCONFIG_PARSER_CXX
29#define QCONFIG_INSTANCE
31#undef QCONFIG_INSTANCE
36static inline const char* getOptName(
const char** argv, int32_t
i)
38 while (
i > 1 && argv[
i][0] !=
'-') {
58static int32_t qAddOptionType(
qConfigSettings<T>& settings, T&
ref, int32_t&
i,
const char** argv,
const int argc, T def);
68template <
typename... X>
77template <
typename... X>
90 }
else if ((
retVal = qAddOptionType<T>(settings,
ref,
i, argv, argc, def))) {
93 if ((
retVal = qAddOptionMinMax<T>(settings,
ref, iOrg < argc ? argv[iOrg] :
""))) {
96 qAddOptionMessage<T>(settings,
ref);
104 return (qAddOptionType<T>(settings,
ref,
i, argv, argc, def));
106template <
typename T,
int32_t index = 0,
int32_t left = std::tuple_size_v<T>>
110 auto&
ref = std::get<index>(tup);
111 int32_t
retVal = qAddOptionMainTupleElem(settings,
ref,
i, argv, argc);
114 printf(
"Invalid number of arguments for option %s\n", getOptName(argv,
i));
122template <
typename T,
int32_t index>
126template <
typename... X>
137 static_assert(!std::is_same_v<T, bool>,
"min option not supported for boolean settings");
139 settings.
min = minval.v;
143 static_assert(!std::is_same_v<T, bool>,
"max option not supported for boolean settings");
145 settings.
max = maxval.v;
148 template <
typename S>
151 settings.
doSet =
true;
152 settings.
set = set.v;
154 template <
typename S>
158 settings.
set = set.v;
162 template <
typename Arg1,
typename... Args>
169 template <
typename... Args>
170 static inline int32_t
qAddOption(T&
ref, int32_t&
i,
const char** argv,
const int argc, T def,
const char* , Args&&... args)
176 template <
typename... Args>
177 static inline int32_t
qAddOptionVec(std::vector<T>&
ref, int32_t&
i,
const char** argv,
const int argc,
const char* , Args&&... args)
180 int32_t iFirst =
i, iLast;
183 T tmp = T(), def = T();
189 if (
i == iFirst ||
i != iLast) {
192 }
while (
i != iLast);
196 template <
typename... Args>
197 static inline int32_t
qAddOptionArray(T*
ref, int32_t
count, int32_t&
i,
const char** argv,
const int argc,
const char* , Args&&... args)
200 int32_t iFirst =
i, iLast;
203 T tmp = T(), def = T();
209 if (
i - iFirst >=
count) {
210 printf(
"Too many values provided for option %s\n", getOptName(argv,
i));
213 if (
i == iFirst ||
i != iLast) {
214 ref[
i - iFirst] = tmp;
216 }
while (
i != iLast);
220 template <
typename... Args>
221 static inline void qConfigHelpOption(
const char*
name,
const char*
type,
const char* def,
const char* optname,
char optnameshort,
const char* preopt,
char preoptshort, int32_t optionType,
const char* help, Args&&... args)
224 const bool boolType = optionType != 1 && std::is_same_v<T, bool>;
225 const char*
arguments = settings.
doSet ?
" (" : (settings.
doDefault || optionType == 1 || boolType) ?
" [arg] (" : optionType == 2 ?
" [...] (" :
" arg (";
226 char argBuffer[4] = {0};
227 uint32_t argBufferPos = 0;
228 if (optnameshort && preoptshort) {
229 argBuffer[argBufferPos++] =
'-';
232 argBuffer[argBufferPos++] = preoptshort == 0 ?
'-' : preoptshort;
233 argBuffer[argBufferPos++] = optnameshort;
235 std::cout <<
"\t" <<
name <<
": " << argBuffer << (optnameshort == 0 ?
"" :
arguments) <<
"--";
236 if (optname[0] == 0) {
237 std::cout << preopt <<
name;
239 std::cout << optname;
241 std::cout << (optnameshort == 0 ?
arguments :
", ") <<
"type: " <<
type;
242 if (optionType == 0) {
243 std::cout <<
", default: " << def;
245 if (optionType == 1) {
246 std::cout <<
", sets " <<
name <<
" to " << def;
249 std::cout <<
", minimum: " << settings.
min;
252 std::cout <<
", maximum: " << settings.
max;
254 std::cout <<
")\n\t\t" << help <<
".\n";
255 if (settings.
doSet) {
256 std::cout <<
"\t\tSets " <<
name <<
" to " << settings.
set <<
".\n";
258 std::cout <<
"\t\tIf no argument is supplied, " <<
name <<
" is set to " << settings.
set <<
".\n";
259 }
else if (boolType) {
260 std::cout <<
"\t\tIf no argument is supplied, " <<
name <<
" is set to true.\n";
262 if (optionType == 2) {
263 std::cout <<
"\t\tCan be set multiple times, accepts multiple arguments.\n";
268 template <
typename... Args>
277static inline const char* getArg(int32_t&
i,
const char** argv,
const int argc,
bool allowOption =
false)
279 if (
i + 1 < argc && argv[
i + 1][0] && ((allowOption && argv[
i + 1][0] ==
'-' && argv[
i + 1][1] !=
'-') || argv[
i + 1][0] !=
'-')) {
286static inline int32_t qAddOptionGeneric(qConfigSettings<T>& settings, T&
ref, int32_t&
i,
const char** argv,
const int argc, T def, std::function<
T(
const char*)>
func,
bool allowDefault =
false)
288 const char* arg = getArg(
i, argv, argc, !allowDefault);
292 }
else if (allowDefault) {
296 if (!settings.allowEmpty) {
297 printf(
"Argument missing for option %s!\n", getOptName(argv,
i));
306 return qAddOptionGeneric<bool>(
307 settings,
ref,
i, argv, argc, settings.
doDefault ? settings.
set :
true, [](
const char*
a) ->
bool {
315 return qAddOptionGeneric<int8_t>(
316 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int8_t {
324 return qAddOptionGeneric<uint8_t>(
325 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint8_t {
333 return qAddOptionGeneric<int32_t>(
334 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int32_t {
342 return qAddOptionGeneric<uint32_t>(
343 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint32_t {
344 return strtoul(a, nullptr, 0);
351 return qAddOptionGeneric<int16_t>(
352 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int16_t {
360 return qAddOptionGeneric<uint16_t>(
361 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint16_t {
362 return strtoul(a, nullptr, 0);
369 return qAddOptionGeneric<int64_t>(
370 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
int64_t {
371 return strtoll(a, nullptr, 0);
378 return qAddOptionGeneric<uint64_t>(
379 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint64_t {
380 return strtoull(a, nullptr, 0);
387 return qAddOptionGeneric<float>(
388 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
float {
389 return (float)atof(a);
396 return qAddOptionGeneric<double>(
397 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
double {
405 return qAddOptionGeneric<const char*>(
406 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
const char* {
414 return qAddOptionGeneric<std::string>(
415 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> std::string {
423static inline int32_t qAddOptionMinMax(qConfigSettings<T>& settings, T&
ref,
const char* arg)
425 if (settings.checkMin &&
ref < settings.min) {
426 std::cout <<
"Invalid setting for " << arg <<
": minimum threshold exceeded (" <<
ref <<
" < " << settings.min <<
")!\n";
429 if (settings.checkMax &&
ref > settings.max) {
430 std::cout <<
"Invalid setting for " << arg <<
": maximum threshold exceeded (" <<
ref <<
" > " << settings.max <<
")!\n";
446 std::string
msg = std::string(settings.
message) +
"\n";
447 printf(
msg.c_str(), tmp.c_str());
451static inline void qConfigHelp(
const char* subConfig =
nullptr, int32_t followSub = 0)
454 printf(
"Usage Info:");
467 for (int32_t
i = 1;
i < argc;
i++) {
468 const char* thisoption = argv[
i];
474 if (found ==
false) {
475 printf(
"Invalid argument: %s\n", argv[
i]);
GLuint const GLchar * name
GLint GLint GLsizei GLint GLenum GLenum type
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint GLsizei const GLchar * message
GLboolean GLboolean GLboolean GLboolean a
bpo::variables_map arguments
int32_t qAddOptionType< float >(qConfigSettings< float > &settings, float &ref, int32_t &i, const char **argv, const int argc, float)
int32_t qAddOptionType< int8_t >(qConfigSettings< int8_t > &settings, int8_t &ref, int32_t &i, const char **argv, const int argc, int8_t)
std::vector< std::function< void()> > qprint_global
int32_t qAddOptionType< uint32_t >(qConfigSettings< uint32_t > &settings, uint32_t &ref, int32_t &i, const char **argv, const int argc, uint32_t)
int32_t qAddOptionType< std::string >(qConfigSettings< std::string > &settings, std::string &ref, int32_t &i, const char **argv, const int argc, std::string)
int32_t qAddOptionType< int64_t >(qConfigSettings< int64_t > &settings, int64_t &ref, int32_t &i, const char **argv, const int argc, int64_t)
int32_t qAddOptionType< uint16_t >(qConfigSettings< uint16_t > &settings, uint16_t &ref, int32_t &i, const char **argv, const int argc, uint16_t)
int32_t qAddOptionType< int16_t >(qConfigSettings< int16_t > &settings, int16_t &ref, int32_t &i, const char **argv, const int argc, int16_t)
int32_t qAddOptionType< uint8_t >(qConfigSettings< uint8_t > &settings, uint8_t &ref, int32_t &i, const char **argv, const int argc, uint8_t)
int32_t qAddOptionType< uint64_t >(qConfigSettings< uint64_t > &settings, uint64_t &ref, int32_t &i, const char **argv, const int argc, uint64_t)
int32_t qAddOptionType< bool >(qConfigSettings< bool > &settings, bool &ref, int32_t &i, const char **argv, const int argc, bool)
int32_t qAddOptionType< double >(qConfigSettings< double > &settings, double &ref, int32_t &i, const char **argv, const int argc, double)
int32_t qAddOptionType< const char * >(qConfigSettings< const char * > &settings, const char *&ref, int32_t &i, const char **argv, const int argc, const char *)
int32_t qConfigParse(int argc, const char **argv, const char *)
int32_t qAddOptionType< int32_t >(qConfigSettings< int32_t > &settings, int32_t &ref, int32_t &i, const char **argv, const int argc, int32_t)
std::string print_type(T val, bool precise=false)
int32_t qAddOptionMinMax< bool >(qConfigSettings< bool > &, bool &, const char *)
static int32_t qAddOptionMainTuple(qConfigSettings< typename qSettingsType< T >::settingsType >, T &, int32_t &, const char **, const int32_t)
static int32_t qAddOptionMainTuple(qConfigSettings< typename qSettingsType< T >::settingsType > settings, T &tup, int32_t &i, const char **argv, const int argc)
qConfigSettings(const qConfigSettings< S > v)
static int32_t qAddOptionMain(qConfigSettings< T > &settings, T &ref, int32_t &i, const char **argv, const int argc, T def)
static int32_t qAddOption(T &ref, int32_t &i, const char **argv, const int argc, T def, const char *, Args &&... args)
static int32_t qAddOptionArray(T *ref, int32_t count, int32_t &i, const char **argv, const int argc, const char *, Args &&... args)
static void qProcessSetting(qConfigSettings< T > &settings, qmin_t< T > minval)
static void qAddOptionSettings(qConfigSettings< T > &settings, Arg1 &&arg1, Args &&... args)
static int32_t qAddOptionVec(std::vector< T > &ref, int32_t &i, const char **argv, const int argc, const char *, Args &&... args)
static void qProcessSetting(qConfigSettings< T > &settings, qset_t< S > set)
static void qProcessSetting(qConfigSettings< T > &settings, qmessage_t msg)
static void qProcessSetting(qConfigSettings< T > &settings, qmax_t< T > maxval)
static void qConfigHelpOption(const char *name, const char *type, const char *def, const char *optname, char optnameshort, const char *preopt, char preoptshort, int32_t optionType, const char *help, Args &&... args)
static auto qConfigGetSettings(Args &... args)
static void qProcessSetting(qConfigSettings< T > &settings, qdef_t< S > set)
static void qAddOptionSettings(qConfigSettings< T > &)
uint64_t const void const *restrict const msg