You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
|
|
#ifndef CB_ARGUMENTPARSER_H
|
|
#define CB_ARGUMENTPARSER_H
|
|
|
|
#include "argument.h"
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <iostream>
|
|
#include <unordered_map>
|
|
#include <cassert>
|
|
#include <algorithm>
|
|
|
|
|
|
namespace cb::argparser {
|
|
using ArgumentMapType = std::unordered_map<std::string, cb::argparser::Argument>;
|
|
|
|
class ArgumentMap : public ArgumentMapType {
|
|
public:
|
|
|
|
Argument &operator[](std::string &&argument_name) {
|
|
assert(count(argument_name) > 0 && "Argument not found!");
|
|
return this->at(argument_name);
|
|
}
|
|
};
|
|
|
|
|
|
class ArgumentParser {
|
|
private:
|
|
std::vector<ParseArgument> arguments;
|
|
std::vector<ParseArgument> positional_arguments;
|
|
|
|
public:
|
|
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
|
|
bool default_value,
|
|
Mode mode = Mode::OPTIONAL);
|
|
|
|
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
|
|
const char *default_value,
|
|
Mode mode = Mode::OPTIONAL);
|
|
|
|
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
|
|
int default_value,
|
|
Mode mode = Mode::OPTIONAL);
|
|
|
|
void add_argument(const std::string &short_name, const std::string &name, Type type, std::string description,
|
|
std::string default_value = "",
|
|
Mode mode = Mode::OPTIONAL);
|
|
|
|
ArgumentMap parse(int argc, char *argv[]);
|
|
|
|
private:
|
|
static void insert_argument(ArgumentMap &parsed_args, ParseArgument &positional_arg, const Argument &argument);
|
|
|
|
ArgumentMap parse_arguments(int argc, char *const *argv);
|
|
|
|
static bool requiresAdditionalArgument(const ParseArgument &parseArgument, int argc, char *const *argv, int i);
|
|
|
|
static void consume_argument(ArgumentMap &parsed_args, ParseArgument &arg, std::string content = "1");
|
|
|
|
void verify_required_arguments_present() const;
|
|
|
|
static bool matchesArgument(const std::string &text, const ParseArgument &arg);
|
|
|
|
bool argument_exists(const std::string &short_name, const std::string &name);
|
|
};
|
|
|
|
}
|
|
|
|
#endif //CB_ARGUMENTPARSER_H
|