diff --git a/Base/usr/share/man/man1/ps.md b/Base/usr/share/man/man1/ps.md index 483a21d6bf..4454964522 100644 --- a/Base/usr/share/man/man1/ps.md +++ b/Base/usr/share/man/man1/ps.md @@ -5,7 +5,7 @@ ps - list currently running processes ## Synopsis ```**sh -$ ps [--version] [-a] [-A] [-e] [-f] [-q pid-list] +$ ps [--version] [-a] [-A] [-e] [-f] [-p pid-list] [-q pid-list] ``` ## Description @@ -18,7 +18,8 @@ For each process, print its PID (process ID), to which TTY it belongs, and invok * `-a`: Consider all processes that are associated with a TTY. * `-A` or `-e`: Consider all processes, not just those in the current TTY. * `-f`: Also print for each process: UID (as resolved username), PPID (parent PID), and STATE (Runnable, Sleeping, Selecting, Reading, etc.) -* `-q pid-list`: Only consider the given PIDs, if they exist. `pid-list` is a list of PIDs, separated by commas or spaces. +* `-p pid-list`: Only consider the given PIDs, if they exist. `pid-list` is a list of PIDs, separated by commas or spaces. +* `-q pid-list`: Only consider the given PIDs, if they exist. Output the processes in the order provided by `pid-list`. `pid-list` is a list of PIDs, separated by commas or spaces. ## Examples 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 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_list; Core::ArgsParser args_parser; @@ -85,7 +87,15 @@ ErrorOr 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 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 columns; Optional uid_column; @@ -132,9 +147,10 @@ ErrorOr 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(); };