formatting changes

This commit is contained in:
BENEDEK László 2023-12-01 15:22:58 +01:00
parent 5810494286
commit ea025ba2fc
2 changed files with 56 additions and 69 deletions

View File

@ -1,6 +1,7 @@
#include "flags.h"
#include <sstream>
#include <iostream>
#include <sstream>
namespace Flags {
@ -15,7 +16,7 @@ std::vector<std::string> split(const std::string &input, const char separator) {
}
bool starts_with(const std::string& input, const std::string& start) {
return (input.size() >= start.size() && input.substr(0, 2) == start);
return (input.size() >= start.size() && input.substr(0, start.size()) == start);
}
PARSER(StringFlag, std::string, {
@ -98,23 +99,20 @@ PARSER(LongDoubleFlag, long double, {
PARSER(BoolFlag, bool, {
std::string copy(arg);
std::transform(copy.begin(), copy.end(), copy.begin(),
[](const char c) { return std::tolower(c); });
std::transform(copy.begin(), copy.end(), copy.begin(), [](const char c) { return std::tolower(c); });
if (copy == "false")
value = false;
else
value = true;
})
Parser::Parser(const std::string &prefix, const std::string &help_text)
: prefix(prefix), help_text(help_text) {
Parser::Parser(const std::string& prefix, const std::string& help_text) : prefix(prefix), help_text(help_text) {
set_parser<std::string>(flag_constructor_t(StringFlag::make));
set_parser<int>(flag_constructor_t(IntFlag::make));
set_parser<long int>(flag_constructor_t(LongIntFlag::make));
set_parser<long long int>(flag_constructor_t(LongLongIntFlag::make));
set_parser<unsigned long int>(flag_constructor_t(UnsignedLongIntFlag::make));
set_parser<unsigned long long int>(
flag_constructor_t(UnsignedLongLongIntFlag::make));
set_parser<unsigned long long int>(flag_constructor_t(UnsignedLongLongIntFlag::make));
set_parser<float>(flag_constructor_t(FloatFlag::make));
set_parser<double>(flag_constructor_t(DoubleFlag::make));
set_parser<long double>(flag_constructor_t(LongDoubleFlag::make));
@ -143,8 +141,7 @@ bool Parser::parse(int argc, char **argv) {
}
// check for the last argument
if (starts_with(args[args.size() - 1], prefix) &&
args[args.size() - 1].size() > prefix.size()) {
if (starts_with(args[args.size() - 1], prefix) && args[args.size() - 1].size() > prefix.size()) {
flags[args[args.size() - 1].substr(prefix.size())]->parse("");
}
@ -152,23 +149,18 @@ bool Parser::parse(int argc, char **argv) {
}
unsigned Parser::get_parsed() const {
return std::count_if(
flags.begin(), flags.end(),
[](std::pair<std::string, Flag *> f) { return f.second->get_parsed(); });
return std::count_if(flags.begin(), flags.end(), [](std::pair<std::string, Flag*> f) { return f.second->get_parsed(); });
}
unsigned Parser::get_found() const {
return std::count_if(
flags.begin(), flags.end(),
[](std::pair<std::string, Flag *> f) { return f.second->get_found(); });
return std::count_if(flags.begin(), flags.end(), [](std::pair<std::string, Flag*> f) { return f.second->get_found(); });
}
void Parser::help() const {
std::cout << help_text << std::endl;
for (auto f : flags) {
std::cout << '\t' << prefix << f.first << ": "
<< f.second->get_description() << std::endl;
std::cout << '\t' << prefix << f.first << ": " << f.second->get_description() << std::endl;
}
}
} // namespace Flags

23
flags.h
View File

@ -26,30 +26,28 @@ protected:
public:
virtual const void* get_value_ptr() const = 0;
Flag(const std::string &description, const bool required)
: description(description), found(!required), parsed(false) {}
Flag(const std::string& description, const bool required) : description(description), found(!required), parsed(false) {}
virtual ~Flag(){};
const std::string& get_description() const { return description; }
bool get_found() const { return found; }
bool get_parsed() const { return parsed; }
bool get_found() const { return found; }
virtual void parse(const std::string& arg) = 0;
};
// type of flag based parser classes' factory functions
typedef std::function<Flag *(const void *, const std::string &, const bool)>
flag_constructor_t;
typedef std::function<Flag*(const void*, const std::string&, const bool)> flag_constructor_t;
// use this class to create your own parser
template <typename T> class FlagTemplate : public Flag {
template <typename T>
class FlagTemplate : public Flag {
protected:
T value;
public:
explicit FlagTemplate<T>(const void *default_value,
const std::string &description, const bool required)
explicit FlagTemplate<T>(const void* default_value, const std::string& description, const bool required)
: Flag(description, required), value(*(T*)default_value) {}
const void* get_value_ptr() const override { return &value; };
@ -60,8 +58,7 @@ public:
using FlagTemplate<type>::FlagTemplate; \
\
public: \
static Flag *make(const void *default_value, \
const std::string &description, const bool required) { \
static Flag* make(const void* default_value, const std::string& description, const bool required) { \
return new name(default_value, description, required); \
} \
void parse(const std::string& arg) override func \
@ -93,10 +90,8 @@ public:
}
template <typename T>
T *add(const std::string &name, const std::string &description,
const bool required, T default_value) {
flags[name] =
constructors[typeid(T)]((void *)&default_value, description, required);
T* add(const std::string& name, const std::string& description, const bool required, T default_value) {
flags[name] = constructors[typeid(T)]((void*)&default_value, description, required);
return (T*)flags[name]->get_value_ptr();
}