2023-07-29 02:03:28 -04:00
|
|
|
/*
|
|
|
|
* Created by Brett on 28/07/23.
|
|
|
|
* Licensed under GNU General Public License V3.0
|
|
|
|
* See LICENSE file for license detail
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BLT_TESTS_ARGPARSE_H
|
|
|
|
#define BLT_TESTS_ARGPARSE_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#ifndef HASHMAP
|
2023-07-29 13:38:19 -04:00
|
|
|
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
|
|
|
|
#define HASHMAP HASHMAP
|
|
|
|
|
|
|
|
#include <parallel_hashmap/phmap.h>
|
|
|
|
#include <parallel_hashmap/phmap_fwd_decl.h>
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
using HASHMAP = phmap::flat_hash_map<K, V>();
|
|
|
|
template<typename K>
|
|
|
|
using HASHSET = phmap::flat_hash_set<K>();
|
|
|
|
#else
|
|
|
|
#define HASHMAP HASHMAP
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
template<typename K, typename V>
|
|
|
|
using HASHMAP = std::unordered_map<K, V>();
|
|
|
|
|
|
|
|
template<typename K>
|
|
|
|
using HASHSET = std::unordered_set<K>();
|
|
|
|
#endif
|
2023-07-29 02:03:28 -04:00
|
|
|
#endif
|
|
|
|
|
2023-07-29 13:38:19 -04:00
|
|
|
namespace blt::parser {
|
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
enum class arg_action {
|
2023-07-29 13:38:19 -04:00
|
|
|
STORE,
|
|
|
|
STORE_CONST,
|
|
|
|
STORE_TRUE,
|
|
|
|
STORE_FALSE,
|
|
|
|
APPEND,
|
|
|
|
APPEND_CONST,
|
|
|
|
COUNT,
|
|
|
|
HELP,
|
|
|
|
VERSION,
|
|
|
|
EXTEND
|
2023-07-29 02:03:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class arg_t {
|
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class arg_vector {
|
|
|
|
private:
|
|
|
|
std::vector<std::string> names;
|
|
|
|
std::vector<std::string> flags;
|
|
|
|
|
|
|
|
void insertAndSort(const std::string& str);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
public:
|
|
|
|
arg_vector() = default;
|
2023-07-29 13:38:19 -04:00
|
|
|
|
|
|
|
arg_vector(const std::vector<std::string>& args);
|
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector(std::initializer_list<std::string> args);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector(const std::string& arg);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector(const char* arg);
|
|
|
|
|
|
|
|
arg_vector& operator=(const std::string& arg);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector& operator=(const char* arg);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector& operator=(std::initializer_list<std::string>& args);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_vector& operator=(std::vector<std::string>& args);
|
|
|
|
|
2023-07-29 13:38:19 -04:00
|
|
|
[[nodiscard]] inline std::vector<std::string>& getNames() {
|
2023-07-29 02:03:28 -04:00
|
|
|
return names;
|
|
|
|
}
|
2023-07-29 13:38:19 -04:00
|
|
|
|
|
|
|
[[nodiscard]] inline std::vector<std::string>& getFlags() {
|
2023-07-29 02:03:28 -04:00
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class arg_nargs {
|
|
|
|
private:
|
|
|
|
static constexpr int UNKNOWN = 0x1;
|
|
|
|
static constexpr int ALL = 0x2;
|
|
|
|
static constexpr int ALL_REQUIRED = 0x4;
|
|
|
|
int args = 0;
|
|
|
|
int flags = 0;
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
void decode(char c);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
public:
|
|
|
|
arg_nargs() = default;
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs(int args): args(args) {}
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs(char c);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs(std::string s);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs(const char* s);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs& operator=(const std::string& s);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs& operator=(const char* s);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs& operator=(char c);
|
2023-07-29 13:38:19 -04:00
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
arg_nargs& operator=(int args);
|
|
|
|
};
|
|
|
|
|
2023-07-29 13:38:19 -04:00
|
|
|
struct arg_properties {
|
2023-07-29 02:03:28 -04:00
|
|
|
private:
|
|
|
|
public:
|
|
|
|
arg_vector a_flags;
|
|
|
|
arg_action a_action;
|
|
|
|
arg_nargs a_nargs;
|
|
|
|
std::optional<std::string> a_const;
|
|
|
|
std::string a_default;
|
|
|
|
std::string a_def;
|
|
|
|
std::string a_help;
|
|
|
|
std::string a_metavar;
|
|
|
|
bool a_required = false;
|
|
|
|
};
|
|
|
|
|
2023-07-29 13:38:19 -04:00
|
|
|
class arg_tokenizer {
|
|
|
|
private:
|
|
|
|
static constexpr char FLAG = '-';
|
|
|
|
std::vector<std::string> args;
|
|
|
|
size_t nextIndex = 0;
|
|
|
|
|
|
|
|
inline const std::string& get(size_t i){
|
|
|
|
return args[i];
|
|
|
|
}
|
|
|
|
inline const std::string& next(size_t& i){
|
|
|
|
return args[i++];
|
|
|
|
}
|
|
|
|
inline bool hasNext(size_t i){
|
|
|
|
return (size_t)i < args.size();
|
|
|
|
}
|
|
|
|
inline bool isFlag(size_t i){
|
|
|
|
return get(i).starts_with(FLAG);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
arg_tokenizer() = default;
|
|
|
|
|
|
|
|
arg_tokenizer(const char** argv, size_t argc);
|
|
|
|
|
|
|
|
inline void forward() {
|
|
|
|
nextIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const std::string& get() {
|
|
|
|
return get(nextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const std::string& next() {
|
|
|
|
return next(nextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasNext() {
|
|
|
|
return hasNext(nextIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isFlag() {
|
|
|
|
return isFlag(nextIndex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct arg_results {
|
2023-07-29 02:03:28 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
public:
|
2023-07-29 13:38:19 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class argparse {
|
|
|
|
private:
|
|
|
|
arg_tokenizer tokenizer;
|
|
|
|
public:
|
2023-07-29 02:03:28 -04:00
|
|
|
argparse() = default;
|
|
|
|
|
2023-07-29 13:38:19 -04:00
|
|
|
void addArgument(const arg_properties& args);
|
|
|
|
|
2023-07-29 02:03:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //BLT_TESTS_ARGPARSE_H
|