-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.cppm
More file actions
83 lines (72 loc) · 2.4 KB
/
options.cppm
File metadata and controls
83 lines (72 loc) · 2.4 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module;
export module mcpplibs.cmdline:options;
import std;
namespace mcpplibs::cmdline::detail {
export struct Arg {
std::string name;
std::string help_;
bool required_ = false;
std::string default_val;
constexpr Arg(std::string_view name) : name(name) {}
[[nodiscard]] constexpr Arg& help(std::string_view help_text) {
help_ = help_text;
return *this;
}
[[nodiscard]] constexpr Arg& required(bool required = true) {
required_ = required;
return *this;
}
[[nodiscard]] constexpr Arg& default_value(std::string_view value) {
default_val = value;
return *this;
}
};
export struct Option {
char short_ = '\0';
std::string long_name;
std::string help_;
bool takes_value_ = false;
bool multiple_ = false;
bool global_ = false;
std::string value_name_;
constexpr Option(std::string_view long_opt) : long_name(long_opt) {}
constexpr Option(char short_char) : short_(short_char) {}
[[nodiscard]] constexpr Option& short_name(char short_char) {
short_ = short_char;
return *this;
}
[[nodiscard]] Option& long_opt(std::string_view long_opt) {
long_name = long_opt;
return *this;
}
[[nodiscard]] constexpr Option& help(std::string_view help_text) {
help_ = help_text;
return *this;
}
[[nodiscard]] constexpr Option& takes_value(bool takes = true) {
takes_value_ = takes;
return *this;
}
[[nodiscard]] constexpr Option& multiple(bool multiple = true) {
multiple_ = multiple;
return *this;
}
[[nodiscard]] constexpr Option& global(bool global = true) {
global_ = global;
return *this;
}
[[nodiscard]] constexpr Option& value_name(std::string_view value_name) {
value_name_ = value_name;
return *this;
}
[[nodiscard]] bool matches_short(char short_char) const { return short_ && short_ == short_char; }
[[nodiscard]] bool matches_long(std::string_view long_opt) const {
return !long_name.empty() && long_name == long_opt;
}
[[nodiscard]] bool matches(std::string_view token) const {
if (token.size() == 2 && token[0] == '-' && token[1] == short_) return true;
if (token.size() > 2 && token.starts_with("--") && token.substr(2) == long_name) return true;
return false;
}
};
} // namespace mcpplibs::cmdline::detail