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

23
flags.h
View File

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