cli20 0.1.2
A C++20 typed-schema command-line parser
Loading...
Searching...
No Matches
parser_iostream.hh
Go to the documentation of this file.
1#pragma once
2
3#include <cstdlib>
4#include <ostream>
5
6#include "cli/parser.hh"
7
8namespace cli {
9
17template <ArgumentSpec T>
18// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays, modernize-avoid-c-arrays)
19auto parse_or_exit(int argc, char* argv[], std::ostream& out, std::ostream& err)
20 -> T {
21 auto result = parse<T>(argc, argv);
22 if (!result) {
23 if (const auto message = result.error.message(); !message.empty()) {
24 auto& stream = result.error.use_stdout() ? out : err;
25 stream << message << '\n';
26 }
27 std::exit(result.error.exit_code());
28 }
29 return std::move(result.value);
30}
31
32} // namespace cli
Definition action.hh:29
auto parse_or_exit(int argc, char *argv[]) -> T
Parses argc/argv and exits the process on failure.
Definition parser.hh:1488
auto parse(int argc, char *argv[]) -> ParseResult< T >
Convenience free function that parses argc/argv into type T.
Definition parser.hh:1464