gron: Make gron read from stdin if no file is provided

This commit is contained in:
Sam Atkins 2021-06-07 21:36:34 +01:00 committed by Andreas Kling
parent 7ac196974d
commit d09fb8f599
Notes: sideshowbarker 2024-07-18 12:36:50 +09:00

View File

@ -10,6 +10,7 @@
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <stdio.h>
#include <string.h>
@ -41,15 +42,25 @@ int main(int argc, char** argv)
return 1;
}
if (argc != 2 || !strcmp(argv[1], "--help")) {
warnln("usage: gron <file>");
warnln("Print each value in a JSON file with its fully expanded key.");
return argc != 2 ? 1 : 0;
}
auto file = Core::File::construct(argv[1]);
if (!file->open(Core::OpenMode::ReadOnly)) {
warnln("Failed to open {}: {}", file->name(), file->error_string());
return 1;
Core::ArgsParser args_parser;
args_parser.set_general_help("Print each value in a JSON file with its fully expanded key.");
const char* path = nullptr;
args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
RefPtr<Core::File> file;
if (!path) {
file = Core::File::standard_input();
} else {
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
if (file_or_error.is_error()) {
warnln("Failed to open {}: {}", path, file_or_error.error());
return 1;
}
file = file_or_error.value();
}
if (pledge("stdio", nullptr) < 0) {