Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
ui_lyra.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_app.hpp>
6
7#include <lyra/lyra.hpp>
8
9#include <sstream>
10#include <utility>
11
12namespace portals::ui::lyra {
13
15struct CliOptions {
16 bool automaticHelp{true};
17 std::string version{};
18 const bool *helpRequested{};
19};
20
32inline CommandLineParser cli(::lyra::cli parser, CliOptions options = {}) {
33 return [parser = std::move(parser), options = std::move(options)](
34 const app::Arguments &arguments) -> std::expected<LaunchAction, CommandLineError> {
35 bool showHelp{};
36 bool showVersion{};
37 auto invocation = parser;
38
39 if (options.automaticHelp) {
40 invocation |= ::lyra::help(showHelp);
41 }
42
43 if (!options.version.empty()) {
44 invocation |= ::lyra::opt([&showVersion](bool flag) {
45 showVersion = flag;
46
47 return ::lyra::parser_result::ok(::lyra::parser_result_type::short_circuit_all);
48 })["--version"]("Display version information.");
49 }
50
51 const auto result = invocation.parse(::lyra::args(arguments.begin(), arguments.end()));
52
53 if (!result) {
54 return std::unexpected(CommandLineError{CommandLineErrorCode::INVALID_ARGUMENT, result.message()});
55 }
56
57 if (showHelp || (options.helpRequested && *options.helpRequested)) {
58 std::ostringstream output;
59
60 output << invocation;
61
62 return LaunchAction{LaunchDisposition::FINISH, std::move(output).str()};
63 }
64
65 if (showVersion) {
66 return LaunchAction{LaunchDisposition::FINISH, options.version + '\n'};
67 }
68
69 return LaunchAction{};
70 };
71}
72
73} // namespace portals::ui::lyra
Optional built-in launch actions; parsing itself remains Lyra's responsibility.
Definition ui_lyra.hpp:15
const bool * helpRequested
Optional caller-owned custom help flag; must outlive invocation.
Definition ui_lyra.hpp:18
bool automaticHelp
Add Lyra's -?, -h and –help; disable when supplying custom help.
Definition ui_lyra.hpp:16
std::string version
Nonempty text enables a short-circuiting –version option.
Definition ui_lyra.hpp:17