-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_line.h
55 lines (50 loc) · 1.83 KB
/
cmd_line.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include <charconv> // for from_chars, from_chars_result
#include <new> // for operator new
#include <string_view> // for string_view
#include <vector> // for allocator, vector
class CmdLineParser
{
public:
CmdLineParser(int argc, char** argv);
struct has_args_tag
{
};
inline static constexpr has_args_tag has_args{};
std::vector<std::string_view> Get(std::string_view arg, has_args_tag) const;
bool Get(std::string_view arg) const;
private:
std::vector<std::string_view> m_CmdLine;
};
template <class T>
T GetCmdLineParam(const CmdLineParser& parser, std::string_view arg, const T& default_value);
template <>
inline std::vector<std::string_view>
GetCmdLineParam<std::vector<std::string_view>>(const CmdLineParser& parser, std::string_view arg, const std::vector<std::string_view>& default_value)
{
const auto ret = parser.Get(arg, CmdLineParser::has_args);
return ret.empty() ? default_value : ret;
}
template <>
inline std::string_view GetCmdLineParam<std::string_view>(const CmdLineParser& parser, std::string_view arg, const std::string_view& default_value)
{
const auto ret = parser.Get(arg, CmdLineParser::has_args);
return ret.empty() ? default_value : ret.front();
}
template <>
inline int GetCmdLineParam<int>(const CmdLineParser& parser, std::string_view arg, const int& default_value)
{
int ret;
std::string_view param = GetCmdLineParam<std::string_view>(parser, arg, "none");
std::from_chars_result char_conv_result = std::from_chars(param.data(), param.data() + param.size(), ret);
if (char_conv_result.ptr == arg.data())
{
return default_value;
}
return ret;
}
template <>
inline bool GetCmdLineParam<bool>(const CmdLineParser& parser, std::string_view arg, const bool& default_value)
{
return default_value || parser.Get(arg);
}