HexEditor: Parse arguments using ArgsParser

This commit is contained in:
Sam Atkins 2024-01-30 12:41:21 +00:00 committed by Sam Atkins
parent 56caee44e3
commit 4cef57a021

View file

@ -2,12 +2,14 @@
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
* Copyright (c) 2021, Conor Byrne <conor@cbyrne.dev>
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "HexEditorWidget.h"
#include <LibConfig/Client.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystemAccessClient/Client.h>
@ -24,6 +26,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app = TRY(GUI::Application::create(arguments));
StringView filename;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(filename, "File to open", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/HexEditor.md") }));
TRY(Desktop::Launcher::seal_allowlist());
@ -54,9 +62,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->show();
window->set_icon(app_icon.bitmap_for_size(16));
if (arguments.argc > 1) {
if (!filename.is_empty()) {
// FIXME: Using `try_request_file_read_only_approved` doesn't work here since the file stored in the editor is only readable.
auto response = FileSystemAccessClient::Client::the().request_file(window, arguments.strings[1], Core::File::OpenMode::ReadWrite);
auto response = FileSystemAccessClient::Client::the().request_file(window, filename, Core::File::OpenMode::ReadWrite);
if (!response.is_error())
hex_editor_widget->open_file(response.value().filename(), response.value().release_stream());
}