ps: Add -p option, and distinguish it from -q

This commit is contained in:
Sam Atkins 2023-05-15 15:32:06 +01:00 committed by Andreas Kling
parent 45c429853a
commit 3aff3c610a
Notes: sideshowbarker 2024-07-16 21:42:29 +09:00
2 changed files with 22 additions and 5 deletions

View file

@ -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

View file

@ -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();
};