fixed: name clashing

master v0.1.2
cobrapitz 3 years ago
parent 6897f5681e
commit 18c0cf5bd5

@ -8,15 +8,15 @@
namespace cb::argparser { namespace cb::argparser {
enum class Mode { enum class Mode {
POSITIONAL, POSITIONAL_MODE,
REQUIRED, REQUIRED_MODE,
OPTIONAL, OPTIONAL_MODE,
}; };
enum class Type { enum class Type {
STRING, STRING_TYPE,
BOOL, BOOL_TYPE,
INTEGER, INTEGER_TYPE,
}; };
struct ParseArgument { struct ParseArgument {
@ -34,12 +34,12 @@ struct ParseArgument {
class Argument { class Argument {
private: private:
std::string content; std::string content;
Type type{Type::STRING}; Type type{Type::STRING_TYPE};
public: public:
Argument() = default; Argument() = default;
explicit Argument(std::string content, Type type = Type::STRING); explicit Argument(std::string content, Type type = Type::STRING_TYPE);
[[nodiscard]] const std::string &as_str() const { [[nodiscard]] const std::string &as_str() const {
return content; return content;
@ -50,7 +50,7 @@ public:
} }
int as_int() { int as_int() {
assert(type == Type::INTEGER); assert(type == Type::INTEGER_TYPE);
return std::stoi(content); return std::stoi(content);
} }

@ -35,19 +35,19 @@ private:
public: public:
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description, void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
bool default_value, bool default_value,
Mode mode = Mode::OPTIONAL); Mode mode = Mode::OPTIONAL_MODE);
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description, void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
const char *default_value, const char *default_value,
Mode mode = Mode::OPTIONAL); Mode mode = Mode::OPTIONAL_MODE);
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description, void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
int default_value, int default_value,
Mode mode = Mode::OPTIONAL); Mode mode = Mode::OPTIONAL_MODE);
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description, void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
std::string default_value = "", std::string default_value = "",
Mode mode = Mode::OPTIONAL); Mode mode = Mode::OPTIONAL_MODE);
ArgumentMap parse(int argc, char *argv[]); ArgumentMap parse(int argc, char *argv[]);

@ -3,7 +3,7 @@
cb::argparser::Argument::Argument(std::string content, cb::argparser::Type type) : content{std::move(content)}, type{type} { cb::argparser::Argument::Argument(std::string content, cb::argparser::Type type) : content{std::move(content)}, type{type} {
if (type == Type::BOOL && this->content == "false") { if (type == Type::BOOL_TYPE && this->content == "false") {
this->content = ""; this->content = "";
} }
} }
@ -16,7 +16,7 @@ void cb::argparser::ArgumentParser::add_argument(const std::string &short_name,
throw std::runtime_error(error_message); throw std::runtime_error(error_message);
} }
if (mode == Mode::POSITIONAL) { if (mode == Mode::POSITIONAL_MODE) {
assert(short_name.empty() && "The name should be left empty if it's a positional argument!"); assert(short_name.empty() && "The name should be left empty if it's a positional argument!");
assert(name.empty() && "The name should be left empty if it's a positional argument!"); assert(name.empty() && "The name should be left empty if it's a positional argument!");
} else { } else {
@ -31,7 +31,7 @@ void cb::argparser::ArgumentParser::add_argument(const std::string &short_name,
argument.description = std::move(description); argument.description = std::move(description);
argument.default_value = std::move(default_value); argument.default_value = std::move(default_value);
if (mode == Mode::POSITIONAL) { if (mode == Mode::POSITIONAL_MODE) {
positional_arguments.push_back(argument); positional_arguments.push_back(argument);
} else { } else {
arguments.push_back(argument); arguments.push_back(argument);
@ -115,7 +115,7 @@ cb::argparser::ArgumentMap cb::argparser::ArgumentParser::parse_arguments(int ar
bool bool
cb::argparser::ArgumentParser::requiresAdditionalArgument(const cb::argparser::ParseArgument &parseArgument, int argc, cb::argparser::ArgumentParser::requiresAdditionalArgument(const cb::argparser::ParseArgument &parseArgument, int argc,
char *const *argv, int i) { char *const *argv, int i) {
if (parseArgument.type != Type::BOOL) { if (parseArgument.type != Type::BOOL_TYPE) {
return true; return true;
} }
@ -132,7 +132,7 @@ cb::argparser::ArgumentParser::requiresAdditionalArgument(const cb::argparser::P
void cb::argparser::ArgumentParser::verify_required_arguments_present() const { void cb::argparser::ArgumentParser::verify_required_arguments_present() const {
for (auto &&argument: arguments) { for (auto &&argument: arguments) {
if (argument.mode != Mode::REQUIRED) { if (argument.mode != Mode::REQUIRED_MODE) {
continue; continue;
} }
if (!argument.is_present) { if (!argument.is_present) {
@ -162,7 +162,7 @@ void cb::argparser::ArgumentParser::consume_argument(cb::argparser::ArgumentMap
void cb::argparser::ArgumentParser::add_argument(const std::string &short_name, const std::string &name, void cb::argparser::ArgumentParser::add_argument(const std::string &short_name, const std::string &name,
cb::argparser::Type type, std::string description, bool default_value, cb::argparser::Type type, std::string description, bool default_value,
cb::argparser::Mode mode) { cb::argparser::Mode mode) {
assert(type == Type::BOOL); assert(type == Type::BOOL_TYPE);
std::string value = default_value ? "1" : ""; std::string value = default_value ? "1" : "";
add_argument(short_name, name, type, std::move(description), value, mode); add_argument(short_name, name, type, std::move(description), value, mode);
} }
@ -176,6 +176,6 @@ void cb::argparser::ArgumentParser::add_argument(const std::string &short_name,
void cb::argparser::ArgumentParser::add_argument(const std::string &short_name, const std::string &name, void cb::argparser::ArgumentParser::add_argument(const std::string &short_name, const std::string &name,
cb::argparser::Type type, std::string description, int default_value, cb::argparser::Type type, std::string description, int default_value,
cb::argparser::Mode mode) { cb::argparser::Mode mode) {
assert(type == Type::INTEGER); assert(type == Type::INTEGER_TYPE);
add_argument(short_name, name, type, std::move(description), std::to_string(default_value), mode); add_argument(short_name, name, type, std::move(description), std::to_string(default_value), mode);
} }

Loading…
Cancel
Save