diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-22 15:44:54 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-22 19:28:31 +0100 |
commit | d3cf68a5407a756e79a5b2f97303b855b7253e86 (patch) | |
tree | c682d881aa21bcdeeebd6c4056127113e49c9ebd /Userland/Libraries/LibMain/Main.cpp | |
parent | 317ceb0ee22ee4bceaad97773082a425f0694a94 (diff) | |
download | serenity-d3cf68a5407a756e79a5b2f97303b855b7253e86.zip |
LibMain: Add a new library for more ergonomic userspace entry functions
By linking with LibMain, your program no longer needs to provide main().
Instead, execution begins in this function:
ErrorOr<int> serenity_main(Main::Arguments);
This allows programs that link with LibMain to use TRY() already in
their entry function, without having to do manual ErrorOr unwrapping.
This is very experimental, but it seems like a nice idea so let's try it
out. :^)
Diffstat (limited to 'Userland/Libraries/LibMain/Main.cpp')
-rw-r--r-- | Userland/Libraries/LibMain/Main.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Userland/Libraries/LibMain/Main.cpp b/Userland/Libraries/LibMain/Main.cpp new file mode 100644 index 0000000000..1cee6c2e49 --- /dev/null +++ b/Userland/Libraries/LibMain/Main.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/Format.h> +#include <AK/StringView.h> +#include <AK/Vector.h> +#include <LibMain/Main.h> + +int main(int argc, char** argv) +{ + Vector<StringView> arguments; + arguments.ensure_capacity(argc); + for (int i = 0; i < argc; ++i) + arguments.unchecked_append(argv[i]); + + auto result = serenity_main({ + .argc = argc, + .argv = argv, + .arguments = arguments.span(), + }); + if (result.is_error()) { + warnln("Runtime error: {}", result.error()); + return 1; + } + return result.value(); +} |