Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
Command-line launch with Lyra

Русская версия

Connect the adapter

The optional portals::lyra target supplies the pinned, header-only Lyra 1.8.0 dependency and <portals/ui_lyra.hpp>. Link it explicitly:

find_package(Portals CONFIG REQUIRED)
target_link_libraries(my_application PRIVATE portals::lyra)

The same target works with add_subdirectory. Installed headers, CMake metadata, license and dependency manifest travel with the SDK and require no network or system Lyra installation. Core-only consumers link portals::core and do not include or parse Lyra.

Parse application options

Build a native Lyra declaration, bind it to ordinary typed options and pass the adapter to the lazy launch stage:

#include <portals/ui.hpp>
#include <portals/ui_lyra.hpp>
struct Options {
int width{80};
std::string theme{"modern"};
bool ansi16{};
};
int main(int argc, char **argv) {
namespace ui = portals::ui;
Options options;
auto parser = ::lyra::cli()
| ::lyra::opt(options.width, "columns")["-w"]["--width"](
"Requested columns.")
| ::lyra::opt(options.theme, "theme")["-t"]["--theme"](
"Palette.").choices("modern", "classic")
| ::lyra::opt(options.ansi16)["--ansi16"](
"Use 16 ANSI colors.");
return ui::scene([&](portals::ManagedScene &) {
return ui::vbox(
ui::label(options.theme),
ui::label(std::to_string(options.width)));
})
| ui::applicationOptions([&] {
settings.writer.colorMode = options.ansi16
return settings;
})
| ui::onKey(portals::key::F10, ui::quit)
| ui::reportOnError()
| ui::runApp(argc, argv,
ui::lyra::cli(parser,
{.version = "my_application 1.0"}));
}
Additive lifecycle owner over one existing Application subtree. UI affine and nonmovable....
Definition managed.hpp:530
@ ANSI16
Nearest standard RGB palette entry encoded as SGR 30-37/90-97 and 40-47/100-107.
Definition diff.hpp:19
@ TRUE_COLOR
RGB SGR 38;2/48;2, preserving the existing default.
Definition diff.hpp:18
Finite runner and component policies; one owner services all I/O.
Definition application.hpp:76
FrameWriterLimits writer
Output encoding and owned-frame limits.
Definition application.hpp:83
ColorMode colorMode
Fixed output palette for this writer's complete lifetime.
Definition diff.hpp:104

Keep bound options alive

ui::lyra::cli owns a copy of the parser declaration. It does not capture argv or parse during construction. Lyra's bound variables and reference captures remain borrowed: keep them alive through synchronous runApp. The lazy scene factory observes the parsed options. ui::opts(options) can pass the same read-only typed options into a reusable lazy scene factory. Parser/recipe construction and owning captures can allocate before the runner boundary. Callbacks invoked by the runner use its checked error policy.

Diagnostic text ownership and allocation

CommandLineError::message and LaunchAction::output use LaunchText, which shares immutable owning bytes. Empty construction, copying and moving allocate nothing, including on MSVC Debug. Its view(), data(), size(), empty() and find() accessors are read-only; keep a copy of the text alive while borrowing its bytes. Constructing nonempty text copies the supplied bytes and can allocate. When this happens inside the parser callback, allocation failure becomes NO_MEMORY with an empty diagnostic and exit status 2. Text constructed before the runner call follows the construction boundary described above.

Help and version

By default the adapter adds Lyra's -?, -h, --help. A nonempty version adds --version. These options use Lyra's own short-circuit results and return an owning LaunchAction with FINISH; runApp prints its output and returns before invoking the scene factory or acquiring a terminal. There is no std::exit. Do not also declare these option names in the supplied parser.

For custom help, declare ::lyra::help(showHelp) in the native parser and pass {.automaticHelp = false, .helpRequested = &showHelp}. The flag is borrowed and must outlive the call. This explicit observer is necessary because Lyra hides the help flag behind its callback; inspecting argument strings would bypass the parser. Initialize it to false for each launch.

Invalid arguments

Invalid numbers, missing values, unknown options and failed native .choices validation become CommandLineErrorCode::INVALID_ARGUMENT with an owning Lyra diagnostic; runApp returns status 2 for command-line failure. The generic launch boundary handles parser callback exceptions separately from widget errors. Lyra can update some bound values or invoke callbacks before a later parse failure; the adapter preserves that upstream behavior and never constructs the application after failure.

Unicode arguments

Lyra consumes the strings supplied by the launch argument snapshot. It does not convert Windows narrow main arguments into Unicode. Non-ASCII command arguments therefore depend on how the executable obtains its argv; the Windows Unicode environment snapshot is a separate facility and does not repair narrow argv. POSIX argument bytes are preserved.

Read arguments and environment

ui::app::args(), ui::app::environment() and its alias ui::app::env() return checked owning read-only snapshots after successful ui::app::initialize, including initialization performed by ui::runApp(argc, argv, parser). Before initialization they return ProcessContextError::UNINITIALIZED. Environment::get(name) returns std::expected<std::optional<std::string_view>, ProcessContextError> without throwing: an error reports an invalid query or capture/conversion failure, successful std::nullopt means the variable is missing, and an engaged empty view means an existing empty value. Keep the snapshot alive while borrowing its views. Windows lookup compares Unicode names without case; POSIX preserves native bytes and compares names exactly. Reading a snapshot never refreshes or mutates the process environment. Capture it before other threads can mutate the live environment; Portals cannot synchronize foreign environment writers.

Lyra reference and license

Lyra is licensed under BSL-1.0, independently of Portals. The unchanged notice is installed as share/Portals/licenses/lyra-LICENSE.txt; the vendored vendor/lyra/manifest.json records provenance and checksums. See the native Lyra reference for declarations, callbacks, validation, option styles and parse guarantees.