Repository navigation
Expand file tree
/
Copy path05_validation.cpp
More file actions
127 lines (111 loc) · 4.3 KB
/
Copy path05_validation.cpp
File metadata and controls
127 lines (111 loc) · 4.3 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <clapp/clapp.hpp>
#include <cstdint>
#include <expected>
#include <optional>
#include <print>
#include <string>
#include <string_view>
namespace {
/** \brief How fast to run. A plain enum: the accepted spellings come from reflection. */
enum class run_mode : unsigned char {
fast[[= clapp::value{.help = "Run swiftly"}]],
slow[[= clapp::value{.help = "Crawl slowly but steadily"}]],
};
/**
* \brief Spell a #run_mode the way the command line does.
* \param mode The parsed value.
* \return A view into a string literal.
*/
[[nodiscard]] constexpr std::string_view name_of(run_mode mode) noexcept {
switch (mode) {
case run_mode::fast:
return "fast";
case run_mode::slow:
return "slow";
}
return "?";
}
/** \brief A TCP port, which is a `std::uint16_t` that may not be zero. */
struct port_number {
std::uint16_t value = 0;
};
/** \brief The root command. */
struct[[= clapp::cmd{
.name = "05_validation",
.version = "1.0.0",
.about = "Value sets, range checks and argument relations"
}]] cli {
/** Required, and restricted to the enumerators of #run_mode. */
[[= clapp::arg{
.index = 1,
.help = "What mode to run the program in",
.value_name = "MODE"
}]] run_mode mode;
/** Optional, and validated by the specialization above. */
[[= clapp::arg{
.long_ = "port",
.help = "Network port to use",
.value_name = "PORT"
}]] std::optional<port_number>
port;
/** Either this or `--major`, never both and never neither. */
[[= clapp::arg{
.long_ = "set-ver",
.help = "Set the version manually",
.value_name = "VER"
}]][[= clapp::conflicts_with{"major"}]][
[= clapp::required_unless_any{"major"}]] std::optional<std::string>
set_ver;
[[= clapp::arg{
.long_ = "major",
.help = "Auto-increment the major version instead"
}]] bool major;
};
}
/**
* \brief Parse a #port_number, rejecting anything outside 1-65535.
*
* \note The whole of clap's `04_02_validate.rs` fits in one static function, because a
* clapp::value_parser specialization is where the type's own rules belong. Two
* details are worth copying into your own:
*
* * Delegate the numeric half to `clapp::value_parser<unsigned long>` rather than
* reimplementing it, so `--port eight` reports the same sentence a bare integer
* argument would.
* * Fill in `domain`. It is what turns "out of range" into a message that says
* *which* range, and it costs one string literal.
*/
template<>
struct clapp::value_parser<port_number> {
/**
* \brief Convert \p value into a #port_number.
* \param value The raw bytes from the command line.
* \return The port, or the error explaining why those bytes are not one.
*/
[[nodiscard]] static constexpr std::expected<port_number, clapp::parse_error>
parse(clapp::os_str value) {
const std::expected<unsigned long, clapp::parse_error> number =
clapp::value_parser<unsigned long>::parse(value);
if (!number.has_value()) return std::unexpected(number.error());
if (*number < 1 || *number > 65535)
return std::unexpected(clapp::parse_error{
.kind = clapp::parse_error_kind::out_of_range,
.input = value,
.type_name = "port_number",
.reason = "port not in range 1-65535",
.domain = "1..=65535"
});
return port_number{.value = static_cast<std::uint16_t>(*number)};
}
};
int main(int argc, char **argv) {
const cli args = clapp::parse<cli>(argc, argv);
std::println("mode: {}", name_of(args.mode));
if (args.port.has_value())
std::println("port: Some({})", args.port->value);
else
std::println("port: None");
// The relations above are what make this total: exactly one of the two was given.
const std::string version = args.set_ver.has_value() ? *args.set_ver : std::string{"2.0.0"};
std::println("version: {}", version);
}