mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 16:37:47 +00:00
LibCore+Everywhere: Remove ArgsParser::add*(char const*&)
This is not guaranteed to always work correctly as ArgsParser deals in StringViews and might have a non-properly-null-terminated string as a value. As a bonus, using StringView (and DeprecatedString where necessary) leads to nicer looking code too :^)
This commit is contained in:
parent
60908adcbe
commit
500044906d
43 changed files with 122 additions and 145 deletions
|
@ -28,7 +28,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
Config::pledge_domain("FontEditor");
|
||||
TRY(Core::System::pledge("stdio recvfd sendfd thread rpath cpath wpath"));
|
||||
|
||||
char const* path = nullptr;
|
||||
StringView path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "The font file for editing.", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -42,7 +42,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto font_editor = TRY(window->set_main_widget<FontEditor::MainWidget>());
|
||||
TRY(font_editor->initialize_menubar(*window));
|
||||
|
||||
if (path) {
|
||||
if (!path.is_empty()) {
|
||||
TRY(font_editor->open_file(path));
|
||||
} else {
|
||||
auto mutable_font = TRY(TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/KaticaRegular10.font"))->unmasked_character_set());
|
||||
|
|
|
@ -51,7 +51,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto app_icon = GUI::Icon::default_icon("filetype-image"sv);
|
||||
|
||||
char const* path = nullptr;
|
||||
StringView path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "The image file to be displayed.", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -68,7 +68,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto main_toolbar = TRY(toolbar_container->try_add<GUI::Toolbar>());
|
||||
|
||||
auto widget = TRY(root_widget->try_add<ViewWidget>());
|
||||
if (path) {
|
||||
if (!path.is_empty()) {
|
||||
widget->set_path(path);
|
||||
}
|
||||
widget->on_scale_change = [&](float scale) {
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
char const* file_path = nullptr;
|
||||
StringView file_path;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(file_path, "PDF file to open", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -45,7 +45,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
if (file_path) {
|
||||
if (!file_path.is_empty()) {
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, file_path);
|
||||
if (response.is_error())
|
||||
return 1;
|
||||
|
|
|
@ -26,7 +26,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
Config::pledge_domain("PixelPaint");
|
||||
|
||||
char const* image_file = nullptr;
|
||||
StringView image_file;
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(image_file, "Image file to open", "path", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
@ -72,7 +72,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
window->show();
|
||||
|
||||
if (image_file) {
|
||||
if (!image_file.is_empty()) {
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, image_file);
|
||||
if (response.is_error())
|
||||
return 1;
|
||||
|
|
|
@ -26,15 +26,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
|
||||
char const* filename = nullptr;
|
||||
StringView filename;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(filename, "File to read from", "file", Core::ArgsParser::Required::No);
|
||||
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (filename) {
|
||||
if (!Core::DeprecatedFile::exists({ filename, strlen(filename) }) || Core::DeprecatedFile::is_directory(filename)) {
|
||||
if (!filename.is_empty()) {
|
||||
if (!Core::DeprecatedFile::exists(filename) || Core::DeprecatedFile::is_directory(filename)) {
|
||||
warnln("File does not exist or is a directory: {}", filename);
|
||||
return 1;
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->resize(640, 480);
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
auto spreadsheet_widget = TRY(window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename == nullptr));
|
||||
auto spreadsheet_widget = TRY(window->set_main_widget<Spreadsheet::SpreadsheetWidget>(*window, NonnullRefPtrVector<Spreadsheet::Sheet> {}, filename.is_empty()));
|
||||
|
||||
spreadsheet_widget->initialize_menubar(*window);
|
||||
spreadsheet_widget->update_window_title();
|
||||
|
@ -64,7 +64,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
window->show();
|
||||
|
||||
if (filename) {
|
||||
if (!filename.is_empty()) {
|
||||
auto file = TRY(FileSystemAccessClient::Client::the().request_file_read_only_approved(window, filename));
|
||||
spreadsheet_widget->load_file(file.filename(), file.stream());
|
||||
}
|
||||
|
|
|
@ -251,7 +251,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
Config::pledge_domain("Terminal");
|
||||
|
||||
char const* command_to_execute = nullptr;
|
||||
StringView command_to_execute;
|
||||
bool keep_open = false;
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
|
@ -260,7 +260,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if (keep_open && !command_to_execute) {
|
||||
if (keep_open && command_to_execute.is_empty()) {
|
||||
warnln("Option -k can only be used in combination with -e.");
|
||||
return 1;
|
||||
}
|
||||
|
@ -273,7 +273,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
if (shell_pid == 0) {
|
||||
close(ptm_fd);
|
||||
if (command_to_execute)
|
||||
if (!command_to_execute.is_empty())
|
||||
TRY(run_command(command_to_execute, keep_open));
|
||||
else
|
||||
TRY(run_command(Config::read_string("Terminal"sv, "Startup"sv, "Command"sv, ""sv), false));
|
||||
|
|
|
@ -27,7 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
app->set_config_domain(TRY("TextEditor"_string));
|
||||
|
||||
auto preview_mode = "auto"sv;
|
||||
char const* file_to_edit = nullptr;
|
||||
StringView file_to_edit;
|
||||
Core::ArgsParser parser;
|
||||
parser.add_option(preview_mode, "Preview mode, one of 'none', 'html', 'markdown', 'auto'", "preview-mode", '\0', "mode");
|
||||
parser.add_positional_argument(file_to_edit, "File to edit, with optional starting line and column number", "file[:line[:column]]", Core::ArgsParser::Required::No);
|
||||
|
@ -73,8 +73,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
window->show();
|
||||
window->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
if (file_to_edit) {
|
||||
auto filename = TRY(String::from_utf8(StringView(file_to_edit, strlen(file_to_edit))));
|
||||
if (!file_to_edit.is_empty()) {
|
||||
auto filename = TRY(String::from_utf8(file_to_edit));
|
||||
FileArgument parsed_argument(filename);
|
||||
auto response = FileSystemAccessClient::Client::the().request_file_read_only_approved(window, parsed_argument.filename().to_deprecated_string());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue