Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
cli.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <CLI/CLI.hpp>
8#include <charconv>
9#include <concepts>
13
14#if CLI11_VERSION_MAJOR < 2 || (CLI11_VERSION_MAJOR == 2 && CLI11_VERSION_MINOR < 7) || \
15 (CLI11_VERSION_MAJOR == 2 && CLI11_VERSION_MINOR == 7 && CLI11_VERSION_PATCH < 2)
16#error "Uni20 requires CLI11 2.7.2 or newer"
17#endif
18
19namespace uni20::cli
20{
22enum class action
23{
24 run,
25 help,
26 version,
28 error
29};
31enum class no_arguments
32{
33 help,
34 run
35};
36
45
54
58void configure(CLI::App& app, presentation::program_info const& program);
59
62[[nodiscard]] parse_result parse(CLI::App& app, int argc, char const* const* argv, parse_policy policy = {});
63
65[[nodiscard]] std::vector<presentation::help_group> help_groups(CLI::App const& app);
66
68[[nodiscard]] presentation::report_builder help_report(CLI::App const& app, presentation::program_info const& program);
69
72
74[[nodiscard]] presentation::report_builder result_report(CLI::App const& app, presentation::program_info const& program,
75 parse_result const& result);
76
77namespace detail
78{
79template <typename T, typename Converter>
80CLI::Option* converted_option(CLI::App& app, std::string names, T& target, std::string description, Converter converter)
81{
82 auto* option = app.add_option_function<std::string>(
83 names,
84 [&target, converter, names](std::string const& token) {
85 try
86 {
87 target = converter(token);
88 }
89 catch (std::invalid_argument const& error)
90 {
91 throw CLI::ValidationError(names, error.what());
92 }
93 catch (std::out_of_range const& error)
94 {
95 throw CLI::ValidationError(names, error.what());
96 }
97 catch (std::runtime_error const& error)
98 {
99 throw CLI::ValidationError(names, error.what());
100 }
101 },
102 std::move(description));
103 return option->run_callback_for_default();
104}
105} // namespace detail
106
110template <std::unsigned_integral T>
111 requires(!std::same_as<T, bool>)
112CLI::Option* add_count_option(CLI::App& app, std::string names, T& value, std::string description = {})
113{
114 auto* option =
115 detail::converted_option(app, std::move(names), value, std::move(description), [](std::string const& text) {
116 T parsed{};
117 auto const result = std::from_chars(text.data(), text.data() + text.size(), parsed);
118 if (result.ec != std::errc{} || result.ptr != text.data() + text.size())
119 throw std::invalid_argument("expected a representable nonnegative decimal integer: " + text);
120 return parsed;
121 });
122 return option->type_name("COUNT")->default_function([&value] { return fmt::format("{}", value); });
123}
124
128template <std::signed_integral T>
129CLI::Option* add_half_int_option(CLI::App& app, std::string names, basic_half_int<T>& value,
130 std::string description = {})
131{
132 auto* option = detail::converted_option(app, std::move(names), value, std::move(description),
133 [](std::string const& text) { return basic_half_int<T>::parse(text); });
134 return option->type_name("HALF_INT")->default_function([&value] { return uni20::to_string(value); });
135}
136} // namespace uni20::cli
Signed value type that stores multiples of one half exactly.
Definition half_int.hpp:34
static auto parse(std::string_view text) -> basic_half_int
Parse a textual half-integer representation.
Definition half_int.hpp:172
Builder for simple terminal reports with headings, status lines, fields, and tables.
Definition presentation.hpp:602
locale_t value
Definition data_table.cpp:49
presentation::styled_text text
Definition display.cpp:141
Defines basic_half_int<T>, a signed integer type with half-integer resolution.
Definition cli.cpp:6
std::vector< presentation::help_group > help_groups(CLI::App const &app)
Project CLI11 option metadata into parser-independent semantic help groups.
Definition cli.cpp:150
CLI::Option * add_half_int_option(CLI::App &app, std::string names, basic_half_int< T > &value, std::string description={})
Bind an exact half-integer, accepting integer, .0/.5 decimal and denominator-two fractional forms.
Definition cli.hpp:129
presentation::report_builder help_report(CLI::App const &app, presentation::program_info const &program)
Build full help from the same options used for parsing, plus application-supplied content.
Definition cli.cpp:157
void configure(CLI::App &app, presentation::program_info const &program)
Register common help/version/build-info actions on a single-command application.
Definition cli.cpp:102
presentation::report_builder result_report(CLI::App const &app, presentation::program_info const &program, parse_result const &result)
Build the selected informational/error document; a run outcome has no document and is rejected.
Definition cli.cpp:190
parse_result parse(CLI::App &app, int argc, char const *const *argv, parse_policy policy)
Parse argc/argv, preserving CLI11's argument boundaries and help precedence.
Definition cli.cpp:116
presentation::report_builder build_info_report(presentation::program_info const &program)
Build application and existing Uni20 build information without running numerical initialization.
Definition cli.cpp:166
no_arguments
Whether an empty invocation displays help or parses application defaults.
Definition cli.hpp:32
CLI::Option * add_count_option(CLI::App &app, std::string names, T &value, std::string description={})
Bind an unsigned decimal count with complete-token, negative and overflow checks.
Definition cli.hpp:112
action
The executable decides whether to calculate or render an informational/error result.
Definition cli.hpp:23
stream
Selects the ordinary display stream for a message.
Definition display.hpp:25
auto to_string(basic_half_int< T > value) -> std::string
Format a half-integer as an integer or decimal .5.
Definition half_int.hpp:477
Parser-independent application identity and help documents.
Executable policy for empty invocations and usage failures.
Definition cli.hpp:39
int empty_exit_code
Definition cli.hpp:42
display::stream empty_destination
Definition cli.hpp:41
int error_exit_code
Definition cli.hpp:43
no_arguments empty
Definition cli.hpp:40
Owned parse outcome; parsing itself does not print, open files or terminate the process.
Definition cli.hpp:48
int exit_code
Definition cli.hpp:51
std::string message
Definition cli.hpp:52
action requested
Definition cli.hpp:49
display::stream destination
Definition cli.hpp:50
Application identity and explanatory content, independent of any option parser.
Definition program.hpp:32