1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 17:24:57 +00:00

Shell: Use ArgsParser for argument parsing

This commit is contained in:
AnotherTest 2020-06-17 19:13:34 +04:30 committed by Andreas Kling
parent 2915dcfcc3
commit 3d6a035d0f

View file

@ -26,6 +26,7 @@
#include "Execution.h"
#include "Shell.h"
#include <LibCore/ArgsParser.h>
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
@ -116,14 +117,23 @@ int main(int argc, char** argv)
return shell->complete(editor);
};
if (argc > 2 && !strcmp(argv[1], "-c")) {
dbgprintf("sh -c '%s'\n", argv[2]);
shell->run_command(argv[2]);
const char* command_to_run = nullptr;
const char* file_to_read_from = nullptr;
Core::ArgsParser parser;
parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string");
parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No);
parser.parse(argc, argv);
if (command_to_run) {
dbgprintf("sh -c '%s'\n", command_to_run);
shell->run_command(command_to_run);
return 0;
}
if (argc == 2 && argv[1][0] != '-') {
auto file = Core::File::construct(argv[1]);
if (file_to_read_from && StringView { "-" } != file_to_read_from) {
auto file = Core::File::construct(file_to_read_from);
if (!file->open(Core::IODevice::ReadOnly)) {
fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string());
return 1;