mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:34:59 +00:00
gron: Make gron read from stdin if no file is provided
This commit is contained in:
parent
7ac196974d
commit
d09fb8f599
1 changed files with 20 additions and 9 deletions
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue