diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-06-17 19:13:34 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-05 15:43:14 +0200 |
commit | 3d6a035d0f2b8022ce7026c675ab395d30d2582c (patch) | |
tree | 94c21bdec1f022c7449bddcd1de1bcacc922a30f /Shell | |
parent | 2915dcfcc314deb4b41efdf14768e527614dc3fc (diff) | |
download | serenity-3d6a035d0f2b8022ce7026c675ab395d30d2582c.zip |
Shell: Use ArgsParser for argument parsing
Diffstat (limited to 'Shell')
-rw-r--r-- | Shell/main.cpp | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Shell/main.cpp b/Shell/main.cpp index e3dcb0b5ad..20ef531e51 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -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; |