| 359 | } |
| 360 | |
| 361 | tl::expected<int64_t, std::string> |
| 362 | parse_signed(std::string_view value, |
| 363 | const std::optional<int64_t> min_value, |
| 364 | const std::optional<int64_t> max_value, |
| 365 | const std::string_view description) |
| 366 | { |
| 367 | const std::string stripped_value = strip_whitespace(value); |
| 368 | |
| 369 | size_t end = 0; |
| 370 | long long result = 0; |
| 371 | bool failed = false; |
| 372 | try { |
| 373 | // Note: sizeof(long long) is guaranteed to be >= sizeof(int64_t) |
| 374 | result = std::stoll(stripped_value, &end, 10); |
| 375 | } catch (std::exception&) { |
| 376 | failed = true; |
| 377 | } |
| 378 | if (failed || end != stripped_value.size()) { |
| 379 | return tl::unexpected(FMT("invalid integer: \"{}\"", stripped_value)); |
| 380 | } |
| 381 | |
| 382 | const int64_t min = min_value ? *min_value : INT64_MIN; |
| 383 | const int64_t max = max_value ? *max_value : INT64_MAX; |
| 384 | if (result < min || result > max) { |
| 385 | return tl::unexpected( |
| 386 | FMT("{} must be between {} and {}", description, min, max)); |
| 387 | } else { |
| 388 | return result; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | tl::expected<std::pair<uint64_t, SizeUnitPrefixType>, std::string> |
| 393 | parse_size(const std::string& value) |