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.

64 lines
1.7 KiB
C++

#ifndef GUARD_DRAMATICALLY_MACROPLANKTONIC_SPEED_WALKING_CAVILS_5903
#define GUARD_DRAMATICALLY_MACROPLANKTONIC_SPEED_WALKING_CAVILS_5903
#pragma once
#include <functional>
#include <span>
#include <stdexcept>
#include <string_view>
struct Option
{
std::string_view key, value_placeholder, help;
std::function<void (std::string_view)> apply;
};
template <typename T>
T FromString(std::string_view sv) = delete;
template<> bool FromString(std::string_view sv);
#define INT_TYPES(x) \
x(char) x(unsigned char) x(signed char) \
x(short) x(unsigned short) x(int) x(unsigned int) \
x(long) x(unsigned long) x(long long) x(unsigned long long)
#define FROM_STRING_SPEC(x) template<> x FromString(std::string_view sv);
INT_TYPES(FROM_STRING_SPEC)
#undef FROM_STRING_SPEC
template<> float FromString(std::string_view sv);
template<> double FromString(std::string_view sv);
struct ParseError : std::runtime_error
{ using std::runtime_error::runtime_error; };
struct Exit { int code; };
using ParseCallback = void (std::string_view key, std::string_view value);
namespace Detail
{
void ParseOneOption(
std::string_view sv, const std::function<ParseCallback>& cb);
}
inline void ParseOptions(
std::span<std::string_view> args, const std::function<ParseCallback>& cb)
{ for (std::string_view sv : args) Detail::ParseOneOption(sv, cb); }
inline void ParseArgs(int argc, char** argv, const std::function<ParseCallback>& cb)
{
for (int i = 1; i < argc; ++i)
Detail::ParseOneOption(argv[i], cb);
}
bool HandleOption(
std::span<Option> opts, std::string_view key, std::string_view val);
void PrintHelp(std::span<const Option> opts);
// implement this
int Main(int argc, char** argv);
#endif