diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-05-15 15:32:06 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-16 12:54:18 +0200 |
commit | 3aff3c610a390d6dabb5f586183b06f31a8a798b (patch) | |
tree | e551f3415ab50a91c07b0d44e4c6159a5b22f404 /Userland/Utilities | |
parent | 45c429853ac1e2626edfe38f96613216d450ec0e (diff) | |
download | serenity-3aff3c610a390d6dabb5f586183b06f31a8a798b.zip |
ps: Add `-p` option, and distinguish it from `-q`
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/ps.cpp | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Userland/Utilities/ps.cpp b/Userland/Utilities/ps.cpp index 33b5c36502..53959591ae 100644 --- a/Userland/Utilities/ps.cpp +++ b/Userland/Utilities/ps.cpp @@ -78,6 +78,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) bool every_process_flag = false; bool every_terminal_process_flag = false; bool full_format_flag = false; + bool provided_pid_list = false; + bool provided_quick_pid_list = false; Vector<pid_t> pid_list; Core::ArgsParser args_parser; @@ -85,7 +87,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) args_parser.add_option(every_process_flag, "Show every process", nullptr, 'A'); args_parser.add_option(every_process_flag, "Show every process (Equivalent to -A)", nullptr, 'e'); args_parser.add_option(full_format_flag, "Full format", nullptr, 'f'); - args_parser.add_option(make_list_option(pid_list, "A comma- or space-separated list of PIDs. Only processes matching those PIDs will be selected", nullptr, 'q', "pid-list", [](StringView pid_string) { + args_parser.add_option(make_list_option(pid_list, "Show processes with a matching PID. (Comma- or space-separated list)", nullptr, 'p', "pid-list", [&](StringView pid_string) { + provided_pid_list = true; + auto pid = pid_string.to_int(); + if (!pid.has_value()) + warnln("Could not parse '{}' as a PID.", pid_string); + return pid; + })); + args_parser.add_option(make_list_option(pid_list, "Show processes with a matching PID. (Comma- or space-separated list.) Processes will be listed in the order given.", nullptr, 'q', "pid-list", [&](StringView pid_string) { + provided_quick_pid_list = true; auto pid = pid_string.to_int(); if (!pid.has_value()) warnln("Could not parse '{}' as a PID.", pid_string); @@ -93,6 +103,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) })); args_parser.parse(arguments); + if (provided_pid_list && provided_quick_pid_list) { + warnln("`-p` and `-q` cannot be specified together."); + return 1; + } + Vector<Column> columns; Optional<size_t> uid_column; @@ -132,9 +147,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) if (!pid_list.is_empty()) { every_process_flag = true; + processes.remove_all_matching([&](auto& process) { return !pid_list.contains_slow(process.pid); }); + } - processes.remove_all_matching([&](auto& a) { return !pid_list.contains_slow(a.pid); }); - + if (provided_quick_pid_list) { auto processes_sort_predicate = [&pid_list](auto& a, auto& b) { return pid_list.find_first_index(a.pid).value() < pid_list.find_first_index(b.pid).value(); }; |