Utilities: Add option to suppress errors to grep

This commit is contained in:
Marco Cutecchia 2021-10-30 13:04:12 +02:00 committed by Ali Mohammad Pur
parent 647f89f15e
commit 0086466b61
Notes: sideshowbarker 2024-07-18 01:43:32 +09:00

View file

@ -48,6 +48,7 @@ int main(int argc, char** argv)
bool case_insensitive = false;
bool invert_match = false;
bool quiet_mode = false;
bool suppress_errors = false;
bool colored_output = isatty(STDOUT_FILENO);
Core::ArgsParser args_parser;
@ -57,6 +58,7 @@ int main(int argc, char** argv)
args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
args_parser.add_option(quiet_mode, "Do not write anything to standard output", "quiet", 'q');
args_parser.add_option(suppress_errors, "Suppress error messages for nonexistent or unreadable files", "no-messages", 's');
args_parser.add_option(Core::ArgsParser::Option {
.requires_argument = true,
.help_string = "Action to take for binary files ([binary], text, skip)",
@ -158,9 +160,10 @@ int main(int argc, char** argv)
return false;
};
auto handle_file = [&matches, binary_mode](StringView filename, bool print_filename) -> bool {
auto handle_file = [&matches, binary_mode, suppress_errors](StringView filename, bool print_filename) -> bool {
auto file = Core::File::construct(filename);
if (!file->open(Core::OpenMode::ReadOnly)) {
if (!suppress_errors)
warnln("Failed to open {}: {}", filename, file->error_string());
return false;
}